This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
views.py
733 lines (560 loc) · 27.1 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth import update_session_auth_hash
from django.http import HttpResponse, Http404
from django.contrib import messages
from core.utils.mail_service import send_forget_password_email
from core.utils.utils import generate_qr_code, link_scraper
from django.utils import timezone
from datetime import timedelta
import psutil
from core.models import (
UserAccount,
Subscription,
PanelConnection,
ResetPassword,
)
from core.forms import (
UserCreationForm,
AdminUserCreationForm,
AdminUserEditForm,
UserEditForm,
UserPasswordChangeForm,
UserEmailChangeForm,
SubscriptionForm,
SubscriptionEditForm,
AdminConnectionCreationForm
)
from core.utils.link_detail_api_service import get_user_info, connection_test, get_all_online_users
from GateKeeper.settings import EMAIL_ACTIVE, CUSTOM_APP_NAME
# util functions
def is_admin_or_moderator(user):
return user.account_type in ('admin', 'moderator')
def is_allowed_to_edit(user, target_user):
return user.account_type == 'admin' or \
(user.account_type == 'moderator' and target_user.account_type == 'user') or \
(user.account_type == 'moderator' and target_user.username == user.username) or \
target_user.username == user.username
def is_allowed_to_delete(user, target_user):
return user.account_type == 'admin' or \
(user.account_type == 'moderator' and target_user.account_type == 'user') or \
(user.account_type == 'moderator' and target_user.username == user.username)
def has_permission(request, user):
return (
request.user.account_type == 'admin' or
(request.user.account_type == 'moderator' and user.account_type == 'user') or
(request.user.account_type == 'moderator' and user.username == request.user.username) or
user.username == request.user.username
)
def redirect_based_on_user_type(user):
if is_admin_or_moderator(user):
return redirect('panel-admin')
else:
return redirect('user-view-links', username=user.username)
def handle_form_errors(request, form):
for field, errors in form.errors.items():
for error in errors:
messages.error(request, f'Error in {field}: {error}')
def get_subscription_links(user):
try:
links = Subscription.objects.filter(assigned_to=user)
return links.filter(is_active=True)
except ObjectDoesNotExist:
return None
def handle_object_does_not_exist(request):
messages.error(request, 'No subscription links were located.')
raise Http404("No subscription links were located.")
# view methods
def index(request) -> HttpResponse:
if request.user.is_authenticated:
return redirect_based_on_user_type(request.user)
return redirect('sign-in')
def user_sign_in(request) -> HttpResponse:
if request.user.is_authenticated:
return redirect_based_on_user_type(request.user)
context = {
'custom_app_name': CUSTOM_APP_NAME,
'page_title': ['Sign In'],
'EMAIL_ACTIVE': EMAIL_ACTIVE,
}
if request.method == 'POST':
email = request.POST.get('email')
password = request.POST.get('password')
try:
user = UserAccount.objects.get(email=email)
user_auth = authenticate(request, username=user.username, password=password)
if user_auth is not None:
login(request, user_auth)
user.last_login_ip = request.META.get('REMOTE_ADDR')
user.save()
if user.account_type in ('admin', 'moderator'):
return redirect('panel-admin')
return redirect('user-view-links', username=user.username)
except ObjectDoesNotExist:
messages.error(request, 'Invalid email or password')
if 'logout' in request.GET and request.GET['logout'] == '1':
messages.success(request, 'You have successfully logged out.')
return render(request, 'login/sign-in.html', context)
def user_sign_up(request) -> HttpResponse:
if request.user.is_authenticated:
return redirect_based_on_user_type(request.user)
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
user_form = form.save(commit=False)
user_form.account_type = 'user'
user_form.first_name = user_form.first_name.capitalize()
user_form.last_name = user_form.last_name.capitalize()
user_form.email = user_form.email.lower()
user_form.username = user_form.username.lower()
user_form.save()
messages.success(request, 'Congratulations on creating your account!')
return redirect('sign-in')
context = {
'custom_app_name': CUSTOM_APP_NAME,
'page_title': ['Sign Up'],
}
form = UserCreationForm()
context['form'] = form
return render(request, 'login/sign-up.html', context)
def user_sign_out(request) -> HttpResponse:
logout(request)
return redirect('sign-in')
def user_forget_password(request) -> HttpResponse:
if request.user.is_authenticated:
return redirect_based_on_user_type(request.user)
if not EMAIL_ACTIVE:
raise PermissionDenied
context = {
'custom_app_name': CUSTOM_APP_NAME,
'page_title': ['Forget Password'],
}
if request.method == 'POST':
try:
user = UserAccount.objects.get(email=request.POST.get("email"))
reset_password = ResetPassword.objects.create(
user=user,
)
reset_password.save()
send_forget_password_email(request, user, reset_password.key_token)
messages.success(
request, 'Your password reset instructions have been sent to your email. Check your inbox now.'
)
except ObjectDoesNotExist:
messages.error(request, 'Invalid email')
return render(request, 'login/forget-password.html', context)
def user_reset_password(request, reset_key) -> HttpResponse:
if request.user.is_authenticated:
return redirect_based_on_user_type(request.user)
if not EMAIL_ACTIVE:
raise PermissionDenied
context = {
'custom_app_name': CUSTOM_APP_NAME,
'page_title': ['Reset Password'],
"reset_key": reset_key,
}
reset_password = ResetPassword.objects.get(key_token=reset_key)
if request.method == 'POST':
try:
user = UserAccount.objects.get(id=reset_password.user.id)
password = request.POST.get('password')
password2 = request.POST.get('confirm-password')
if password == password2:
user.set_password(password)
user.save()
reset_password.delete()
user = authenticate(request, username=user.username, password=password)
if user is not None:
login(request, user)
return redirect_based_on_user_type(user)
except ObjectDoesNotExist:
messages.error(request, 'Could not change perform the password reset!')
return render(request, 'login/reset-password.html', context)
@login_required(login_url='/auth/sign-in')
def panel_admin(request) -> HttpResponse:
if is_admin_or_moderator(request.user):
context = {
'custom_app_name': CUSTOM_APP_NAME,
'request': request,
'page_title': ['User Management', 'Overview'],
}
try:
last_24_h = timezone.now() - timedelta(days=1)
last_week = timezone.now() - timedelta(days=7)
users = UserAccount.objects
context['last_24_h_active_users_count'] = len(users.filter(last_login__gte=last_24_h))
context['last_week_active_users_count'] = len(users.filter(last_login__gte=last_week))
context['overall_active_users_count'] = len(users.filter(last_login__isnull=False))
context['panel_online_users'] = get_all_online_users()
except UserAccount.DoesNotExist:
pass
context["cpu_percent"] = psutil.cpu_percent(interval=1)
context["ram_percent"] = psutil.virtual_memory().percent
return render(request, 'panel/pages/overview.html', context)
return redirect('panel-user', username=request.user.username)
@login_required(login_url='/auth/sign-in')
def panel_admin_user_list(request) -> HttpResponse:
if request.user.account_type in ('admin', 'moderator'):
context = {
'custom_app_name': CUSTOM_APP_NAME,
'request': request,
'page_title': ['User Management', 'User List'],
}
page_ordering = request.GET.get('order', '-id')
if request.user.account_type == 'admin':
users = UserAccount.objects.all().order_by(f'{page_ordering}')
else:
users = UserAccount.objects.filter(account_type='user').order_by(f'{page_ordering}')
if request.method == 'POST':
form = AdminUserCreationForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.email = user.email.lower()
user.username = user.username.lower()
form.save()
add_user_form = AdminUserCreationForm()
context['add_user_form'] = add_user_form
context['users'] = users
return render(request, 'panel/pages/users-list.html', context)
add_user_form = AdminUserCreationForm()
context['add_user_form'] = add_user_form
context['users'] = users
return render(request, 'panel/pages/users-list.html', context)
raise PermissionDenied
@login_required(login_url='/auth/sign-in')
def panel_admin_edit_user(request, username: str) -> HttpResponse:
if request.user.account_type in ['admin', 'moderator']:
context = {
'custom_app_name': CUSTOM_APP_NAME,
'request': request,
'page_title': ['User Management', 'User List', 'Edit User'],
}
user = get_object_or_404(UserAccount, username=username)
if request.method == 'POST':
form = AdminUserEditForm(request.POST, instance=user)
if form.is_valid():
user_form = form.save(commit=False)
user_form.email = user_form.email.lower() \
if user_form.email != "" else user_form.email
user_form.username = user_form.username.lower() \
if user_form.username != "" else user_form.username
user_form.first_name = user_form.first_name.capitalize() \
if user_form.first_name != "" else user_form.first_name
user_form.last_name = user_form.last_name.capitalize() \
if user_form.last_name != "" else user_form.last_name
user_form.save()
messages.success(request, 'User was successfully updated!')
return redirect('panel-admin-user-lists')
form = AdminUserEditForm(instance=user)
context['form'] = form
return render(request, 'panel/pages/admin-edit-user.html', context)
messages.error(request, 'Action Not Allowed')
return redirect('panel-user', username=request.user.username)
@login_required(login_url='/auth/sign-in')
def panel_admin_create_link(request, username: str) -> HttpResponse:
if request.user.account_type in ['admin', 'moderator']:
context = {
'custom_app_name': CUSTOM_APP_NAME,
'request': request,
'page_title': ['User Management', 'User List', 'Create Link'],
}
user = get_object_or_404(UserAccount, username=username)
if request.method == 'POST':
form = SubscriptionForm(request.POST)
if form.is_valid():
link_form = form.save(commit=False)
link_form.created_by = request.user
link_form.assigned_to = user
link_form.expose = False
link_form.save()
messages.success(
request, f'Link was successfully assigned to {user.first_name} {user.last_name} [{user.username}].'
)
return redirect('panel-admin-user-lists')
form = SubscriptionForm()
context['form'] = form
context['user'] = user
return render(request, 'panel/pages/admin-create-link.html', context)
messages.error(request, 'Action Not Allowed')
return redirect('panel-user', username=request.user.username)
@login_required(login_url='/auth/sign-in')
def panel_admin_edit_link(request, shorten_uuid_link: str) -> HttpResponse:
if is_admin_or_moderator(request.user):
context = {
'custom_app_name': CUSTOM_APP_NAME,
'request': request,
'page_title': ['User Management', 'User List', 'Edit Link'],
}
link = get_object_or_404(Subscription, subscription_uuid=shorten_uuid_link)
if request.method == 'POST':
form = SubscriptionEditForm(request.POST, instance=link)
if form.is_valid():
link_instance = form.save(commit=False)
link_instance.save()
messages.success(
request,
f'Link with the id [{link.subscription_uuid}] was successfully updated.'
)
return redirect('user-view-links', username=link.assigned_to.username)
form = SubscriptionEditForm(instance=link)
context['form'] = form
context['user'] = link.assigned_to
return render(request, 'panel/pages/admin-edit-link.html', context)
messages.error(request, 'Action Not Allowed')
return redirect('panel-user', username=request.user.username)
@login_required(login_url='/auth/sign-in')
def panel_admin_delete_link(request, shorten_uuid_link: str) -> HttpResponse:
if is_admin_or_moderator(request.user):
link = get_object_or_404(Subscription, subscription_uuid=shorten_uuid_link)
if is_allowed_to_delete(request.user, link.assigned_to):
link.delete()
return redirect('user-view-links', username=link.assigned_to.username)
raise PermissionDenied
@login_required(login_url='/auth/sign-in')
def panel_admin_delete_user(request, username: str) -> HttpResponse:
if is_admin_or_moderator(request.user):
user = UserAccount.objects.get(username=username)
if is_allowed_to_edit(request.user, user):
user.delete()
return redirect('panel-admin-user-lists')
raise PermissionDenied
@login_required(login_url='/auth/sign-in')
def panel_admin_setting_panel_connection(request) -> HttpResponse:
if request.user.account_type == 'admin':
context = {
'custom_app_name': CUSTOM_APP_NAME,
'request': request,
'page_title': ['Panel Setting', 'Panel Connection'],
}
if request.method == 'POST':
form = AdminConnectionCreationForm(request.POST)
if form.is_valid():
connection_form = form.save(commit=False)
connection_form.url = connection_form.url.rstrip('/')
if connection_form.panel_name != PanelConnection.panel_marzban:
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()
context['panel_connections'] = panel_connections
context['connection_creation_form'] = AdminConnectionCreationForm()
return render(request, 'panel/pages/admin-setting-panel-connection.html', context)
return redirect('panel-user', username=request.user.username)
@login_required(login_url='/auth/sign-in')
def panel_admin_setting_panel_edit_connection(request, connection_id: int) -> HttpResponse:
if request.user.account_type == 'admin':
context = {
'custom_app_name': CUSTOM_APP_NAME,
'request': request,
'page_title': ['Panel Setting', 'Edit Connection'],
}
panel_connection = get_object_or_404(PanelConnection, id=connection_id)
if request.method == 'POST':
form = AdminConnectionCreationForm(request.POST, instance=panel_connection)
if form.is_valid():
connection_form = form.save(commit=False)
connection_form.url = connection_form.url.rstrip('/')
if connection_form.panel_name != PanelConnection.panel_marzban:
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')
connection_form = AdminConnectionCreationForm(instance=panel_connection)
context['panel_connection'] = panel_connection
context['connection_form'] = connection_form
return render(request, 'panel/pages/admin-setting-panel-edit-connection.html', context)
raise PermissionDenied
@login_required(login_url='/auth/sign-in')
def panel_admin_setting_panel_deleted_connection(request, connection_id: int) -> HttpResponse:
if request.user.account_type == 'admin':
panel_connection = get_object_or_404(PanelConnection, id=connection_id)
panel_connection.delete()
return redirect('panel-admin-setting-panel-connection')
raise PermissionDenied
@login_required(login_url='/auth/sign-in')
def panel_user_profile_overview(request, username: str) -> HttpResponse:
context = {
'request': request,
}
if request.user.username == username or is_admin_or_moderator(request.user):
user = get_object_or_404(UserAccount, username=username)
context['page_title'] = ['Users', 'Profile', 'User Profile']
context['custom_app_name'] = CUSTOM_APP_NAME
context['user'] = user
context['form'] = UserEditForm(instance=user)
context['email_change_form'] = UserEmailChangeForm(instance=user)
context['password_reset_form'] = UserPasswordChangeForm(user)
return render(request, 'panel/pages/user-profile.html', context)
return redirect('sign-in')
@login_required(login_url='/auth/sign-in')
def panel_user_edit(request, username: str) -> HttpResponse:
user = get_object_or_404(UserAccount, username=username)
if request.method == 'POST':
form = UserEditForm(request.POST, instance=user)
if form.is_valid():
if has_permission(request, user):
user_form = form.save(commit=False)
user_form.username = user_form.username.lower() \
if user_form.username != "" else user_form.username
user_form.first_name = user_form.first_name.capitalize() \
if user_form.first_name != "" else user_form.first_name
user_form.last_name = user_form.last_name.capitalize() \
if user_form.last_name != "" else user_form.last_name
user_form.save()
messages.success(request, 'Your Profile was successfully updated!')
return redirect('panel-user-profile', username=user_form.username)
else:
handle_form_errors(request, form)
return redirect('panel-user-profile', username=username)
messages.error(request, 'Action Not Allowed')
return redirect('panel-user-profile', username=username)
@login_required(login_url='/auth/sign-in')
def panel_user_change_email(request, username):
user = get_object_or_404(UserAccount, username=username)
if request.method == 'POST':
form = UserEmailChangeForm(request.POST, instance=user)
if form.is_valid():
if request.user.account_type == 'admin' or \
(request.user.account_type == 'moderator' and user.account_type == 'user') or \
(request.user.account_type == 'moderator' and user.username == request.user.username) or \
user.username == request.user.username:
form.save()
update_session_auth_hash(request, user)
messages.success(request, 'Your E-mail was successfully updated!')
else:
handle_form_errors(request, form)
return redirect('panel-user-profile', username=username)
messages.error(request, 'Action Not Allowed')
return redirect('panel-user-profile', username=username)
@login_required(login_url='/auth/sign-in')
def panel_user_reset_password(request, username: str) -> HttpResponse:
user = get_object_or_404(UserAccount, username=username)
if request.method == 'POST':
form = UserPasswordChangeForm(user, request.POST)
if form.is_valid() and user.check_password(form.cleaned_data['old_password']):
if (
request.user.account_type == 'admin' or
(request.user.account_type == 'moderator' and user.account_type == 'user') or
(request.user.account_type == 'moderator' and user.username == request.user.username) or
user.username == request.user.username
):
form.save()
update_session_auth_hash(request, user)
messages.success(request, 'Your password was successfully updated!')
else:
handle_form_errors(request, form)
return redirect('panel-user-profile', username=username)
messages.error(request, 'Action Not Allowed')
return redirect('panel-user-profile', username=username)
@login_required(login_url='/auth/sign-in')
def user_view_links(request, username: str) -> HttpResponse:
context = {
'custom_app_name': CUSTOM_APP_NAME,
'page_title': ['Link Control', 'View Links'],
'request': request,
}
user = get_object_or_404(UserAccount, username=username)
if request.user.username == username or \
(request.user.account_type == 'moderator' and user.account_type == 'user') or \
request.user.account_type == 'admin':
try:
links = Subscription.objects.filter(assigned_to=user)
context['links'] = links.filter(is_active=True)
context['exposed_links'] = links.filter(is_active=True, expose=True)
context['restricted_links'] = links.filter(is_active=True, expose=False)
except ObjectDoesNotExist:
messages.error(request, 'No subscription links were located.')
return render(request, 'panel/pages/user-view-links.html', context)
raise Http404("No subscription links were located.")
@login_required(login_url='/auth/sign-in')
def user_view_single_link(request, shorten_uuid_link) -> HttpResponse:
context = {
'custom_app_name': CUSTOM_APP_NAME,
'page_title': ['Link Control', 'View Links'],
'request': request,
}
try:
link = Subscription.objects.get(subscription_uuid=shorten_uuid_link)
if request.user.username == link.assigned_to.username or \
(request.user.account_type == 'moderator' and link.assigned_to.account_type == 'user') or \
request.user.account_type == 'admin':
context['link'] = link
context['scheme_host'] = request.scheme + '://' + request.get_host()
if not link.panel_connection:
qrcode_data = generate_qr_code(
link.subscription_link
)
context['qrcode'] = qrcode_data
elif link.panel_connection.panel_name == PanelConnection.panel_marzban:
context['link_details'] = get_user_info(link)
qrcode_data = generate_qr_code(
f'{context.get("scheme_host")}/panel/user/user-link/show/{link.subscription_uuid}'
)
context['qrcode'] = qrcode_data
else:
context['link_details'] = get_user_info(link)
qrcode_data = generate_qr_code(
link.subscription_link
)
context['qrcode'] = qrcode_data
return render(request, 'panel/pages/user-view-link-activation.html', context)
except ObjectDoesNotExist:
handle_object_does_not_exist(request)
@login_required(login_url='/auth/sign-in')
def user_view_single_link_expose(request, shorten_uuid_link) -> HttpResponse:
try:
link = Subscription.objects.get(subscription_uuid=shorten_uuid_link)
if request.user.username == link.assigned_to.username or \
(request.user.account_type == 'moderator' and link.assigned_to.account_type == 'user') or \
request.user.account_type == 'admin':
link.expose = True
link.save()
return redirect('user_link_page', shorten_uuid_link=shorten_uuid_link)
except ObjectDoesNotExist:
handle_object_does_not_exist(request)
@login_required(login_url='/auth/sign-in')
def user_view_single_link_restrict(request, shorten_uuid_link) -> HttpResponse:
try:
link = Subscription.objects.get(subscription_uuid=shorten_uuid_link)
if request.user.username == link.assigned_to.username or \
(request.user.account_type == 'moderator' and link.assigned_to.account_type == 'user') or \
request.user.account_type == 'admin':
link.expose = False
link.save()
return redirect('user_link_page', shorten_uuid_link=shorten_uuid_link)
except ObjectDoesNotExist:
handle_object_does_not_exist(request)
def user_view_single_link_show(request, shorten_uuid_link) -> HttpResponse:
user_ip = request.META['REMOTE_ADDR']
try:
link = Subscription.objects.get(subscription_uuid=shorten_uuid_link)
if link.expose or link.assigned_to.last_login_ip == user_ip:
link.use_count += 1
link.save()
if link.panel_connection:
if link.panel_connection.panel_name == PanelConnection.panel_marzban:
return HttpResponse(link_scraper(link.subscription_link))
else:
return HttpResponse(link.subscription_link)
else:
raise Http404("No subscription link were located.")
except ObjectDoesNotExist:
handle_object_does_not_exist(request)