-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
188 lines (149 loc) · 5.7 KB
/
api.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
import json
import urllib
import base64
import logging
import requests
from config import ROUTER_IP, ROUTER_PASS, TOTAL_BW, REQ_COOKIE
from utils import get_ip_address
from utils import get_suffix_for_ip
logging.basicConfig(filename='app.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')
proxies = urllib.request.getproxies()
session = requests.Session()
try:
salt = get_suffix_for_ip(get_ip_address())
logging.warning(salt)
except Exception as e:
salt = None
logging.warning(e)
_DEVICES = [] # donot edit! used as memo internaly
def _ask_modem_something(modem_address, modem_password, data, api_path):
if not modem_address.startswith('http'):
modem_address = 'http://' + modem_address.strip('/')
# modem_address = modem_address.strip('/')
api_path = api_path.lstrip('/')
ecos_pwd = base64.b64encode(modem_password.encode('utf-8')).decode()
if REQ_COOKIE == None:
cookies = {
# 'Authorization': f'Basic {pswd.decode()}'
# 'Authorization': 'Basic YWRtaW46YWRtaW4='
# for lan connection mji language=en; ecos_pw=YWRtaW4=5gk:language=en
"Authorization": f"language=en; ecos_pw={ecos_pwd}{salt}:language=en"
}
else:
cookies = {
# 'Authorization': f'Basic {pswd.decode()}'
# 'Authorization': 'Basic YWRtaW46YWRtaW4='
# for lan connection mji language=en; ecos_pw=YWRtaW4=5gk:language=en
"Authorization": REQ_COOKIE
}
# logging.warning(cookies)
# f"http://{ROUTER_IP}/goform/updateIptAccount"
# referer: http://192.168.0.1/sys_iptAccount.asp
headers = {
'Referer': '{}/'.format(modem_address)
}
global session
try:
r = session.post('{}/{}'.format(modem_address, api_path),
headers=headers, cookies=cookies, data=data)
except:
logging.info("using proxy")
r = session.post('{}/{}'.format(modem_address, api_path),
headers=headers, cookies=cookies, data=data, proxies=proxies)
# logging.debug(r.content)
return r.content
def _getTrafficStats(api_path="goform/updateIptAccount", data="something"):
resp = _ask_modem_something(ROUTER_IP, ROUTER_PASS, data, api_path)
return resp.decode()
def getTrafficStatsJson(api_path="goform/updateIptAccount", data="something"):
resp = _getTrafficStats(api_path, data)
jsonResponse = []
try:
splitLines = resp.split('\n')
for line in splitLines:
ip, upKB, downKB, sentMsg, sentMB, recievedMsg, recievedMB = line.split(
';')
jsonResponse.append({
"ip": ip,
"upKB": float(upKB),
"downKB": float(downKB),
"sentMsg": float(sentMsg),
"sentMB": float(sentMB),
"recievedMsg": float(recievedMsg),
"recievedMB": float(recievedMB)})
except Exception as e:
logging.info(e)
return jsonResponse
def _getDhcpList(api_path="lan_dhcp_clients.asp"):
h = _getTrafficStats(api_path)
start = 'dhcpList=new Array('
startPtr = h.find(start)
_dhcplist = h[startPtr + len(start): h.find(")", startPtr)]
# print(_dhcplist)
return _dhcplist
def getDhcpListJson(api_path="lan_dhcp_clients.asp"):
devices = []
jsonResponse = []
for device in _getDhcpList().split(','):
devices.append(device[1:-1])
# name , ip , mac , lease time
for device in devices:
try:
name, ip, mac, jank, leaseTime = device.split(';')
jsonResponse.append({
"name": name,
"ip": ip,
"mac": mac,
"jank": jank,
"leaseTime": float(leaseTime)
})
except Exception as e:
logging.debug(e)
return jsonResponse
def getDeviceParamFromIp(ip, param):
global _DEVICES
if _DEVICES == []:
_DEVICES = getDhcpListJson()
for device in _DEVICES:
if(device["ip"] == ip):
return device[param]
_DEVICES = []
def getAllStatsJson():
devices = getTrafficStatsJson("goform/updateIptAccount")
jsonResponse = {}
devicesArr = []
totalDownKB = 0
totalUpKB = 0
totalRecievedMB = 0
totalSentMB = 0
for device in devices:
deviceIP = device["ip"]
_name = getDeviceParamFromIp(deviceIP, "name")
if _name == "" or _name == None:
device["name"] = getDeviceParamFromIp(deviceIP, "mac")
else:
device["name"] = _name
device["mac"] = getDeviceParamFromIp(deviceIP, "mac")
device["jank"] = getDeviceParamFromIp(deviceIP, "jank")
device["leaseTime"] = getDeviceParamFromIp(deviceIP, "leaseTime")
device["totalSpeed"] = device["upKB"] + device["downKB"]
device["totalUsedMB"] = round(device["sentMB"] + device["recievedMB"], 2)
devicesArr.append(device)
totalDownKB += device["downKB"]
totalUpKB += device["upKB"]
totalSentMB += device["sentMB"]
totalRecievedMB += device["recievedMB"]
# calculated params
metrics = {}
totalSpeed = totalDownKB + totalUpKB
totalUsedGB = (totalSentMB + totalRecievedMB)/1024
metrics["totalDownKB"] = round(totalDownKB, 2)
metrics["totalUpKB"] = round(totalUpKB, 2)
metrics["totalSpeed"] = round(totalSpeed, 2)
metrics["totalSentMB"] = round(totalSentMB, 2)
metrics["totalRecievedMB"] = round(totalRecievedMB, 2)
metrics["load"] = round((totalSpeed)/(TOTAL_BW/100), 2)
metrics["totalUsedGB"] = round(totalUsedGB, 2)
jsonResponse["devices"] = devicesArr
jsonResponse["metrics"] = metrics
return jsonResponse