使用中文日志 调整一些业务逻辑
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
|
*.log
|
||||||
|
28
log.py
Normal file
28
log.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 同一个进程中所有调用这个文件的 .py 文件都使用这个变量
|
||||||
|
_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)
|
||||||
|
print(_time(), *args, **kwargs)
|
||||||
|
kwargs["file"]=_log_fp
|
||||||
|
result = func(_time(),*args, **kwargs)
|
||||||
|
_log_fp.flush()
|
||||||
|
return result
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
myprint=myprint_dec(print)
|
||||||
|
|
||||||
|
def log_init(file_name:str):
|
||||||
|
global _log_fp
|
||||||
|
if _log_fp is None:
|
||||||
|
_log_fp=open(file_name,mode="w+",encoding="utf-8")
|
||||||
|
|
20
server.py
20
server.py
@@ -8,6 +8,8 @@ import time
|
|||||||
|
|
||||||
import prot_codec as pc
|
import prot_codec as pc
|
||||||
import target as tg
|
import target as tg
|
||||||
|
from log import myprint
|
||||||
|
from log import log_init
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -15,22 +17,6 @@ import target as tg
|
|||||||
|
|
||||||
SERVER_PORT = 5345
|
SERVER_PORT = 5345
|
||||||
|
|
||||||
_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)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -79,5 +65,5 @@ def start_service():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
_log_fp=open("proxy_server.log",mode="w+",encoding="utf-8")
|
log_init("proxy_service.log")
|
||||||
start_service()
|
start_service()
|
||||||
|
@@ -7,6 +7,8 @@ import socket
|
|||||||
|
|
||||||
|
|
||||||
import prot_codec as pc
|
import prot_codec as pc
|
||||||
|
from log import myprint
|
||||||
|
from log import log_init
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -40,7 +42,7 @@ class tcp_target(object):
|
|||||||
global _tcp_server
|
global _tcp_server
|
||||||
# 解包查看数据类型
|
# 解包查看数据类型
|
||||||
cmd,data=pc.decode(recv_data)
|
cmd,data=pc.decode(recv_data)
|
||||||
print(cmd.decode('utf-8'))
|
myprint(cmd.decode('utf-8'))
|
||||||
j=json.loads(cmd)
|
j=json.loads(cmd)
|
||||||
if(j["device"]=="client"):
|
if(j["device"]=="client"):
|
||||||
if(j["option"]=="login"):
|
if(j["option"]=="login"):
|
||||||
@@ -72,12 +74,12 @@ class tcp_target(object):
|
|||||||
try:
|
try:
|
||||||
_tcp_client.send(data)
|
_tcp_client.send(data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("target_close:",str(e))
|
myprint("target_close:",str(e))
|
||||||
if(_tcp_server is not None):
|
if(_tcp_server is not None):
|
||||||
try:
|
try:
|
||||||
_tcp_server.send(data)
|
_tcp_server.send(data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("target_close:",str(e))
|
myprint("target_close:",str(e))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -10,6 +10,8 @@ import time
|
|||||||
|
|
||||||
|
|
||||||
import prot_codec as pc
|
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):
|
def send_to(ip,port,data:bytearray):
|
||||||
for item in _local_client:
|
for item in _local_client:
|
||||||
if(item[1]==ip and item[2]==port):
|
if(item[1]==ip and item[2]==port):
|
||||||
myprint(f"recv from remote {ip},{port}")
|
|
||||||
item[0].send(data)
|
item[0].send(data)
|
||||||
return
|
return
|
||||||
myprint(f"can not fond {ip},{port}")
|
myprint(f"can not fond {ip}:{port}")
|
||||||
|
|
||||||
|
|
||||||
# 关闭指定地址的端口
|
# 关闭指定地址的端口
|
||||||
def close(ip,port):
|
def close(ip,port):
|
||||||
|
global _local_client
|
||||||
for item in _local_client:
|
for item in _local_client:
|
||||||
if(item[1]==ip and item[2]==port):
|
if(item[1]==ip and item[2]==port):
|
||||||
myprint(f'remote close {ip},{port}')
|
myprint(f'断开连接 {ip}:{port}')
|
||||||
item[0].close()
|
item[0].close()
|
||||||
_local_client.remove(item)
|
_local_client.remove(item)
|
||||||
break
|
break
|
||||||
|
|
||||||
# 关闭所有
|
# 关闭所有
|
||||||
def close_all():
|
def close_all():
|
||||||
|
global _local_client
|
||||||
for item in _local_client:
|
for item in _local_client:
|
||||||
item[0].close()
|
item[0].close()
|
||||||
_local_client=[]
|
_local_client=[]
|
||||||
myprint('remote close all')
|
myprint('连接列表已清空')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -92,7 +79,7 @@ def remote_client_handler(tcp_remote:socket):
|
|||||||
try:
|
try:
|
||||||
recv = tcp_remote.recv(4096)
|
recv = tcp_remote.recv(4096)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
myprint("remote:",str(e))
|
myprint("代理服务器连接异常",str(e))
|
||||||
break
|
break
|
||||||
if recv:
|
if recv:
|
||||||
recv_data+=recv
|
recv_data+=recv
|
||||||
@@ -107,11 +94,14 @@ def remote_client_handler(tcp_remote:socket):
|
|||||||
j=json.loads(cmd)
|
j=json.loads(cmd)
|
||||||
if(j['device']=='server'):
|
if(j['device']=='server'):
|
||||||
if(j['option']=='data'):
|
if(j['option']=='data'):
|
||||||
|
myprint(f"收到数据 {j['ip']}:{j['port']}")
|
||||||
send_to(j['ip'],j['port'],data)
|
send_to(j['ip'],j['port'],data)
|
||||||
elif(j['option']=='disconnect'):
|
elif(j['option']=='disconnect'):
|
||||||
|
myprint(f"收到服务器的断开通知 {j['ip']}:{j['port']}")
|
||||||
close(j['ip'],j['port'])
|
close(j['ip'],j['port'])
|
||||||
elif(j['device']=='proxy'):
|
elif(j['device']=='proxy'):
|
||||||
if(j['option']=='close'):
|
if(j['option']=='close'):
|
||||||
|
myprint("收到代理服务器的断开通知")
|
||||||
close_all()
|
close_all()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
myprint(str(e))
|
myprint(str(e))
|
||||||
@@ -119,7 +109,7 @@ def remote_client_handler(tcp_remote:socket):
|
|||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
tcp_remote.close()
|
tcp_remote.close()
|
||||||
myprint("proxy close")
|
myprint("代理服务器已断开")
|
||||||
close_all()
|
close_all()
|
||||||
if _local_server is not None:
|
if _local_server is not None:
|
||||||
_local_server.close()
|
_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)
|
data=pc.encode(json.dumps(cmd).encode('utf-8'),recv)
|
||||||
if _remote_client is not None:
|
if _remote_client is not None:
|
||||||
_remote_client.send(data)
|
_remote_client.send(data)
|
||||||
myprint(f"send to remote {addr[0]},{addr[1]}")
|
myprint(f"发送数据 {addr[0]}:{addr[1]}")
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
tcp_server.close()
|
close(addr[0],addr[1])
|
||||||
if(_remote_client is not None):
|
|
||||||
cmd={'device':'client','option':'disconnect','ip':addr[0],'port':addr[1]}
|
cmd={'device':'client','option':'disconnect','ip':addr[0],'port':addr[1]}
|
||||||
data=pc.encode(json.dumps(cmd).encode('utf-8'),b'default')
|
data=pc.encode(json.dumps(cmd).encode('utf-8'),b'default')
|
||||||
|
if(_remote_client is not None):
|
||||||
try:
|
try:
|
||||||
|
myprint(f"发送断开通知到服务器端 {addr[0]}:{addr[1]}")
|
||||||
_remote_client.send(data)
|
_remote_client.send(data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
myprint("remote close",str(e))
|
myprint("远端连接异常",str(e))
|
||||||
_local_client.remove(addr_info)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -188,6 +178,6 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
_log_fp=open("target_client.log",mode="w+",encoding="utf-8")
|
log_init("target_client.log")
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
@@ -9,6 +9,8 @@ import time
|
|||||||
|
|
||||||
|
|
||||||
import prot_codec as pc
|
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.166",80)
|
||||||
# LOCAL_SERVER_IP = ("192.168.3.167",22)
|
# 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):
|
def send_to(ip,port,data:bytearray):
|
||||||
for item in _local_client:
|
for item in _local_client:
|
||||||
if(item[1]==ip and item[2]==port):
|
if(item[1]==ip and item[2]==port):
|
||||||
myprint(f"recv from remote {ip},{port}")
|
|
||||||
item[0].send(data)
|
item[0].send(data)
|
||||||
break
|
break
|
||||||
|
|
||||||
# 关闭指定地址的端口
|
# 关闭指定地址的端口
|
||||||
def close(ip,port):
|
def close(ip,port):
|
||||||
|
global _local_client
|
||||||
for item in _local_client:
|
for item in _local_client:
|
||||||
if(item[1]==ip and item[2]==port):
|
if(item[1]==ip and item[2]==port):
|
||||||
myprint(f"remote close:{ip},{port}")
|
myprint(f"断开连接 {ip}:{port}")
|
||||||
item[0].close()
|
item[0].close()
|
||||||
# 删除已被关闭的条目
|
# 删除已被关闭的条目
|
||||||
_local_client.remove(item)
|
_local_client.remove(item)
|
||||||
@@ -75,11 +61,12 @@ def close(ip,port):
|
|||||||
|
|
||||||
# 关闭所有
|
# 关闭所有
|
||||||
def close_all():
|
def close_all():
|
||||||
|
global _local_client
|
||||||
for item in _local_client:
|
for item in _local_client:
|
||||||
item[0].close()
|
item[0].close()
|
||||||
# 关闭端口之后把列表置空
|
# 关闭端口之后把列表置空
|
||||||
_local_client=[]
|
_local_client=[]
|
||||||
myprint('remote close all')
|
myprint('连接列表已清空')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -94,24 +81,28 @@ def local_client_handler(tcp_server:socket,ip,port):
|
|||||||
try:
|
try:
|
||||||
recv = tcp_server.recv(4096)
|
recv = tcp_server.recv(4096)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
myprint("local:",str(e))
|
myprint("本地连接异常",str(e))
|
||||||
break
|
break
|
||||||
if recv:
|
if recv:
|
||||||
cmd={'device':'server','option':'data','ip':ip,'port':port}
|
cmd={'device':'server','option':'data','ip':ip,'port':port}
|
||||||
data=pc.encode(json.dumps(cmd).encode('utf-8'),recv)
|
data=pc.encode(json.dumps(cmd).encode('utf-8'),recv)
|
||||||
if _remote_client is not None:
|
if _remote_client is not None:
|
||||||
_remote_client.send(data)
|
_remote_client.send(data)
|
||||||
myprint(f"send to remote {ip},{port}")
|
myprint(f"发送数据到客户端 {ip}:{port}")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
tcp_server.close()
|
tcp_server.close()
|
||||||
_local_client.remove(self_info)
|
close(ip,port)
|
||||||
# 发送连接断开的消息
|
# 发送连接断开的消息
|
||||||
cmd={'device':'server','option':'disconnect','ip':ip,'port':port}
|
cmd={'device':'server','option':'disconnect','ip':ip,'port':port}
|
||||||
data=pc.encode(json.dumps(cmd).encode('utf-8'),b'default')
|
data=pc.encode(json.dumps(cmd).encode('utf-8'),b'default')
|
||||||
if _remote_client is not None:
|
if _remote_client is not None:
|
||||||
|
try:
|
||||||
|
myprint(f"发送断开通知到客户端 {ip}:{port}")
|
||||||
_remote_client.send(data)
|
_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)
|
j=json.loads(cmd)
|
||||||
if(j['device']=='client'):
|
if(j['device']=='client'):
|
||||||
if(j['option']=='connect'):
|
if(j['option']=='connect'):
|
||||||
|
myprint("收到客户端的连接通知")
|
||||||
temp = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
temp = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
temp.connect(LOCAL_SERVER_IP)
|
temp.connect(LOCAL_SERVER_IP)
|
||||||
thd = threading.Thread(target = local_client_handler, args = (temp,j['ip'],j['port']))
|
thd = threading.Thread(target = local_client_handler, args = (temp,j['ip'],j['port']))
|
||||||
thd.start()
|
thd.start()
|
||||||
elif(j['option']=='disconnect'):
|
elif(j['option']=='disconnect'):
|
||||||
|
myprint(f"收到客户端的断开通知 {j['ip']}:{j['port']}")
|
||||||
close(j['ip'],j['port'])
|
close(j['ip'],j['port'])
|
||||||
elif(j['option']=='data'):
|
elif(j['option']=='data'):
|
||||||
|
myprint(f"收到数据 {j['ip']}:{j['port']}")
|
||||||
send_to(j['ip'],j['port'],data)
|
send_to(j['ip'],j['port'],data)
|
||||||
elif(j['device']=='proxy'):
|
elif(j['device']=='proxy'):
|
||||||
if(j['option']=='close'):
|
if(j['option']=='close'):
|
||||||
|
myprint("收到代理服务器的断开通知")
|
||||||
close_all()
|
close_all()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
myprint(str(e))
|
myprint(str(e))
|
||||||
@@ -161,7 +156,7 @@ def remote_client_handler(tcp_client_1:socket):
|
|||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
tcp_client_1.close()
|
tcp_client_1.close()
|
||||||
myprint("proxy close.")
|
myprint("与代理服务器的连接已断开")
|
||||||
close_all()
|
close_all()
|
||||||
|
|
||||||
|
|
||||||
@@ -182,6 +177,6 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
_log_fp=open("target_server.log",mode="w+",encoding="utf-8")
|
log_init("target_server.log")
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user