wg-backend-django/wg_api/utils/reqs_handler.py

163 lines
4.1 KiB
Python
Raw Normal View History

import json
import requests
def get_session():
2023-10-30 03:40:43 -04:00
login_data = {'username':"infidel","password":"roadsidepicnic", "rememberMe":False}
inf_s = requests.Session()
2023-10-30 03:40:43 -04:00
response = inf_s.post('http://10.30.1.106:5000/login', json=login_data)
print("Session")
print(inf_s.cookies.get_dict())
print(response.json())
print("-"*20)
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)
2023-10-30 03:40:43 -04:00
req = inf_s.post('http://10.30.1.106:5000/client/set-status', req_val)
print(req)
2022-11-30 03:58:16 -05:00
def req_edit_user(id):
inf_s = get_session()
req_val = id
2023-10-30 03:40:43 -04:00
req_url = 'http://10.30.1.106:5000/api/client/'+req_val
2022-11-30 03:58:16 -05:00
req = inf_s.get(req_url)
print("-->")
print(req)
# req = json.loads(req)
return req.json()
def req_apply_sys():
inf_s = get_session()
2023-10-30 03:40:43 -04:00
# http://10.30.1.106:5000/api/apply-wg-config?_=1669872830286
req_url = 'http://10.30.1.106:5000/api/apply-wg-config'
req = inf_s.get(req_url)
print("-->")
print(req)
return req
2022-11-30 03:58:16 -05:00
#
2023-10-30 03:40:43 -04:00
# http://10.30.1.106:5000/api/client/caivc1vekn84im2m6ec0?_=1669623393468
def req_ip_recommendation():
# Write your own ip recommendation next time
2023-10-30 03:40:43 -04:00
# http://10.30.1.106:5000/api/suggest-client-ips
inf_s = get_session()
2023-10-30 03:40:43 -04:00
req_url = 'http://10.30.1.106:5000/api/suggest-client-ips'
req = inf_s.get(req_url)
req = req.json()
2023-10-30 03:40:43 -04:00
print(len(req))
2023-10-30 03:40:43 -04:00
if len(req) > 1:
print(req[0])
req = [req[0]]
2023-10-30 03:40:43 -04:00
#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
2023-10-30 03:40:43 -04:00
#tmp_array = []
#for x in req:
# print(x)
# tmp_array.append(x)
# i = i+1
2022-12-16 13:45:33 -05:00
2023-10-30 03:40:43 -04:00
#req_dump["allocated_ips"] = tmp_array
print("Allocated IP")
print(req)
#print(req_dump)
2022-12-16 13:45:33 -05:00
2023-10-30 03:40:43 -04:00
return req
def req_del_user(id):
2022-12-16 13:45:33 -05:00
inf_s = get_session()
2023-10-30 03:40:43 -04:00
req_url = 'http://10.30.1.106:5000/remove-client'
data = {"id":id}
print(data)
2022-12-16 13:45:33 -05:00
2023-10-30 03:40:43 -04:00
try:
req = inf_s.post(req_url, json=data)
print(req.json())
except AssertionError as msg:
print(msg)
def req_get_client(id):
inf_s = get_session()
req_url = 'http://10.30.1.106:5000/api/client/'+id
2022-12-16 13:45:33 -05:00
2023-10-30 03:40:43 -04:00
req = inf_s.get(req_url)
return req.json()
2022-12-16 13:45:33 -05:00
2023-10-30 03:40:43 -04:00
def reg_bulk(data):
inf_s = get_session()
req_url = 'http://10.30.1.106:5000/new-client'
for x in data:
#print(x)
if len(x) > 1:
allocated_ip = req_ip_recommendation()
uname = x[0]
email = x[1]
code = x[2]
org = x[3]
#data = {"name":uname,"email":email,"code":code,"org":org}
data = {
"name":code+"-"+uname,
"email":email,
#"allocated_ips":["192.168.100.67/32","192.168.101.7/32"],
"allocated_ips":allocated_ip,
"allowed_ips":["10.30.1.0/24"],
"extra_allowed_ips":[],
"use_server_dns":False,
"enabled":True,
"public_key":"",
"preshared_key":""
}
# data = {"name":uname,"email":email,"allocated_ips":suggest_ips,"allowed_ips":allowed_ips,"extra_allowed_ips":[],"use_server_dns":true,"enabled":true,"preshared_key":true}
2022-12-16 13:45:33 -05:00
2023-10-30 03:40:43 -04:00
req = inf_s.post(req_url, json=data)
print(json.dumps(data, indent=2))
2022-12-16 13:45:33 -05:00
2023-10-30 03:40:43 -04:00
# ret_reg=$(curl -s --location --request POST 'http://10.30.1.106:5000/new-client' \
2022-12-16 13:45:33 -05:00
# --header 'Content-Type: application/json' \
# --cookie .tmp-cookie \
# --data-raw '{"name":"'$uname'","email":"'$email'","allocated_ips":'$suggest_ips',"allowed_ips":["10.0.0.0/8", "192.168.100.0/24"],"extra_allowed_ips":[],"use_server_dns":true,"enabled":true,"preshared_key":true}' )