-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiKuai.py
211 lines (194 loc) · 7.88 KB
/
iKuai.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
import requests
import json
import asyncio
from datetime import datetime
from os.path import exists
class iKuaiHelper:
def __init__(self):
self.config = {
"refreshRate": 5, # seconds
"maxOnlineTime": 3000, # 1 hour
"maxOfflineTime": 300, # 5 min
"maxIdleTime": 300, # 5 min
"maxBlockedTime": 600, # 10 min
"onlineThreshold": 1024, # 1KBps
"filter": "192.168.101",
}
self.devices = [] # device status
self.headers = {
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.9',
'Connection': 'keep-alive',
'Content-Type': 'application/json;charset=UTF-8',
# 'Cookie': cookies,
'Origin': 'http://192.168.2.1',
'Referer': 'http://192.168.2.1/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.50',
}
self.login()
self.setIgnoredDevices() # self.ignoredDevices
self.acl_mac("clear") # clear old records
def login(self, relogin = False):
cookies = None
if not relogin and exists("cookies.txt"):
with open("cookies.txt", "r") as file:
self.log("Load cookies...")
cookies = file.read()
file.close()
if not relogin and cookies:
self.headers['Cookie'] = cookies
return
# Perform login
with open("login.json") as file:
self.log("Load login information ...")
json_data = json.load(file)
file.close
response = requests.post('http://192.168.2.1/Action/login', headers=self.headers, json=json_data, verify=False)
data = response.headers
data = data.get('Set-Cookie')
data = data.split(";")[0]
cookies = f"{data}; username=switch; login=1"
with open("cookies.txt", "w") as file:
self.log("Save cookies...")
file.write(cookies)
file.close()
self.headers['Cookie'] = cookies
return
def getOnlineDevices(self):
json_data = {
'func_name': 'monitor_lanip',
'action': 'show',
'param': {
'TYPE': 'data,total',
'ORDER_BY': 'ip_addr_int',
'orderType': 'IP',
'limit': '0,100',
'ORDER': '',
'FINDS': 'ip_addr,mac,comment,username',
'KEYWORDS': self.config['filter'],
},
}
response = requests.post('http://192.168.2.1/Action/call', headers=self.headers, json=json_data, verify=False)
json_data = json.loads(response.text)
json_data = json_data.get('Data').get('data')
return json_data
def acl_mac(self, action, mac = None):
if action == "add":
json_data = {
'func_name': 'acl_mac',
'action': 'add',
'param': {
'enabled': 'yes',
'mac': mac,
},
}
response = requests.post('http://192.168.2.1/Action/call', headers=self.headers, json=json_data, verify=False)
elif action == "del":
json_data = {
'func_name': 'acl_mac',
'action': 'del',
'param': {
'mac': mac,
},
}
response = requests.post('http://192.168.2.1/Action/call', headers=self.headers, json=json_data, verify=False)
pass
elif action == "clear":
json_data = {
'func_name': 'acl_mac',
'action': 'show',
'param': {
'TYPE': 'total,data',
'limit': '0,50',
'ORDER_BY': '',
'ORDER': '',
},
}
response = requests.post('http://192.168.2.1/Action/call', headers=self.headers, json=json_data, verify=False)
response = json.loads(response.text)
devices = response.get('Data').get('data')
for device in devices:
if not device.get('comment'):
self.acl_mac("del", device['mac'])
def getDevicesWithComments(self):
json_data = {
'func_name': 'mac_comment',
'action': 'show',
'param': {
'TYPE': 'total,data',
'limit': '0,50',
'ORDER_BY': '',
'ORDER': '',
},
}
response = requests.post('http://192.168.2.1/Action/call', headers=self.headers, json=json_data, verify=False)
json_data = json.loads(response.text)
json_data = json_data.get('Data').get('data')
return json_data
def setIgnoredDevices(self):
self.ignoredDevices = []
devices = self.getDevicesWithComments()
for i,device in enumerate(devices):
self.ignoredDevices.append(device['mac'])
def updateDeviceStatus(self, mac, status, now):
device = next((device for device in self.devices if device['mac'] == mac), None)
if device is None:
device = {"mac":mac,"online":0,"idle":0,"blocked":0,"time":0}
self.devices.append(device)
if device['blocked']: # blocked device can download if acl ip is used
return
device['time'] = now
if status < self.config['onlineThreshold']: # not connected
device['idle'] = device['idle'] + self.config['refreshRate']
else: # connected and new connection
device['online'] = device['online'] + self.config['refreshRate'] + device['idle'] if device['online'] > 0 else self.config['refreshRate']
device['idle'] = 0
pass
def processOnlineDevices(self):
now = int(datetime.now().timestamp())
devices = self.getOnlineDevices()
for i, device in enumerate(devices):
if device['mac'] in self.ignoredDevices:
continue
self.updateDeviceStatus(device['mac'], device['download'], now)
for i, device in enumerate(self.devices):
if device['online'] >= self.config['maxOnlineTime']: # online over 1h
# block mac
device['online'] = 0
device['blocked'] = now
self.acl_mac("add", device['mac'])
self.log(f"block mac - {device['mac']}")
if device['idle'] >= self.config['maxIdleTime']: # idle over xx min
# reset online status
device['online'] = 0
device['idle'] = 0
self.log(f"idle reset - {device['mac']}")
if device['blocked'] and now - device['blocked'] >= self.config['maxBlockedTime']: # block over xx min
# unblock mac
device['blocked'] = 0
self.acl_mac("del", device['mac'])
self.log(f"unblock mac - {device['mac']}")
if device['time'] and now - device['time'] >= self.config['maxOfflineTime']: # offline
# reset online status
device['online'] = 0
device['idle'] = 0
device['time'] = 0
self.log(f"offline reset - {device['mac']}")
async def periodicTasks(self):
while True:
# process online devices
self.processOnlineDevices()
# wait
self.log(self.devices)
await asyncio.sleep(self.config['refreshRate'])
def log(self, msg):
now = datetime.now()
if isinstance(msg, list):
for item in msg:
print(f"{now} - {item}")
print(f"{now} - ## End ##")
else:
print(f"{now} - {msg}")
print(f"{now} - =========")
def on_exit(self):
self.acl_mac("clear")