NEW API :

* /api2/wgEdit?id=<USER ID>&state=<ENABLED/DISABLED>
This commit is contained in:
infidel 2022-11-24 17:11:55 +07:00
parent 98cf19b7ab
commit 24cb941f94
22 changed files with 164 additions and 34 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.swp *.swp
/inf-env/

BIN
wg_api/.views.py.swo Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -151,6 +151,7 @@ def get_profiles():
current_data["data_id"]=i current_data["data_id"]=i
final_data.append(current_data) final_data.append(current_data)
i = i + 1 i = i + 1
final_data = json.dumps(final_data) final_data = json.dumps(final_data)
# print(final_data) # print(final_data)

View File

@ -0,0 +1,29 @@
import json
import requests
def get_session():
login_data = {'username':"admin","password":"admin"}
inf_s = requests.Session()
inf_s.post('http://10.30.1.138:5000/login', login_data)
return inf_s
def req_edit_state(id, state):
inf_s = get_session()
if state == "false":
state = False
if state == "true":
state = True
req_val = {"id":id,"status":state}
req_val = json.dumps(req_val)
print("Req Val --> ", req_val)
req = inf_s.post('http://10.30.1.138:5000/client/set-status', req_val)
print(req)

View File

@ -8,6 +8,7 @@ from .models import user_model
from .utils.html_parser import get_logs, get_profiles from .utils.html_parser import get_logs, get_profiles
import json import json
from .utils.statistics import count_clients, count_traffic, count_connection, count_percentage, count_broken from .utils.statistics import count_clients, count_traffic, count_connection, count_percentage, count_broken
from .utils.reqs_handler import req_edit_state
# Create your views here. # Create your views here.
class wgView(viewsets.ModelViewSet): class wgView(viewsets.ModelViewSet):
@ -127,48 +128,145 @@ def wgViewClients(request):
final_data = [] final_data = []
if filter : if filter :
print("x"*100)
i = 1 filter = str(filter).split(" ")
for x in data:
if str(filter).lower() in str(x["name"]).lower(): for x in filter:
print(x["data_id"]) print("-> ",x)
x["data_id"]=i
print(x["data_id"]) final_data = data
final_data.append(x)
i = i+1 if not (filter[0] == "" and filter[1] == "undefined" and filter[2] == "undefined"):
if filter[0] != "":
f_name = filter[0]
i = 0
tmp_data = []
for x in final_data:
if str(f_name).lower() in str(x["name"]).lower():
x["data_id"]=i
tmp_data.append(x)
i = i+1
final_data = tmp_data
if filter[1] != "undefined":
f_orgs = filter[1]
i = 0
tmp_data = []
for x in final_data:
if str(f_orgs).lower() in str(x["name"]).lower():
x["data_id"]=i
tmp_data.append(x)
i = i+1
final_data = tmp_data
if filter[2] != "undefined":
i = 0
f_email = filter[2]
tmp_data = []
if f_email == "" :
print("Empty mails...")
for x in final_data:
if str(x["email"]).lower() == "":
x["data_id"]=i
tmp_data.append(x)
i = i+1
else:
for x in final_data:
if str(f_email).lower() in str(x["email"]).lower():
x["data_id"]=i
tmp_data.append(x)
i = i+1
final_data = tmp_data
else:
final_data = data
final_data = json.dumps(final_data) final_data = json.dumps(final_data)
elif len(param)>0:
elif param == "stats": if param == "email":
data_logs = get_logs() groups = []
data = json.loads(data_logs) for x in data:
if str("@").lower() in str(x["email"]).lower():
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})
sum_data = count_clients(data) final_data = json.dumps(groups)
connected_data = count_connection(data)
traffic_data = len(data)
percentage_data = count_percentage(data) if param == "orgs":
broken_data = count_broken(data)
traffic_data = count_traffic(data) groups = []
if traffic_data > 1000000000: for x in data:
traffic_data="{:.2f}".format(traffic_data/1000000000)+" GB" if str("-").lower() in str(x["name"]).lower():
else: tmp_val = x["name"]
traffic_data="{:.2f}".format(traffic_data/1000000)+" MB" tmp_val = str(tmp_val).split("-")[0]
tmp_val = tmp_val.upper()
data = {} if {"orgs": tmp_val} not in groups:
print("-> ",tmp_val)
groups.append({"orgs": tmp_val})
data["clienttotal"]=sum_data final_data = json.dumps(groups)
data["traffictotal"]=traffic_data
data["connectedtotal"]=connected_data if param == "stats":
data["percentagetotal"]=percentage_data data_logs = get_logs()
data["brokentotal"]=broken_data data = json.loads(data_logs)
sum_data = count_clients(data)
connected_data = count_connection(data)
traffic_data = len(data)
percentage_data = count_percentage(data)
broken_data = count_broken(data)
traffic_data = count_traffic(data)
if traffic_data > 1000000000:
traffic_data="{:.2f}".format(traffic_data/1000000000)+" GB"
else:
traffic_data="{:.2f}".format(traffic_data/1000000)+" MB"
data = {}
data["clienttotal"]=sum_data
data["traffictotal"]=traffic_data
data["connectedtotal"]=connected_data
data["percentagetotal"]=percentage_data
data["brokentotal"]=broken_data
final_data = json.dumps(data)
final_data = json.dumps(data)
else : else :
final_data = json.dumps(data) final_data = json.dumps(data)
# print(request.GET.get('param', '')) # print(request.GET.get('param', ''))
return HttpResponse(final_data, content_type='application') return HttpResponse(final_data, content_type='application')
def wgEditClients(request):
id = request.GET.get('id', '')
state = request.GET.get('state', '')
print("Edit Request")
if state and id:
print("[ Edit ]=======")
print(id)
print(state)
req_edit_state(id, state)
final_data = json.dumps({"state":"Success"})
return HttpResponse(final_data, content_type='application')

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -25,7 +25,7 @@ SECRET_KEY = 'django-insecure-n(+d-xz5933rr363)v31jcdl^+#ab23&s317uxek+d*-_d6q99
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = True
ALLOWED_HOSTS = ["192.168.10.2", "127.0.0.1", "localhost"] ALLOWED_HOSTS = ["192.168.10.2", "127.0.0.1", "localhost:8000", "localhost:5000"]
# Application definition # Application definition

View File

@ -25,5 +25,6 @@ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('api/', include(router.urls)), path('api/', include(router.urls)),
path('api2/wgLogs', views.wgViewLogs), path('api2/wgLogs', views.wgViewLogs),
path('api2/wgClients', views.wgViewClients) path('api2/wgClients', views.wgViewClients),
path('api2/wgEdit', views.wgEditClients)
] ]