我正在编写一个Python脚本,它通过(超级复杂的)SOCKS/SSL隧道连接到远程主机。我能够在任何端口上建立与远程 Intranet 中 IP 的连接。
我希望做的是设置这个Python脚本以使用本地环回范围(127.0.xx)中的IP地址成为(也许在主机文件的帮助下)远程系统的“副本”,因此使我能够使用不支持代理的应用程序。问题是我并不总是知道他们试图连接到哪些端口。解决这个问题的唯一方法似乎是将套接字绑定到所有 65536 端口,这似乎有点疯狂。所以有两个问题:
- 这疯了吗?我可以设置一个从 1-65536 的 python 套接字列表吗?
- 或者我应该有更好的方法吗?我可以以某种方式监视与 IP 的连接并在需要之前绑定端口吗?
如果可能的话,我想避免使用太多依赖于平台或非Python的代码。
编辑:为了澄清,我只在这里编写客户端 - 我无法控制服务器。相信我,如果我可以控制服务器端,我就不会使用 SOCKS/SSL/CRAM 来实现它:)
I'm writing a Python script which connects to remote hosts over a (super complicated) SOCKS/SSL tunnel. I am able to establish connections to IPs in a remote intranet on any port.
What I'm hoping to do is set up this python script to use IP addresses in the local loopback range (127.0.x.x) to become (maybe with the help of the hosts file) a 'replica' of the remote systems, and hence enable me to use applications which don't support proxies. The problem is that I don't always know what ports they're trying to connect to. It seems the only way to work this out is to bind sockets to all 65536 ports, which seems a little crazy. So two questions:
- Is it crazy? Can I just set up a python list of sockets from 1-65536?
- Or is there a better way I should be doing this? Can I monitor connections to an IP somehow and bind the ports just before they're needed?
I want to avoid using too much platform-dependent or non-python code if possible.
EDIT: To clarify, I'm only writing the client here - I have no control over the server. Believe me, if I had control over the server side of it I would not be doing it with SOCKS/SSL/CRAM :)
发布评论
评论(2)
进入较低级别并连接专为网络分析器(例如 pycap)设计的库怎么样?
通过这种方式,您可以检测所有连接尝试并找到需要公开的端口,或者您可以直接路由数据包,假设该库除了数据包检测之外还可以进行数据包注入(pypcap 页面说此功能是实验性的)。
在我看来,这在 python 中只对缓慢的应用程序有意义,但是……
Pycap 似乎是为 Linux 开发的,但核心捕获是由 libpcap 完成的,对于 Windows,有一个类似的库 winpcap。
What about going lower level and interfacing a library designed for network analyzers like pycap?
This way you could detect all connection attempts and find the ports that you need to expose or may be you can just route the packets directly assuming the library in addition to packet detection can also do packet injection (pypcap page says this feature is experimental).
This would IMO make sense in python only for slow applications however...
Pycap seems to be developed for linux, but the core capturing is done by libpcap and for windows there is a similar library winpcap.
Matt,
如果使用 Windows,最好的选择是通过隧道的 OpenVPN。 OpenVPN 仅需要一个 TCP 端口/流,并为您提供一对具有完全连接性的虚拟接口。
[更新]
可以在客户端使用 TUN/TAP 驱动程序。请参阅 此 Unix 版本了解想法。
Matt,
If using windows your best shot is something like OpenVPN over the tunnel. OpenVPN requires only one TCP port/stream and gives you a pair of virtual interfaces with full connectivity.
[updated]
It may be possible using a TUN/TAP driver on the client side. See this unix version for ideas.