init: workspace opencode sync
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import socket, hashlib
|
||||
|
||||
class R:
|
||||
def __init__(self,h,p=8728): self.h=h; self.p=p; self.s=None
|
||||
def c(self,u,p):
|
||||
self.s=socket.socket(socket.AF_INET,socket.SOCK_STREAM); self.s.settimeout(15)
|
||||
self.s.connect((self.h,self.p))
|
||||
self._ws('/login','=name='+u,'=password='+p)
|
||||
r=self._rr()
|
||||
if any(isinstance(x,tuple) for x in r):
|
||||
for x in r:
|
||||
if isinstance(x,dict) and 'ret' in x: ch=x['ret']
|
||||
if ch and len(ch)==32:
|
||||
h=hashlib.md5(b'\x00'+p.encode()+bytes.fromhex(ch)).hexdigest()
|
||||
self._ws('/login','=name='+u,'=response=00'+h)
|
||||
self._rr()
|
||||
return self
|
||||
def _el(self,l):
|
||||
if l<0x80: return bytes([l])
|
||||
elif l<0x4000: l|=0x8000; return bytes([(l>>8)&0xFF,l&0xFF])
|
||||
else: return bytes([0xC0|((l>>16)&0x0F),(l>>8)&0xFF,l&0xFF]) if l<0x200000 else bytes([0xE0|((l>>24)&0x07),(l>>16)&0xFF,(l>>8)&0xFF,l&0xFF])
|
||||
def _ww(self,w): self.s.sendall(self._el(len(w))+w.encode('utf-8'))
|
||||
def _ws(self,c,*ws): self._ww(c); [self._ww(w) for w in ws]; self.s.sendall(b'\x00')
|
||||
def _rw(self):
|
||||
f=self._rb()
|
||||
if f==b'\x00': return None
|
||||
lb=f[0]; l=lb
|
||||
if lb&0x80:
|
||||
if (lb&0xC0)==0x80: l=((lb&0x3F)<<8)+self._rb()[0]
|
||||
elif (lb&0xE0)==0xC0: l=((lb&0x1F)<<16)+(self._rb()[0]<<8)+self._rb()[0]
|
||||
else: l=((lb&0x0F)<<24)+(self._rb()[0]<<16)+(self._rb()[0]<<8)+self._rb()[0]
|
||||
d=b''
|
||||
while len(d)<l: d+=self._rb()
|
||||
return d.decode('utf-8',errors='replace')
|
||||
def _rb(self): return self.s.recv(1)
|
||||
def _rss(self):
|
||||
ws=[]
|
||||
while True:
|
||||
w=self._rw()
|
||||
if w is None: break
|
||||
ws.append(w)
|
||||
return ws
|
||||
def _rr(self):
|
||||
rs=[]
|
||||
while True:
|
||||
s=self._rss()
|
||||
if not s: break
|
||||
r=s[0]; t=s[1:]
|
||||
if r=='!done': break
|
||||
elif r=='!re':
|
||||
d={}
|
||||
for w in t:
|
||||
if '=' in w:
|
||||
p=w.split('=',2)
|
||||
d[p[1]]=p[2] if len(p)>=3 else ''
|
||||
else: d[w]=''
|
||||
rs.append(d)
|
||||
elif r=='!trap': rs.append(('!trap',t))
|
||||
return rs
|
||||
def cmd(self,c,**a):
|
||||
ws=[c]
|
||||
for k,v in a.items(): ws.append(('?' if k.startswith('?') else '=')+k+'='+v)
|
||||
self._ws(*ws)
|
||||
return self._rr()
|
||||
def close(self):
|
||||
if self.s: self.s.close(); self.s=None
|
||||
|
||||
api=R('192.168.100.2').c('mi4','m14')
|
||||
print("=== All hotspot users ===")
|
||||
for u in api.cmd('/ip/hotspot/user/print'):
|
||||
name = u.get('name','')
|
||||
if 'tg_test' in name or 'test' in name:
|
||||
print(" %s | %s | profile=%s | uptime=%s | comment=%s" % (u.get('.id',''), name, u.get('profile',''), u.get('limit-uptime',''), u.get('comment','')[:40] if u.get('comment') else '-'))
|
||||
print("---")
|
||||
for u in api.cmd('/ip/hotspot/user/print'):
|
||||
name = u.get('name','')
|
||||
prof = u.get('profile','')
|
||||
if prof == 'Family':
|
||||
print(" %s | %s | uptime=%s | limit-bytes=%s | comment=%s" % (u.get('.id',''), name, u.get('limit-uptime',''), u.get('limit-bytes-total',''), u.get('comment','')[:30] if u.get('comment') else '-'))
|
||||
api.close()
|
||||
Reference in New Issue
Block a user