diff --git a/db.sqlite3 b/db.sqlite3 index 51f6569d..9d3de9e8 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/wg_api/__pycache__/views.cpython-310.pyc b/wg_api/__pycache__/views.cpython-310.pyc index ddd5ebf1..284844ba 100644 Binary files a/wg_api/__pycache__/views.cpython-310.pyc and b/wg_api/__pycache__/views.cpython-310.pyc differ diff --git a/wg_api/__pycache__/views.cpython-311.pyc b/wg_api/__pycache__/views.cpython-311.pyc index 3a51dd6a..e05b6ad9 100644 Binary files a/wg_api/__pycache__/views.cpython-311.pyc and b/wg_api/__pycache__/views.cpython-311.pyc differ diff --git a/wg_api/utils/__pycache__/reqs_handler.cpython-310.pyc b/wg_api/utils/__pycache__/reqs_handler.cpython-310.pyc index abe2ee12..87b6de4b 100644 Binary files a/wg_api/utils/__pycache__/reqs_handler.cpython-310.pyc and b/wg_api/utils/__pycache__/reqs_handler.cpython-310.pyc differ diff --git a/wg_api/utils/__pycache__/reqs_handler.cpython-311.pyc b/wg_api/utils/__pycache__/reqs_handler.cpython-311.pyc index 8768bb90..34175a5e 100644 Binary files a/wg_api/utils/__pycache__/reqs_handler.cpython-311.pyc and b/wg_api/utils/__pycache__/reqs_handler.cpython-311.pyc differ diff --git a/wg_api/utils/reqs_handler.py b/wg_api/utils/reqs_handler.py index 5c028baa..f66403d9 100644 --- a/wg_api/utils/reqs_handler.py +++ b/wg_api/utils/reqs_handler.py @@ -54,3 +54,31 @@ def req_apply_sys(): # # http://10.30.1.138:5000/api/client/caivc1vekn84im2m6ec0?_=1669623393468 + +def req_ip_recommendation(): + # Write your own ip recommendation next time + # http://10.30.1.138:5000/api/suggest-client-ips + + inf_s = get_session() + req_url = 'http://10.30.1.138:5000/api/suggest-client-ips' + + req = inf_s.get(req_url) + req = req.json() + + req_dump = {} + req_dump["allocated_ips"] = {} + req_dump["allowed_ips"] = {"OAM":"10.30.1.0/24", "NEW_OAM":"192.168.200.0/24", "WG Nodes":"192.168.100.0/24"} + + i = 0 + + tmp_array = [] + for x in req: + print(x) + tmp_array.append(x) + i = i+1 + + req_dump["allocated_ips"] = tmp_array + print(req) + print(req_dump) + + return json.dumps(req_dump) diff --git a/wg_api/views.py b/wg_api/views.py index 66a5a692..5e123559 100644 --- a/wg_api/views.py +++ b/wg_api/views.py @@ -1,18 +1,21 @@ from django.shortcuts import render from django.http import HttpResponse +from django.contrib.auth import authenticate # from django.core import serializer from rest_framework import viewsets +from rest_framework.decorators import api_view, permission_classes from rest_framework.views import APIView from rest_framework.authentication import SessionAuthentication, BasicAuthentication from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response +from rest_framework.authtoken.models import Token from rest_framework.permissions import IsAdminUser from .serializers import wgSerializer from .models import user_model from .utils.html_parser import get_logs, get_profiles import json from .utils.statistics import count_clients, count_traffic, count_connection, count_percentage, count_broken -from .utils.reqs_handler import req_edit_state, req_edit_user, req_apply_sys +from .utils.reqs_handler import req_edit_state, req_edit_user, req_apply_sys, req_ip_recommendation from django.middleware.csrf import get_token from django.views.decorators.csrf import csrf_exempt @@ -21,11 +24,40 @@ class wgLogin(APIView): permission_classes = [IsAuthenticated] def get(self, request, format=None): + + # serializer = self.get_serializer(data=request.data) + # serializer.is_valid(raise_exception=True) + # user = serializer.validated_data['user'] + user = request.GET.get('username') + password = request.GET.get('password') + content = { 'user': str(request.user), # `django.contrib.auth.User` instance. 'auth': str(request.auth), # None } - return Response(content) + + # user = content['user'] + print(request.META['HTTP_AUTHORIZATION']) + + print(user) + print(password) + + user = authenticate(username=user, password=password) + + print("****-> ",user.id) + + token, _ = Token.objects.get_or_create(user=user) + + response = Response(content) + + response.set_cookie('Token', token.key) + + # print(response) + + return response + # return response + + # return Response(content) # Create your views here. class wgView(viewsets.ModelViewSet): @@ -44,6 +76,16 @@ class wgView(viewsets.ModelViewSet): # serializer = wgSerializer # permission_classes = [IsAdminUser] +@csrf_exempt +@api_view(["GET"]) +@permission_classes([IsAuthenticated]) +def wgCheckAuth(request): + + return HttpResponse(json.dumps({"Auth":True})) + +@csrf_exempt +@api_view(["GET"]) +@permission_classes([IsAuthenticated]) def wgViewLogs(request): param = request.GET.get('param', '') @@ -134,6 +176,9 @@ def wgViewLogs(request): return HttpResponse(final_data, content_type='application') +@csrf_exempt +@api_view(["GET"]) +@permission_classes([IsAuthenticated]) def wgViewClients(request): param = request.GET.get('param', '') @@ -145,6 +190,7 @@ def wgViewClients(request): final_data = [] + # if request.user.is_authenticated: if filter : filter = str(filter).split(" ") for x in filter: @@ -229,11 +275,15 @@ def wgViewClients(request): tmp_val = x["email"] tmp_val = str(tmp_val).split("@")[1] tmp_val = tmp_val.lower() - if {"email": tmp_val} not in groups: - # print("-> ",tmp_val) - groups.append({"email": tmp_val}) - final_data = json.dumps(groups) + if tmp_val not in groups: + groups.append(tmp_val) + + # if {"email": tmp_val} not in groups: + # # print("-> ",tmp_val) + # groups.append({"email": tmp_val}) + + final_data = json.dumps({"email":groups}) if param == "orgs": groups = [] @@ -242,12 +292,15 @@ def wgViewClients(request): tmp_val = x["name"] tmp_val = str(tmp_val).split("-")[0] tmp_val = tmp_val.upper() + + if tmp_val not in groups: + groups.append(tmp_val) - if {"orgs": tmp_val} not in groups: - # print("-> ",tmp_val) - groups.append({"orgs": tmp_val}) + # if {"orgs": tmp_val} not in groups: + # # print("-> ",tmp_val) + # groups.append({"orgs": tmp_val}) - final_data = json.dumps(groups) + final_data = json.dumps({"orgs": groups}) if param == "stats": data_logs = get_logs() @@ -278,11 +331,16 @@ def wgViewClients(request): else : final_data = json.dumps(data) + # else: + # final_data = {"Not Logged In"} # print(request.GET.get('param', '')) return HttpResponse(final_data, content_type='application') +@csrf_exempt +@api_view(["GET"]) +@permission_classes([IsAuthenticated]) def get_ApplyConfig(request): # id = request.GET.get('id', '') @@ -293,6 +351,9 @@ def get_ApplyConfig(request): return HttpResponse(req_response) +@csrf_exempt +@api_view(["GET", "POST"]) +@permission_classes([IsAuthenticated]) def get_EditState(request): id = request.GET.get('id', '') @@ -325,6 +386,9 @@ def get_EditState(request): # return get_token() # @csrf_exempt +@csrf_exempt +@api_view(["GET"]) +@permission_classes([IsAuthenticated]) def get_EditClient(request): my_token = get_token() @@ -353,6 +417,104 @@ def get_EditClient(request): return HttpResponse(ret_val, content_type='application') - +@csrf_exempt +@api_view(["GET"]) +@permission_classes([IsAuthenticated]) +def get_user_param_recommendation(request): + ret_val = req_ip_recommendation() + + return HttpResponse(ret_val, content_type='application') + +@csrf_exempt +@api_view(["GET"]) +@permission_classes([IsAuthenticated]) + +def wgFilterClients(request): + + param = request.GET.get('param', '') + filter = request.GET.get('filter', '') + name = request.GET.get('names', '') + orgs = request.GET.get('orgs', '') + emails = request.GET.get('emails', '') + state = request.GET.get('state', '') + + print("param ",param) + print("filter ",filter) + print("name ",name) + print("orgs ",orgs) + print("emails ",emails) + print("state ",state) + + data = get_profiles() + data = json.loads(data) + + final_data = [] + + if orgs: + orgs = str(orgs).split(", ") + tmp_data = [] + for x in orgs: + print(x) + f_orgs = x + i = 0 + for x in data: + if str(f_orgs).lower() in str(x["name"]).lower(): + x["data_id"]=i + tmp_data.append(x) + i = i+1 + + print(tmp_data) + + if len(final_data) < 1: + final_data = tmp_data + else: + for x in tmp_data: + final_data.append(x) + + if emails: + emails = str(emails).split(", ") + tmp_data = [] + for x in emails: + print(x) + f_emails = x + i = 0 + for x in data: + if str(f_emails).lower() in str(x["email"]).lower(): + x["data_id"]=i + tmp_data.append(x) + i = i+1 + + if len(final_data) < 1: + final_data = tmp_data + else: + for x in tmp_data: + final_data.append(x) + + if not (orgs or filter or name or emails): + final_data = data + + # SANITY Starts here ------------------------------- + + no_duplicate = [] + for x in final_data: + if x not in no_duplicate: + no_duplicate.append(x) + + ret_val = json.dumps(no_duplicate) + + return HttpResponse(ret_val, content_type='application') + +@csrf_exempt +@api_view(["POST"]) +@permission_classes([IsAuthenticated]) +def wgBulkReg(request): + + print("-> Bulk Registration") + RegData = json.loads(request.body) + + for x in RegData: + print(x) + + return HttpResponse({"success"}, content_type='application') diff --git a/wgweb/__pycache__/settings.cpython-310.pyc b/wgweb/__pycache__/settings.cpython-310.pyc index c9f8117e..6d50c353 100644 Binary files a/wgweb/__pycache__/settings.cpython-310.pyc and b/wgweb/__pycache__/settings.cpython-310.pyc differ diff --git a/wgweb/__pycache__/settings.cpython-311.pyc b/wgweb/__pycache__/settings.cpython-311.pyc index d8eb1972..dd1c287d 100644 Binary files a/wgweb/__pycache__/settings.cpython-311.pyc and b/wgweb/__pycache__/settings.cpython-311.pyc differ diff --git a/wgweb/__pycache__/urls.cpython-310.pyc b/wgweb/__pycache__/urls.cpython-310.pyc index 5b1596bf..56eff39a 100644 Binary files a/wgweb/__pycache__/urls.cpython-310.pyc and b/wgweb/__pycache__/urls.cpython-310.pyc differ diff --git a/wgweb/__pycache__/urls.cpython-311.pyc b/wgweb/__pycache__/urls.cpython-311.pyc index eb2cf378..446fe80d 100644 Binary files a/wgweb/__pycache__/urls.cpython-311.pyc and b/wgweb/__pycache__/urls.cpython-311.pyc differ diff --git a/wgweb/settings.py b/wgweb/settings.py index 67fc44f6..12bb1506 100644 --- a/wgweb/settings.py +++ b/wgweb/settings.py @@ -39,6 +39,7 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'corsheaders', 'rest_framework', + 'rest_framework.authtoken', 'wg_api' ] @@ -65,6 +66,13 @@ CORS_ORIGIN_WHITELIST = [ CSRF_TRUSTED_ORIGINS = ['https://wg.nnag.me'] CSRF_COOKIE_SECURE = True + +REST_FRAMEWORK = { + 'DEFAULT_AUTHENTICATION_CLASSES': [ + 'rest_framework.authentication.TokenAuthentication', # <-- And here + ], +} + ROOT_URLCONF = 'wgweb.urls' TEMPLATES = [ @@ -133,6 +141,8 @@ USE_TZ = True STATIC_URL = 'static/' +# LOGIN_REDIRECT_URL = '/admin/dashboard' + # Default primary key field type # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field diff --git a/wgweb/urls.py b/wgweb/urls.py index c66f8dd6..9d988222 100644 --- a/wgweb/urls.py +++ b/wgweb/urls.py @@ -26,7 +26,11 @@ urlpatterns = [ path('api2/wgLogin', views.wgLogin.as_view()), path('api2/wgLogs', views.wgViewLogs), path('api2/wgClients', views.wgViewClients), + path('api2/wgFilterClients', views.wgFilterClients), path('api2/wgEditState', views.get_EditState), path('api2/wgSysApply', views.get_ApplyConfig), + path('api2/wgBulkReg', views.wgBulkReg), + path('api2/wgUserRecommendations', views.get_user_param_recommendation), + path('api2/wgCheckAuth', views.wgCheckAuth), path('api2/wgEditClient', views.get_EditClient) ]