2024-05-22 19:03:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import socket
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
|
|
|
import prot_codec as pc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# tcp服务端脚本
|
|
|
|
# 一个连接远端转发服务器
|
|
|
|
# 另一个连接本地tcp服务器
|
|
|
|
|
|
|
|
|
|
|
|
_remote_client=None
|
|
|
|
_local_client=[]
|
|
|
|
|
|
|
|
|
2024-05-23 11:14:17 +08:00
|
|
|
# LOCAL_SERVER_IP = ("192.168.3.166",80)
|
2024-05-22 19:03:17 +08:00
|
|
|
# LOCAL_SERVER_IP = ("192.168.3.167",22)
|
2024-05-23 11:14:17 +08:00
|
|
|
LOCAL_SERVER_IP = ("10.0.24.251",80)
|
2024-05-22 19:03:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 发送数据到指定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}")
|
|
|
|
item[0].send(data)
|
|
|
|
break
|
|
|
|
|
|
|
|
# 关闭指定地址的端口
|
|
|
|
def close(ip,port):
|
|
|
|
for item in _local_client:
|
|
|
|
if(item[1]==ip and item[2]==port):
|
2024-05-22 19:37:47 +08:00
|
|
|
print(f"remote close:{ip},{port}")
|
2024-05-22 19:03:17 +08:00
|
|
|
item[0].close()
|
|
|
|
break
|
|
|
|
|
2024-05-23 11:14:17 +08:00
|
|
|
# 关闭所有
|
|
|
|
def close_all():
|
|
|
|
for item in _local_client:
|
|
|
|
item[0].close()
|
|
|
|
print('remote close all')
|
2024-05-22 19:03:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 本地数据处理,解包,把负载数据发送到本地服务器
|
|
|
|
def local_client_handler(tcp_server:socket,ip,port):
|
|
|
|
global _remote_client
|
|
|
|
global _local_client
|
|
|
|
self_info=(tcp_server,ip,port)
|
|
|
|
_local_client.append(self_info)
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
recv = tcp_server.recv(4096)
|
|
|
|
except Exception as e:
|
2024-05-22 19:37:47 +08:00
|
|
|
print("local:",str(e))
|
2024-05-22 19:03:17 +08:00
|
|
|
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}")
|
|
|
|
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
tcp_server.close()
|
|
|
|
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)
|
|
|
|
_local_client.remove(self_info)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 远端数据处理,解包,把负载数据发送到本地服务器
|
|
|
|
def remote_client_handler(tcp_client_1:socket):
|
|
|
|
global _remote_client
|
|
|
|
global _local_client
|
|
|
|
print("已连接代理服务器")
|
|
|
|
cmd={'device':'server','option':'login'}
|
|
|
|
data=pc.encode(json.dumps(cmd).encode('utf-8'),b'default')
|
|
|
|
_remote_client.send(data)
|
|
|
|
recv_data=bytearray()
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
recv = tcp_client_1.recv(4096)
|
|
|
|
except Exception as e:
|
2024-05-22 19:37:47 +08:00
|
|
|
print("remote:",str(e))
|
2024-05-22 19:03:17 +08:00
|
|
|
break
|
|
|
|
if recv:
|
|
|
|
recv_data+=recv
|
|
|
|
while True:
|
|
|
|
start=recv_data.find(b'\xff')
|
|
|
|
end=recv_data.find(b'\xfe')
|
|
|
|
if(start == -1 or end == -1):
|
|
|
|
break
|
|
|
|
cmd,data=pc.decode(recv_data[start:end+1])
|
|
|
|
print(cmd.decode('utf-8'))
|
|
|
|
try:
|
|
|
|
j=json.loads(cmd)
|
|
|
|
if(j['device']=='client'):
|
|
|
|
if(j['option']=='connect'):
|
|
|
|
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'):
|
|
|
|
close(j['ip'],j['port'])
|
|
|
|
elif(j['option']=='data'):
|
|
|
|
send_to(j['ip'],j['port'],data)
|
2024-05-23 11:14:17 +08:00
|
|
|
elif(j['device']=='proxy'):
|
|
|
|
if(j['option']=='close'):
|
|
|
|
close_all()
|
2024-05-22 19:03:17 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
|
|
|
recv_data=recv_data[end+1:]
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
tcp_client_1.close()
|
2024-05-23 11:14:17 +08:00
|
|
|
print("proxy close.")
|
2024-10-08 18:46:04 +08:00
|
|
|
close_all()
|
2024-05-22 19:03:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
global _remote_client
|
|
|
|
global _local_client
|
|
|
|
|
|
|
|
_remote_client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
|
|
|
_remote_client.connect(("1.92.113.30",5345))
|
|
|
|
# thd = threading.Thread(target = remote_client_handler, args = (_remote_client,))
|
|
|
|
# thd.start()
|
|
|
|
# thd.join()
|
|
|
|
|
|
|
|
remote_client_handler(_remote_client)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
|