Skip to content

Commit 1c53357

Browse files
committed
Add ban monitor helper
1 parent 4099456 commit 1c53357

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

keyauth.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ def __init__(self, name, ownerid, version, hash_to_check):
6060

6161
self.version = version
6262
self.hash_to_check = hash_to_check
63+
self._ban_monitor_stop = threading.Event()
64+
self._ban_monitor_thread = None
65+
self._ban_monitor_detected = False
6366
self.init()
6467

6568
sessionid = enckey = ""
@@ -432,8 +435,8 @@ def check(self):
432435
else:
433436
return False
434437

435-
def checkblacklist(self):
436-
self.checkinit()
438+
def checkblacklist(self):
439+
self.checkinit()
437440
hwid = others.get_hwid()
438441

439442
post_data = {
@@ -446,10 +449,50 @@ def checkblacklist(self):
446449
response = self.__do_request(post_data)
447450

448451
json = jsond.loads(response)
449-
if json["success"]:
450-
return True
451-
else:
452-
return False
452+
if json["success"]:
453+
return True
454+
else:
455+
return False
456+
457+
def start_ban_monitor(self, interval_seconds=120, check_session=True, on_ban=None):
458+
if interval_seconds < 1:
459+
interval_seconds = 1
460+
461+
if self._ban_monitor_thread and self._ban_monitor_thread.is_alive():
462+
return
463+
464+
self._ban_monitor_detected = False
465+
self._ban_monitor_stop.clear()
466+
467+
def _loop():
468+
while not self._ban_monitor_stop.is_set():
469+
if check_session and not self.check():
470+
self._ban_monitor_detected = True
471+
if on_ban:
472+
on_ban()
473+
return
474+
475+
if self.checkblacklist():
476+
self._ban_monitor_detected = True
477+
if on_ban:
478+
on_ban()
479+
return
480+
481+
self._ban_monitor_stop.wait(interval_seconds)
482+
483+
self._ban_monitor_thread = threading.Thread(target=_loop, daemon=True)
484+
self._ban_monitor_thread.start()
485+
486+
def stop_ban_monitor(self):
487+
self._ban_monitor_stop.set()
488+
if self._ban_monitor_thread and self._ban_monitor_thread.is_alive():
489+
self._ban_monitor_thread.join(timeout=2)
490+
491+
def ban_monitor_running(self):
492+
return self._ban_monitor_thread is not None and self._ban_monitor_thread.is_alive()
493+
494+
def ban_monitor_detected(self):
495+
return self._ban_monitor_detected
453496

454497
def log(self, message):
455498
self.checkinit()

0 commit comments

Comments
 (0)