如何使用 Python 或 C++ 创建通用网络代理?
我有一个通过局域网进行通信的应用程序。但是,我想让它通过互联网进行通信。为此,我建议制作一个中间程序,该程序将读取一台计算机上的应用程序生成的网络流量,并将其发送到另一台计算机上的应用程序。
这涉及:
- 读取应用程序的传出网络流量
- 通过互联网将此流量的副本发送到另一台计算机
- 将该副本提供给另一台计算机上的应用程序
代替此:
计算机 A <-LAN-> 上的应用程序计算机B上的应用程序
我想实现这一点:
A上的应用程序<-->我在 A <-INTERNET-> 上的程序我的 B 上的程序<-->在B上的应用
我可以完成(2),但是对于(1)和(3)我的问题是我对网络的经验很少,我不知道从哪里开始。我可以用 python 编程,但愿意使用 c++ 来完成此任务。
(Hamachi 不适用于此应用程序,我不知道为什么。)
为了回应评论
,我不打算操作任何数据,除非有必要使连接工作。我无法控制应用程序本身,并且除了端口号之外,它没有为我提供任何配置连接的方法。
TCP 和 UDP 均在端口 6112 上使用。首先使用的 IP 地址是 255.255.255.255,用于通用广播,用于发现 LAN 上的其他应用程序(使用 UDP),然后建立 TCP 连接。
I have an application which communicates over the local area network. However, I want to instead make it communicate over the internet. To do this I propose making an intermediate program which will read the network traffic generated from the application on one computer and send it to the application on another computer.
This involves:
- Reading the outgoing network traffic of the application
- Sending a copy of this traffic over the internet to another computer
- Giving this copy to the application on the other computer
Instead of this:
Application on computer A <-LAN-> Application on computer B
I want to achieve this:
Application on A <--> My Program on A <-INTERNET-> My program on B <--> Application on B
I can accomplish (2), but with (1) and (3) my problem is that I have very little experience with networking and I do not know where to start. I can program in python but would be willing to use c++ to accomplish this.
(Hamachi does not work for this application, I do not know why.)
In response to comments
I do not intend to manipulate any data unless it is necessary to make the connection work. I have no control over the application itself and it does not provide me with any methods to configure the connection with the exception of a port number.
TCP and UDP are both used on the port 6112. The IP addresses used are first 255.255.255.255 for a generic broadcast used to discover other applications on the LAN (with UDP), then a TCP connection is established.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在原始问题中缺少的术语是代理。您特别需要一个
透明转发代理
。这是一些源代码的链接 在 Python 中,它将帮助您开始编写代理。
也就是说,如果您四处搜索,您应该能够找到一个不必自己编写的
透明转发代理
。如果您想以最可靠的方式做到这一点,您可以在硬件中完成,并设置托管路由器/交换机/防火墙,将事物路由到您需要的任何地方,而无需编写任何内容。
The term you are missing in your original question is
proxy
. You specifically need atransparent forwarding proxy
.Here is a link to some source code in Python that will get you started with writing a proxy.
That said, if you search around you should be able to find a
transparent forwarding proxy
that you don't have to write yourself.If you want to do this the most robust way, you can do it in hardware and setup a managed router/switch/firewall to route things to where ever you need without having to write anything.
为什么要重新设计车轮?为什么不直接使用 OpenVPN、n2n 或 vtun 等。
Why re-engineer the wheel? Why not just use OpenVPN, n2n or vtun etc etc.