使用中文日志 调整一些业务逻辑
This commit is contained in:
@@ -9,6 +9,8 @@ import time
|
||||
|
||||
|
||||
import prot_codec as pc
|
||||
from log import myprint
|
||||
from log import log_init
|
||||
|
||||
|
||||
|
||||
@@ -30,26 +32,10 @@ _local_client=[]
|
||||
|
||||
# LOCAL_SERVER_IP = ("192.168.3.166",80)
|
||||
# LOCAL_SERVER_IP = ("192.168.3.167",22)
|
||||
LOCAL_SERVER_IP = ("10.0.24.251",80)
|
||||
LOCAL_SERVER_IP = ("192.168.3.174",22)
|
||||
|
||||
|
||||
|
||||
_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)
|
||||
|
||||
|
||||
|
||||
@@ -59,15 +45,15 @@ 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)
|
||||
break
|
||||
|
||||
# 关闭指定地址的端口
|
||||
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)
|
||||
@@ -75,11 +61,12 @@ def close(ip,port):
|
||||
|
||||
# 关闭所有
|
||||
def close_all():
|
||||
global _local_client
|
||||
for item in _local_client:
|
||||
item[0].close()
|
||||
# 关闭端口之后把列表置空
|
||||
_local_client=[]
|
||||
myprint('remote close all')
|
||||
myprint('连接列表已清空')
|
||||
|
||||
|
||||
|
||||
@@ -94,24 +81,28 @@ def local_client_handler(tcp_server:socket,ip,port):
|
||||
try:
|
||||
recv = tcp_server.recv(4096)
|
||||
except Exception as e:
|
||||
myprint("local:",str(e))
|
||||
myprint("本地连接异常",str(e))
|
||||
break
|
||||
if recv:
|
||||
cmd={'device':'server','option':'data','ip':ip,'port':port}
|
||||
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 {ip},{port}")
|
||||
myprint(f"发送数据到客户端 {ip}:{port}")
|
||||
|
||||
else:
|
||||
break
|
||||
tcp_server.close()
|
||||
_local_client.remove(self_info)
|
||||
close(ip,port)
|
||||
# 发送连接断开的消息
|
||||
cmd={'device':'server','option':'disconnect','ip':ip,'port':port}
|
||||
data=pc.encode(json.dumps(cmd).encode('utf-8'),b'default')
|
||||
if _remote_client is not None:
|
||||
_remote_client.send(data)
|
||||
try:
|
||||
myprint(f"发送断开通知到客户端 {ip}:{port}")
|
||||
_remote_client.send(data)
|
||||
except Exception as e:
|
||||
myprint("远端连接异常",str(e))
|
||||
|
||||
|
||||
|
||||
@@ -144,16 +135,20 @@ def remote_client_handler(tcp_client_1:socket):
|
||||
j=json.loads(cmd)
|
||||
if(j['device']=='client'):
|
||||
if(j['option']=='connect'):
|
||||
myprint("收到客户端的连接通知")
|
||||
temp = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
temp.connect(LOCAL_SERVER_IP)
|
||||
thd = threading.Thread(target = local_client_handler, args = (temp,j['ip'],j['port']))
|
||||
thd.start()
|
||||
elif(j['option']=='disconnect'):
|
||||
myprint(f"收到客户端的断开通知 {j['ip']}:{j['port']}")
|
||||
close(j['ip'],j['port'])
|
||||
elif(j['option']=='data'):
|
||||
myprint(f"收到数据 {j['ip']}:{j['port']}")
|
||||
send_to(j['ip'],j['port'],data)
|
||||
elif(j['device']=='proxy'):
|
||||
if(j['option']=='close'):
|
||||
myprint("收到代理服务器的断开通知")
|
||||
close_all()
|
||||
except Exception as e:
|
||||
myprint(str(e))
|
||||
@@ -161,7 +156,7 @@ def remote_client_handler(tcp_client_1:socket):
|
||||
else:
|
||||
break
|
||||
tcp_client_1.close()
|
||||
myprint("proxy close.")
|
||||
myprint("与代理服务器的连接已断开")
|
||||
close_all()
|
||||
|
||||
|
||||
@@ -182,6 +177,6 @@ def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_log_fp=open("target_server.log",mode="w+",encoding="utf-8")
|
||||
log_init("target_server.log")
|
||||
main()
|
||||
|
||||
|
Reference in New Issue
Block a user