147 lines
4.6 KiB
Python
147 lines
4.6 KiB
Python
import socket, hashlib, time
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
s.settimeout(10)
|
|
s.connect(('192.168.100.2', 8728))
|
|
|
|
def el(l):
|
|
if l<0x80: return bytes([l])
|
|
elif l<0x4000: l|=0x8000; return bytes([(l>>8)&0xFF,l&0xFF])
|
|
elif l<0x200000: l|=0xC00000; return bytes([(l>>16)&0xFF,(l>>8)&0xFF,l&0xFF])
|
|
elif l<0x10000000: l|=0xE0000000; return bytes([(l>>24)&0xFF,(l>>16)&0xFF,(l>>8)&0xFF,l&0xFF])
|
|
else: return bytes([0xF0,(l>>24)&0xFF,(l>>16)&0xFF,(l>>8)&0xFF,l&0xFF])
|
|
def ww(w): s.sendall(el(len(w))+(w.encode() if isinstance(w,str) else w))
|
|
def ws(c,*a): ww(c); [ww(x) for x in a]; s.sendall(b'\x00')
|
|
def rb(): return s.recv(1)
|
|
def rw():
|
|
f=rb()
|
|
if f==b'\x00': return None
|
|
lb=f[0]
|
|
if lb&0x80:
|
|
if (lb&0xC0)==0x80: l=((lb&0x3F)<<8)+rb()[0]
|
|
elif (lb&0xE0)==0xC0: l=((lb&0x1F)<<16)+(rb()[0]<<8)+rb()[0]
|
|
elif (lb&0xF0)==0xE0: l=((lb&0x0F)<<24)+(rb()[0]<<16)+(rb()[0]<<8)+rb()[0]
|
|
else: l=(rb()[0]<<24)+(rb()[0]<<16)+(rb()[0]<<8)+rb()[0]
|
|
else: l=lb
|
|
d=b''
|
|
while len(d)<l:
|
|
c=s.recv(l-len(d))
|
|
if not c: break
|
|
d+=c
|
|
return d.decode('utf-8',errors='replace')
|
|
def rss():
|
|
ws=[]
|
|
while True:
|
|
w=rw()
|
|
if w is None: break
|
|
ws.append(w)
|
|
return ws
|
|
def rr():
|
|
rs=[]
|
|
while True:
|
|
sent=rss()
|
|
if not sent: break
|
|
r=sent[0]; t=sent[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(c,**a):
|
|
w=[c]
|
|
for k,v in a.items():
|
|
if k.startswith('?'): w.append(f'{k}={v}')
|
|
else: w.append(f'={k}={v}')
|
|
ws(*w)
|
|
return rr()
|
|
|
|
ws('/login','=name=mi4','=password=m14')
|
|
r=rr()
|
|
if any(isinstance(x,tuple) and x[0]=='!trap' 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:
|
|
resp='00'+hashlib.md5(b'\x00'+b'm14'+bytes.fromhex(ch)).hexdigest()
|
|
ws('/login','=name=mi4',f'=response={resp}')
|
|
rr()
|
|
|
|
# Get router time
|
|
rt=cmd('/system/clock/print')[0]
|
|
date_str=rt['date']
|
|
time_str=rt['time']
|
|
print(f"Router time: {date_str} {time_str}")
|
|
|
|
# Parse time, add 1 minute
|
|
h=int(time_str[0:2]); m=int(time_str[3:5]); s=int(time_str[6:8])
|
|
m+=1
|
|
if m>=60: m=0; h+=1
|
|
if h>=24: h=0
|
|
exp_time=f"{h:02d}:{m:02d}:00"
|
|
exp_comment=f"{date_str} {exp_time}"
|
|
print(f"Expiry time (1 menit): {exp_comment}")
|
|
|
|
# Remove old labibah_tes if exists
|
|
for u in cmd('/ip/hotspot/user/print',**{'?name':'labibah_tes'}):
|
|
cmd('/ip/hotspot/user/remove',**{'.id':u['.id']})
|
|
print("Removed old labibah_tes")
|
|
|
|
# Create new user - active for 1 minute
|
|
cmd('/ip/hotspot/user/add',**{
|
|
'name':'labibah_tes',
|
|
'password':'labibah_tes',
|
|
'profile':'Bulanan2',
|
|
'mac-address':'CA:1A:02:C2:8C:1E',
|
|
'comment':exp_comment,
|
|
'limit-uptime':''
|
|
})
|
|
print(f"\nCreated user: labibah_tes / labibah_tes")
|
|
print(f" Expiry comment: {exp_comment}")
|
|
print(f" Status: AKTIF (belum expired - bisa login sekarang)")
|
|
print(f" Prediksi: setelah jam {exp_time}, scheduler akan expire user ini")
|
|
print(f" (Bulanan2 next-run sekitar ~2-3 menit sekali)\n")
|
|
|
|
# Find Bulanan2 next run
|
|
for sitem in cmd('/system/scheduler/print'):
|
|
if sitem['name']=='Bulanan2':
|
|
print(f"Bulanan2 next-run: {sitem.get('next-run')}")
|
|
|
|
# Wait and monitor
|
|
print(f"\nMemantau setiap 10 detik selama 180 detik...")
|
|
for i in range(180):
|
|
time.sleep(1)
|
|
if i%10==0:
|
|
for u in cmd('/ip/hotspot/user/print',**{'?name':'labibah_tes'}):
|
|
lu=u.get('limit-uptime','(none)')
|
|
co=u.get('comment','')
|
|
active_found=False
|
|
for a in cmd('/ip/hotspot/active/print',**{'?user':'labibah_tes'}):
|
|
active_found=True
|
|
status="TERSAMBUNG" if active_found else "offline"
|
|
print(f" [{i:3d}s] uptime={lu} status={status}")
|
|
break
|
|
else:
|
|
print(f" [{i:3d}s] User REMOVED!")
|
|
break
|
|
|
|
# Final
|
|
print("\n=== HASIL AKHIR ===")
|
|
for u in cmd('/ip/hotspot/user/print',**{'?name':'labibah_tes'}):
|
|
print(f" User: {u['name']}")
|
|
print(f" Comment: {u.get('comment')}")
|
|
print(f" limit-uptime: {u.get('limit-uptime','(none)')}")
|
|
if u.get('limit-uptime')=='1s': print(" => ✅ EXPIRED!")
|
|
elif u.get('limit-uptime')=='(none)': print(" => Masih AKTIF")
|
|
cmd('/ip/hotspot/user/remove',**{'.id':u['.id']})
|
|
print(" Cleaned up")
|
|
break
|
|
else:
|
|
print(" User sudah dihapus oleh scheduler ✅")
|
|
|
|
s.close()
|