@@ -13,7 +13,7 @@ class WebSocketClient:
1313 send_alarm_ip_list = []
1414 pool = ThreadPool (processes = 2 )
1515
16- def __init__ (self , server_ip : str , server_port : int , sk : str , client_type : str , enable_cidr = False , block_ip = None , unblock_ip = None , get_all_block_ip = None ):
16+ def __init__ (self , server_ip : str , server_port : int , sk : str , client_type : str , enable_cidr = False , block_ip = None , unblock_ip = None , get_all_block_ip = None , login_success_callback = None ):
1717 self .server_ip = server_ip
1818 self .server_port = server_port
1919 self .sk = sk
@@ -29,6 +29,7 @@ def __init__(self, server_ip: str, server_port: int, sk: str, client_type: str,
2929 self .unblock_ip = unblock_ip
3030 self .get_all_block_ip = get_all_block_ip
3131 self .client_type = client_type
32+ self .login_success_callback = login_success_callback
3233 self .ws = websocket .WebSocketApp (
3334 "ws://" + server_ip + ":" + str (server_port ) + "/device" ,
3435 on_message = self .on_message ,
@@ -60,6 +61,8 @@ def on_message(self, w, message):
6061 if message ["method" ] == "login" :
6162 self .is_login = True
6263 util .print ("[+] 登录成功,设备名称: " + message ["data" ]["deviceName" ])
64+ if self .login_success_callback is not None :
65+ self .login_success_callback ()
6366 if self .client_type == "block" :
6467 if message ["method" ] == "blockCidr" :
6568 util .print ("[+] 封禁IP: " + message ["data" ]["cidr" ])
@@ -124,9 +127,9 @@ def connect(self):
124127 return
125128 self .pool .apply_async (self .web_socket_d )
126129
127- def send_alarm (self , ip : str , attackAsset : str , attackMethod : str ):
128- if self .client_type == "block " :
129- util .print ("[-] 封禁模块无法发送告警数据 " )
130+ def send_alarm (self , ip : str , attack_asset : str , attack_method : str ):
131+ if self .client_type != "alarm " :
132+ util .print ("[-] 非告警模块无法发送告警数据 " )
130133 return
131134 if not self .is_login :
132135 util .print ("[-] 未登录成功,无法发送数据" )
@@ -140,12 +143,12 @@ def send_alarm(self, ip: str, attackAsset: str, attackMethod: str):
140143 "method" : "alarmIp" ,
141144 "data" : {
142145 "ip" : ip ,
143- "attackAsset" : attackAsset ,
144- "attackMethod" : attackMethod
146+ "attackAsset" : attack_asset ,
147+ "attackMethod" : attack_method
145148 }
146149 }
147150 iv = util .random_bytes ()
148- util .print ("[+] 发送告警IP: " + ip + "->" + attackAsset + "\t " + attackMethod )
151+ util .print ("[+] 发送告警IP: " + ip + "->" + attack_asset + "\t " + attack_method )
149152 self .ws .send (iv + util .aes_cfb_encrypt (self .sk [3 :].encode (), iv , json .dumps (send_data ).encode ()))
150153 def send_notify (self , title : str , content : str ):
151154 if not self .is_login :
@@ -159,4 +162,16 @@ def send_notify(self, title: str, content: str):
159162 }
160163 }
161164 iv = util .random_bytes ()
165+ self .ws .send (iv + util .aes_cfb_encrypt (self .sk [3 :].encode (), iv , json .dumps (send_data ).encode ()))
166+ def send_sync (self ):
167+ if self .client_type != "block" :
168+ util .print ("[-] 非封禁模块无法请求封禁IP" )
169+ return
170+ if not self .is_login :
171+ util .print ("[-] 未登录成功,无法发送数据" )
172+ return
173+ send_data = {
174+ "method" : "syncBlockIp"
175+ }
176+ iv = util .random_bytes ()
162177 self .ws .send (iv + util .aes_cfb_encrypt (self .sk [3 :].encode (), iv , json .dumps (send_data ).encode ()))
0 commit comments