添加日志记录到文件
This commit is contained in:
@@ -5,6 +5,7 @@ import sys
|
||||
import json
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
import prot_codec as pc
|
||||
@@ -23,6 +24,7 @@ import prot_codec as pc
|
||||
# 保存连接代理服务器的端口
|
||||
_remote_client=None
|
||||
# 保存本地连接的tcp客户端端口列表
|
||||
# 这个列表的ip和port地址和target_client.py中的同名变量一一对应
|
||||
_local_client=[]
|
||||
|
||||
|
||||
@@ -32,11 +34,32 @@ LOCAL_SERVER_IP = ("10.0.24.251",80)
|
||||
|
||||
|
||||
|
||||
_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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# 发送数据到指定ip地址和端口
|
||||
def send_to(ip,port,data:bytearray):
|
||||
for item in _local_client:
|
||||
if(item[1]==ip and item[2]==port):
|
||||
print(f"recv from remote {ip},{port}")
|
||||
myprint(f"recv from remote {ip},{port}")
|
||||
item[0].send(data)
|
||||
break
|
||||
|
||||
@@ -44,7 +67,7 @@ def send_to(ip,port,data:bytearray):
|
||||
def close(ip,port):
|
||||
for item in _local_client:
|
||||
if(item[1]==ip and item[2]==port):
|
||||
print(f"remote close:{ip},{port}")
|
||||
myprint(f"remote close:{ip},{port}")
|
||||
item[0].close()
|
||||
# 删除已被关闭的条目
|
||||
_local_client.remove(item)
|
||||
@@ -56,7 +79,7 @@ def close_all():
|
||||
item[0].close()
|
||||
# 关闭端口之后把列表置空
|
||||
_local_client=[]
|
||||
print('remote close all')
|
||||
myprint('remote close all')
|
||||
|
||||
|
||||
|
||||
@@ -71,14 +94,14 @@ def local_client_handler(tcp_server:socket,ip,port):
|
||||
try:
|
||||
recv = tcp_server.recv(4096)
|
||||
except Exception as e:
|
||||
print("local:",str(e))
|
||||
myprint("local:",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)
|
||||
print(f"send to remote {ip},{port}")
|
||||
myprint(f"send to remote {ip},{port}")
|
||||
|
||||
else:
|
||||
break
|
||||
@@ -97,7 +120,7 @@ def local_client_handler(tcp_server:socket,ip,port):
|
||||
def remote_client_handler(tcp_client_1:socket):
|
||||
global _remote_client
|
||||
global _local_client
|
||||
print("已连接代理服务器")
|
||||
myprint("已连接代理服务器")
|
||||
cmd={'device':'server','option':'login'}
|
||||
data=pc.encode(json.dumps(cmd).encode('utf-8'),b'default')
|
||||
_remote_client.send(data)
|
||||
@@ -106,7 +129,7 @@ def remote_client_handler(tcp_client_1:socket):
|
||||
try:
|
||||
recv = tcp_client_1.recv(4096)
|
||||
except Exception as e:
|
||||
print("remote:",str(e))
|
||||
myprint("remote:",str(e))
|
||||
break
|
||||
if recv:
|
||||
recv_data+=recv
|
||||
@@ -116,7 +139,7 @@ def remote_client_handler(tcp_client_1:socket):
|
||||
if(start == -1 or end == -1):
|
||||
break
|
||||
cmd,data=pc.decode(recv_data[start:end+1])
|
||||
print(cmd.decode('utf-8'))
|
||||
myprint(cmd.decode('utf-8'))
|
||||
try:
|
||||
j=json.loads(cmd)
|
||||
if(j['device']=='client'):
|
||||
@@ -133,12 +156,12 @@ def remote_client_handler(tcp_client_1:socket):
|
||||
if(j['option']=='close'):
|
||||
close_all()
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
myprint(str(e))
|
||||
recv_data=recv_data[end+1:]
|
||||
else:
|
||||
break
|
||||
tcp_client_1.close()
|
||||
print("proxy close.")
|
||||
myprint("proxy close.")
|
||||
close_all()
|
||||
|
||||
|
||||
@@ -159,5 +182,6 @@ def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_log_fp=open("target_server.log",mode="w+",encoding="utf-8")
|
||||
main()
|
||||
|
||||
|
Reference in New Issue
Block a user