实现端口转发功能,经测试,ssh、http可以正常使用
This commit is contained in:
144
target_client.py
Normal file
144
target_client.py
Normal file
@@ -0,0 +1,144 @@
|
||||
|
||||
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
import prot_codec as pc
|
||||
|
||||
|
||||
|
||||
# tcp 客户端脚本
|
||||
# 一个本地服务端
|
||||
# 一个远程客户端
|
||||
|
||||
|
||||
|
||||
_LOCAL_PORT = 31234
|
||||
|
||||
_remote_client=None
|
||||
_local_client=[]
|
||||
|
||||
|
||||
|
||||
# 发送数据到指定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)
|
||||
return
|
||||
print(f"can not fond {ip},{port}")
|
||||
|
||||
|
||||
# 关闭指定地址的端口
|
||||
def close(ip,port):
|
||||
for item in _local_client:
|
||||
if(item[1]==ip and item[2]==port):
|
||||
item[0].close()
|
||||
break
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# 远端数据处理,解包,把负载数据发送到本地服务器
|
||||
def remote_client_handler(tcp_remote:socket):
|
||||
global _remote_client
|
||||
global _local_client
|
||||
print("已连接代理服务器")
|
||||
recv_data=bytearray()
|
||||
cmd={'device':'client','option':'login'}
|
||||
data=pc.encode(json.dumps(cmd).encode('utf-8'),b'default')
|
||||
_remote_client.send(data)
|
||||
while True:
|
||||
try:
|
||||
recv = tcp_remote.recv(4096)
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
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']=='server'):
|
||||
if(j['option']=='data'):
|
||||
send_to(j['ip'],j['port'],data)
|
||||
elif(j['option']=='disconnect'):
|
||||
close(j['ip'],j['port'])
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
recv_data=recv_data[end+1:]
|
||||
else:
|
||||
break
|
||||
tcp_remote.close()
|
||||
|
||||
|
||||
# 本地数据处理,解包,把负载数据发送到本地服务器
|
||||
def local_client_handler(tcp_server:socket,addr):
|
||||
global _remote_client
|
||||
global _local_client
|
||||
print("addr:",addr)
|
||||
addr_info=(tcp_server,addr[0],addr[1])
|
||||
_local_client.append(addr_info)
|
||||
if(_remote_client is not None):
|
||||
cmd={'device':'client','option':'connect','ip':addr[0],'port':addr[1]}
|
||||
data=pc.encode(json.dumps(cmd).encode('utf-8'),b'default')
|
||||
_remote_client.send(data)
|
||||
while True:
|
||||
try:
|
||||
recv = tcp_server.recv(4096)
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
break
|
||||
if recv:
|
||||
cmd={'device':'client','option':'data','ip':addr[0],'port':addr[1]}
|
||||
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 {addr[0]},{addr[1]}")
|
||||
else:
|
||||
break
|
||||
tcp_server.close()
|
||||
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')
|
||||
_remote_client.send(data)
|
||||
_local_client.remove(addr_info)
|
||||
|
||||
|
||||
|
||||
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()
|
||||
tcp_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
tcp_server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True)
|
||||
tcp_server.bind(("",_LOCAL_PORT))
|
||||
print(f"开始监听({_LOCAL_PORT})")
|
||||
tcp_server.listen(128)
|
||||
while True:
|
||||
temp , temp_address = tcp_server.accept()
|
||||
thd = threading.Thread(target = local_client_handler, args = (temp,temp_address))
|
||||
thd.start()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Reference in New Issue
Block a user