Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
using cookie to check the traffic
Browse files Browse the repository at this point in the history
  • Loading branch information
drunkleen committed Mar 5, 2024
1 parent d68a18a commit 53a6597
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 46 deletions.
8 changes: 4 additions & 4 deletions core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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',
Expand Down
8 changes: 5 additions & 3 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 28 additions & 17 deletions core/utils/link_detail_api_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
32 changes: 22 additions & 10 deletions core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand 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')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h2 class="fw-bold">Add Connection</h2>

<div class="fv-row mb-7 fv-plugins-icon-container">
<label class="fw-semibold fs-6 mb-2">Panel URL</label>
{{ connection_creation_form.panel_url }}
{{ connection_creation_form.url }}
<div class="fv-plugins-message-container invalid-feedback"></div>
</div>

Expand Down Expand Up @@ -91,13 +91,13 @@ <h2 class="fw-bold">Add Connection</h2>
<div id="panel_info_status" hidden="">
<div class="fv-row mb-7 fv-plugins-icon-container">
<label class="fw-semibold fs-6 mb-2">Panel Username</label>
{{ connection_creation_form.panel_user }}
{{ connection_creation_form.username }}
<div class="fv-plugins-message-container invalid-feedback"></div>
</div>
<div class="fv-row mb-7 fv-plugins-icon-container">
<label class="fw-semibold fs-6 mb-2">{{ connection_creation_form.panel_password.label }}</label>
<label class="fw-semibold fs-6 mb-2">{{ connection_creation_form.password.label }}</label>
<input type="password"
name="{{ connection_creation_form.panel_password.name }}"
name="{{ connection_creation_form.password.name }}"
maxlength="256"
class="form-control form-control-solid mb-3 mb-lg-0"
placeholder="Panel Password"
Expand Down Expand Up @@ -184,8 +184,8 @@ <h2 class="fw-bold">Add Connection</h2>
<td data-order="{{ connection.panel_name }}">
{{ connection.panel_name }}
</td>
<td data-order="{{ connection.panel_url }}">
<div class="badge badge-light fw-bold">{{ connection.panel_url }}</div>
<td data-order="{{ connection.url }}">
<div class="badge badge-light fw-bold">{{ connection.url }}</div>
</td>
<td>
{% if connection.is_active %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ <h3 class="fw-bold m-0">Profile Details</h3>
</div>

<div class="row mb-6">
<label class="col-lg-4 col-form-label required fw-semibold fs-6">{{ connection_form.panel_url.label }}</label>
<label class="col-lg-4 col-form-label required fw-semibold fs-6">{{ connection_form.url.label }}</label>

<div class="col-lg-8 fv-row fv-plugins-icon-container">
{{ connection_form.panel_url }}
{{ connection_form.url }}
<div class="fv-plugins-message-container fv-plugins-message-container--enabled invalid-feedback"></div></div>
</div>

Expand Down Expand Up @@ -77,19 +77,19 @@ <h3 class="fw-bold m-0">Profile Details</h3>
{% if panel_connection.panel_name == "Marzban" %}hidden="" {% endif %}>

<div class="row mb-6">
<label class="col-lg-4 col-form-label required fw-semibold fs-6">{{ connection_form.panel_user.label }}</label>
<label class="col-lg-4 col-form-label required fw-semibold fs-6">{{ connection_form.username.label }}</label>

<div class="col-lg-8 fv-row fv-plugins-icon-container">
{{ connection_form.panel_user }}
{{ connection_form.username }}
<div class="fv-plugins-message-container fv-plugins-message-container--enabled invalid-feedback"></div></div>
</div>

<div class="row mb-6">
<label class="col-lg-4 col-form-label required fw-semibold fs-6">{{ connection_form.panel_password.label }}</label>
<label class="col-lg-4 col-form-label required fw-semibold fs-6">{{ connection_form.password.label }}</label>

<div class="col-lg-8 fv-row fv-plugins-icon-container">
<input type="password"
name="{{ connection_form.panel_password.name }}"
name="{{ connection_form.password.name }}"
maxlength="256"
class="form-control form-control-solid mb-3 mb-lg-0"
placeholder="Panel Password"
Expand Down

0 comments on commit 53a6597

Please sign in to comment.