我改编了 Sam Mason 的答案以执行 UDP 反向连接。
这是配置:
HOST UDP-LISTEN <=> python cc <=> NAT <=> internet <=> server <=> python ll UDP-LISTEN
主机监听一个 UDP 端口。您可以通过 python 脚本使用反向连接从任何有权访问服务器的第三个客户端访问主机。
第一部分是听听脚本。它将在 CC_PORT 上侦听客户端脚本,并在 SERVICE_PORT 上转发任何内容。它还会丢弃来自 cc 脚本的 keepalive。
import socket
from select import select
CC_PORT = 8881
SERVICE_PORT = 8880
LISTEN_IP='x.x.x.x'
sock_cc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock_cc.bind((LISTEN_IP,CC_PORT))
sock_serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock_serv.bind((LISTEN_IP,SERVICE_PORT))
sockets = (sock_cc, sock_serv)
for s in sockets:
s.setblocking(0)
client_serv=None
client_cc=None
# loop forever forwarding packets between the connections
while True:
avail, _, _ = select((sock_cc, sock_serv), (), (), 1)
# send a keep alive message every timeout
if not avail:
continue
for s in avail:
# something from the local server, forward it on
if s is sock_cc:
msg = sock_cc.recvfrom(8192)
client_cc=msg[1]
if client_serv is not None:
if msg[0] != b'keep alive':
sock_serv.sendto(msg[0], client_serv)
# something from the remote server
if s is sock_serv:
msg = sock_serv.recvfrom(8192)
client_serv=msg[1]
if client_cc is not None:
sock_cc.sendto(msg[0], client_cc)
客户端客户端脚本
import socket
from select import select
DEST_IP = 'x.x.x.x'
DEST_PORT = 8881
LOCAL_IP = '127.0.0.1'
LOCAL_SERVICE = 8800
sock_remote = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock_remote.connect((DEST_IP,DEST_PORT)) sock_local = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock_local.connect((LOCAL_IP,LOCAL_SERVICE)) sockets = (sock_remote, sock_local) for s in sockets:
s.setblocking(0)
# loop forever forwarding packets between the connections while True:
avail, _, _ = select((sock_local, sock_remote), (), (), 1)
# send a keep alive message every timeout
if not avail:
sock_remote.send(b'keep alive')
continue
for s in avail:
# something from the local server, forward it on
if s is sock_local:
msg = sock_local.recv(8192)
sock_remote.send(msg)
# something from the remote server
if s is sock_remote:
msg = sock_remote.recv(8192)
# don't forward keep alives to local system
if msg != b'keep alive':
sock_local.send(msg)
再次感谢 Sam Mason 帮助我解决了这个问题。