gpt4 book ai didi

networking - sshuttle 如何避免 TCP-over-TCP 诅咒?

转载 作者:可可西里 更新时间:2023-11-01 02:33:56 33 4
gpt4 key购买 nike

sshuttle claims它解决了很多问题discussed problemTCP-over-TCP meltdown .

sshuttle assembles the TCP stream locally, multiplexes it statefully over an ssh session, and disassembles it back into packets at the other end. So it never ends up doing TCP-over-TCP. It’s just data-over-TCP, which is safe.

但从程序的角度来看,它维护与目标服务器的 TCP 连接及其附带的所有内容(读取指数超时),这是关于其他 TCP session 的分层,因为 SSH 还不能仅在 UDP。这看起来很像 TCP-over-TCP。

这里有什么技巧? sshuttle真的解决问题了吗?

我尝试阅读 source code ,但至今没有找到答案。

更重要的是,他们究竟是怎么做到的?如果想在准系统中重新实现它,应该从哪里寻找灵感?

最佳答案

sshuttle 客户端设置防火墙规则(Linux 中的 iptables,这就是 sshuttle 客户端需要 root 权限的原因)将某些传出 TCP 连接重定向到本地端口(默认为 12300 ),启动sshuttle可以看到这个过程:

firewall manager: starting transproxy.
>> iptables -t nat -N sshuttle-12300
>> iptables -t nat -F sshuttle-12300
>> iptables -t nat -I OUTPUT 1 -j sshuttle-12300
>> iptables -t nat -I PREROUTING 1 -j sshuttle-12300
>> iptables -t nat -A sshuttle-12300 -j RETURN --dest 127.0.0.0/8 -p tcp
>> iptables -t nat -A sshuttle-12300 -j REDIRECT --dest 0.0.0.0/0 -p tcp --to-ports 12300 -m ttl ! --ttl 42

并在 sshuttle 退出时删除 iptables nat 规则,

>> iptables -t nat -D OUTPUT -j sshuttle-12300
>> iptables -t nat -D PREROUTING -j sshuttle-12300
>> iptables -t nat -F sshuttle-12300
>> iptables -t nat -X sshuttle-12300

TCP 内容被拾取并通过 ssh 连接多路复用到 sshuttle 服务器,然后再次解复用到连接中。 client.py 中的函数 onaccept_tcpin做多路复用:

def onaccept_tcp(listener, method, mux, handlers):
global _extra_fd
try:
sock, srcip = listener.accept()
except socket.error as e:
if e.args[0] in [errno.EMFILE, errno.ENFILE]:
debug1('Rejected incoming connection: too many open files!\n')
# free up an fd so we can eat the connection
os.close(_extra_fd)
try:
sock, srcip = listener.accept()
sock.close()
finally:
_extra_fd = os.open('/dev/null', os.O_RDONLY)
return
else:
raise

dstip = method.get_tcp_dstip(sock)
debug1('Accept TCP: %s:%r -> %s:%r.\n' % (srcip[0], srcip[1],
dstip[0], dstip[1]))
if dstip[1] == sock.getsockname()[1] and islocal(dstip[0], sock.family):
debug1("-- ignored: that's my address!\n")
sock.close()
return
chan = mux.next_channel()
if not chan:
log('warning: too many open channels. Discarded connection.\n')
sock.close()
return
mux.send(chan, ssnet.CMD_TCP_CONNECT, b'%d,%s,%d' %
(sock.family, dstip[0].encode("ASCII"), dstip[1]))
outwrap = MuxWrapper(mux, chan)
handlers.append(Proxy(SockWrapper(sock, sock), outwrap))
expire_connections(time.time(), mux)

ssnet.py中可以看到数据是如何打包的.

我在 redsocks 中看到了相同的策略(我的意思是设置防火墙规则)旨在将任何 TCP 连接重定向到 SOCKS 或 HTTPS 代理。

关于networking - sshuttle 如何避免 TCP-over-TCP 诅咒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41427123/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com