使用中文日志 调整一些业务逻辑
This commit is contained in:
@@ -10,6 +10,8 @@ import time
|
||||
|
||||
|
||||
import prot_codec as pc
|
||||
from log import myprint
|
||||
from log import log_init
|
||||
|
||||
|
||||
|
||||
@@ -27,22 +29,6 @@ _local_server=None
|
||||
|
||||
|
||||
|
||||
_log_fp=None
|
||||
|
||||
def _time():
|
||||
return '['+time.strftime("%Y-%m-%d %H:%M:%S")+']'
|
||||
|
||||
def myprint_dec(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
# 在这里添加额外的功能
|
||||
# print(_time(), str(e),file=_log_fp)
|
||||
kwargs["file"]=_log_fp
|
||||
result = func(_time(),*args, **kwargs)
|
||||
_log_fp.flush()
|
||||
return result
|
||||
return wrapper
|
||||
|
||||
myprint=myprint_dec(print)
|
||||
|
||||
|
||||
|
||||
@@ -52,27 +38,28 @@ myprint=myprint_dec(print)
|
||||
def send_to(ip,port,data:bytearray):
|
||||
for item in _local_client:
|
||||
if(item[1]==ip and item[2]==port):
|
||||
myprint(f"recv from remote {ip},{port}")
|
||||
item[0].send(data)
|
||||
return
|
||||
myprint(f"can not fond {ip},{port}")
|
||||
myprint(f"can not fond {ip}:{port}")
|
||||
|
||||
|
||||
# 关闭指定地址的端口
|
||||
def close(ip,port):
|
||||
global _local_client
|
||||
for item in _local_client:
|
||||
if(item[1]==ip and item[2]==port):
|
||||
myprint(f'remote close {ip},{port}')
|
||||
myprint(f'断开连接 {ip}:{port}')
|
||||
item[0].close()
|
||||
_local_client.remove(item)
|
||||
break
|
||||
|
||||
# 关闭所有
|
||||
def close_all():
|
||||
global _local_client
|
||||
for item in _local_client:
|
||||
item[0].close()
|
||||
_local_client=[]
|
||||
myprint('remote close all')
|
||||
myprint('连接列表已清空')
|
||||
|
||||
|
||||
|
||||
@@ -92,7 +79,7 @@ def remote_client_handler(tcp_remote:socket):
|
||||
try:
|
||||
recv = tcp_remote.recv(4096)
|
||||
except Exception as e:
|
||||
myprint("remote:",str(e))
|
||||
myprint("代理服务器连接异常",str(e))
|
||||
break
|
||||
if recv:
|
||||
recv_data+=recv
|
||||
@@ -107,11 +94,14 @@ def remote_client_handler(tcp_remote:socket):
|
||||
j=json.loads(cmd)
|
||||
if(j['device']=='server'):
|
||||
if(j['option']=='data'):
|
||||
myprint(f"收到数据 {j['ip']}:{j['port']}")
|
||||
send_to(j['ip'],j['port'],data)
|
||||
elif(j['option']=='disconnect'):
|
||||
myprint(f"收到服务器的断开通知 {j['ip']}:{j['port']}")
|
||||
close(j['ip'],j['port'])
|
||||
elif(j['device']=='proxy'):
|
||||
if(j['option']=='close'):
|
||||
myprint("收到代理服务器的断开通知")
|
||||
close_all()
|
||||
except Exception as e:
|
||||
myprint(str(e))
|
||||
@@ -119,7 +109,7 @@ def remote_client_handler(tcp_remote:socket):
|
||||
else:
|
||||
break
|
||||
tcp_remote.close()
|
||||
myprint("proxy close")
|
||||
myprint("代理服务器已断开")
|
||||
close_all()
|
||||
if _local_server is not None:
|
||||
_local_server.close()
|
||||
@@ -148,18 +138,18 @@ def local_client_handler(tcp_server:socket,addr):
|
||||
data=pc.encode(json.dumps(cmd).encode('utf-8'),recv)
|
||||
if _remote_client is not None:
|
||||
_remote_client.send(data)
|
||||
myprint(f"send to remote {addr[0]},{addr[1]}")
|
||||
myprint(f"发送数据 {addr[0]}:{addr[1]}")
|
||||
else:
|
||||
break
|
||||
tcp_server.close()
|
||||
close(addr[0],addr[1])
|
||||
cmd={'device':'client','option':'disconnect','ip':addr[0],'port':addr[1]}
|
||||
data=pc.encode(json.dumps(cmd).encode('utf-8'),b'default')
|
||||
if(_remote_client is not None):
|
||||
cmd={'device':'client','option':'disconnect','ip':addr[0],'port':addr[1]}
|
||||
data=pc.encode(json.dumps(cmd).encode('utf-8'),b'default')
|
||||
try:
|
||||
myprint(f"发送断开通知到服务器端 {addr[0]}:{addr[1]}")
|
||||
_remote_client.send(data)
|
||||
except Exception as e:
|
||||
myprint("remote close",str(e))
|
||||
_local_client.remove(addr_info)
|
||||
myprint("远端连接异常",str(e))
|
||||
|
||||
|
||||
|
||||
@@ -188,6 +178,6 @@ def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_log_fp=open("target_client.log",mode="w+",encoding="utf-8")
|
||||
log_init("target_client.log")
|
||||
main()
|
||||
|
||||
|
Reference in New Issue
Block a user