diff --git a/core/forms.py b/core/forms.py index e3ba57f..fab1164 100644 --- a/core/forms.py +++ b/core/forms.py @@ -160,7 +160,7 @@ def save(self, commit=True): class AdminConnectionCreationForm(forms.ModelForm): class Meta: model = PanelConnection - fields = ("connection_name", "panel_name", "panel_url", "panel_user", "panel_password") + fields = ("connection_name", "panel_name", "url", "username", "password") def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -176,19 +176,19 @@ def __init__(self, *args, **kwargs): 'autocomplete': "off", }) - self.fields['panel_url'].widget.attrs.update({ + self.fields['url'].widget.attrs.update({ 'class': 'form-control form-control-solid mb-3 mb-lg-0', 'placeholder': 'IP', 'autocomplete': "off", }) - self.fields['panel_user'].widget.attrs.update({ + self.fields['username'].widget.attrs.update({ 'class': 'form-control form-control-solid mb-3 mb-lg-0', 'placeholder': 'Panel Username', 'autocomplete': "off", }) - self.fields['panel_password'].widget.attrs.update({ + self.fields['password'].widget.attrs.update({ 'class': 'form-control form-control-solid mb-3 mb-lg-0', 'placeholder': 'Panel Password', 'type': 'password', diff --git a/core/models.py b/core/models.py index 3b3ac0b..c506f47 100644 --- a/core/models.py +++ b/core/models.py @@ -55,9 +55,11 @@ class PanelConnection(models.Model): ) panel_name = models.CharField(choices=panel_choice, default=panel_marzban, max_length=32) - panel_url = models.CharField(max_length=64, null=True, blank=True) - panel_user = models.CharField(max_length=128, null=True, blank=True) - panel_password = models.CharField(max_length=256, null=True, blank=True) + url = models.CharField(max_length=64, null=True, blank=True) + username = models.CharField(max_length=128, null=True, blank=True) + password = models.CharField(max_length=256, null=True, blank=True) + + session_cookie = models.CharField(max_length=1024, null=True, blank=True) is_active = models.BooleanField(default=True) updated_at = models.DateTimeField(auto_now=True) diff --git a/core/utils/link_detail_api_service.py b/core/utils/link_detail_api_service.py index f69c142..7789253 100644 --- a/core/utils/link_detail_api_service.py +++ b/core/utils/link_detail_api_service.py @@ -27,9 +27,29 @@ def byte_to_gigabytes(byte_data): PATH_GET_TRAFFIC = 'getClientTraffics/' -def get_user_info(link: Subscription): - session = requests.Session() +def connection_test(username, password, panel_url): + body = {"username": username, "password": password} + try: + response = requests.post(f"{panel_url}/login", data=json.dumps(body), headers=headers) + if response.status_code == 200 and response.json()['success']: + return [response.headers['Set-Cookie'], True] + + return [None, False] + except: + return [None, False] + + +def set_new_session(session_data: Subscription): + session_cookie, is_panel_active = connection_test(session_data.panel_connection.username, + session_data.panel_connection.password, + session_data.panel_connection.url) + + if is_panel_active: + panel_connection = PanelConnection.objects.get(session_data.panel_connection) + panel_connection.save() + +def get_user_info(link: Subscription): if link.panel_connection.panel_name == PanelConnection.panel_marzban: response = requests.get(link.subscription_link) if response.status_code == 200: @@ -47,17 +67,16 @@ def get_user_info(link: Subscription): else: return None - body = {"username": link.panel_connection.panel_user, "password": link.panel_connection.panel_password} - session.post(f"{link.panel_connection.panel_url}/login", data=json.dumps(body), headers=headers) + headers['Cookie'] = link.panel_connection.session_cookie if link.panel_connection.panel_name == PanelConnection.panel_alireza: - response = session.get( - f"{link.panel_connection.panel_url}{PATH_API_alireza}{PATH_GET_TRAFFIC}{link.user_email_in_xui_panel}", + response = requests.get( + f"{link.panel_connection.url}{PATH_API_alireza}{PATH_GET_TRAFFIC}{link.user_email_in_xui_panel}", headers=headers ) elif link.panel_connection.panel_name == PanelConnection.panel_sanaei: - response = session.get( - f"{link.panel_connection.panel_url}{PATH_API_MHSANAEI}{PATH_GET_TRAFFIC}{link.user_email_in_xui_panel}", + response = requests.get( + f"{link.panel_connection.url}{PATH_API_MHSANAEI}{PATH_GET_TRAFFIC}{link.user_email_in_xui_panel}", headers=headers ) else: @@ -71,13 +90,5 @@ def get_user_info(link: Subscription): 'expire': json.loads(response.text)['obj']['expiryTime'], } else: + set_new_session(link) return None - - -def connection_test(username, password, panel_url): - body = {"username": username, "password": password} - try: - response = requests.post(f"{panel_url}/login", data=json.dumps(body), headers=headers) - return response.status_code == 200 and response.json()['success'] - except: - return False diff --git a/core/views.py b/core/views.py index 8682509..8a2a31c 100644 --- a/core/views.py +++ b/core/views.py @@ -414,13 +414,19 @@ def panel_admin_setting_panel_connection(request) -> HttpResponse: form = AdminConnectionCreationForm(request.POST) if form.is_valid(): connection_form = form.save(commit=False) - connection_form.panel_url = connection_form.panel_url.rstrip('/') + connection_form.url = connection_form.url.rstrip('/') if connection_form.panel_name != PanelConnection.panel_marzban: - connection_form.is_active = connection_test( - connection_form.panel_user, - connection_form.panel_password, - connection_form.panel_url, + + session_panel_cookie, is_panel_active = connection_test( + connection_form.username, + connection_form.password, + connection_form.url, ) + if is_panel_active: + connection_form.is_active = True + connection_form.session_cookie = session_panel_cookie + else: + connection_form.is_active = False connection_form.save() panel_connections = PanelConnection.objects.all() @@ -446,13 +452,19 @@ def panel_admin_setting_panel_edit_connection(request, connection_id: int) -> Ht form = AdminConnectionCreationForm(request.POST, instance=panel_connection) if form.is_valid(): connection_form = form.save(commit=False) - connection_form.panel_url = connection_form.panel_url.rstrip('/') + connection_form.url = connection_form.url.rstrip('/') if connection_form.panel_name != PanelConnection.panel_marzban: - connection_form.is_active = connection_test( - connection_form.panel_user, - connection_form.panel_password, - connection_form.panel_url, + session_panel_cookie, is_panel_active = connection_test( + connection_form.username, + connection_form.password, + connection_form.url, ) + if is_panel_active: + connection_form.is_active = True + connection_form.session_cookie = session_panel_cookie + else: + connection_form.is_active = False + connection_form.save() return redirect('panel-admin-setting-panel-connection') diff --git a/templates/panel/components/page/admin-setting-panel-connection.html b/templates/panel/components/page/admin-setting-panel-connection.html index f7cf986..9f90895 100644 --- a/templates/panel/components/page/admin-setting-panel-connection.html +++ b/templates/panel/components/page/admin-setting-panel-connection.html @@ -53,7 +53,7 @@