简体   繁体   English

使用 Python sockets 创建简单的 TCP 代理

[英]Create simple TCP Proxy with Python sockets

I'm trying to build a simple TCP proxy that listens to the communication between two machines (machine can be either PC or server).我正在尝试构建一个简单的 TCP 代理来侦听两台机器之间的通信(机器可以是 PC 或服务器)。 From my understanding, a proxy listens for request on machine A and on machine B, and it intercepts packets from both machines when they try to reach each other, the proxy reads the packets and forwards them to their respective destination (either machine A or B).据我了解,代理在机器 A 和机器 B 上侦听请求,并在两台机器尝试相互访问时拦截来自两台机器的数据包,代理读取数据包并将它们转发到各自的目的地(机器 A 或 B )。

I have two ways of implementing a proxy (I'm not sure which is the correct way)我有两种实现代理的方法(我不确定哪种方法是正确的)

  1. I created 2 connection sockets, one for machine A and one for machine B, and proxy (not a server anymore) connects to both and just sits there and listen我创建了 2 个连接 sockets,一个用于机器 A,一个用于机器 B,代理(不再是服务器)连接到两者并坐在那里听

Example of implementation 1: proxy.py实现示例1: proxy.py

    sa = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # -> Machine A
    print(ip_machine_a)
    sa.connect((ip_machine_a, proxy_port))
    
    sb = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # -> Machine B
    print(ip_machine_b)
    sb.connect((ip_machine_b, proxy_port))
  1. I create one socket that accepts connection from machine A and one socket that connects to machine B, this way my proxy is a server with bind()我创建了一个接受来自机器 A 的连接的套接字和一个连接到机器 B 的套接字,这样我的代理就是一个带有 bind() 的服务器

Example of implementation 2: proxy.py实现示例2: proxy.py

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.bind('localhost', 4000)
s.listen(1) 
connection, addr = s.accept() # Accept any connection (blocking) -> connection is Machine A

sb = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
sb.connect((ip_machine_b, port_b)) # -> Machine B

Which is the correct way of implementing a simple TCP proxy that listens for packet.这是实现侦听数据包的简单 TCP 代理的正确方法。 s between two machines?在两台机器之间? In my opinion, I doubt it is implementation 2 because, in my currrent situation, I cannot write a script from different machine that connects to proxy, because I don't have access to the machines (A and B).在我看来,我怀疑它是实现 2,因为在我目前的情况下,我无法从连接到代理的不同机器上编写脚本,因为我无法访问机器(A 和 B)。

I'm trying to build a simple TCP proxy that listens to the communication between two machines (machine can be either PC or server).我正在尝试构建一个简单的 TCP 代理来侦听两台机器之间的通信(机器可以是 PC 或服务器)。 From my understanding, a proxy listens for request on machine A and on machine B, and it intercepts packets from both machines when they try to reach each other, the proxy reads the packets and forwards them to their respective destination (either machine A or B).据我了解,代理在机器 A 和机器 B 上侦听请求,并在两台机器尝试相互访问时拦截来自两台机器的数据包,代理读取数据包并将它们转发到各自的目的地(机器 A 或 B )。

I have two ways of implementing a proxy (I'm not sure which is the correct way)我有两种实现代理的方法(我不确定哪种方法是正确的)

  1. I created 2 connection sockets, one for machine A and one for machine B, and proxy (not a server anymore) connects to both and just sits there and listen我创建了 2 个连接 sockets,一个用于机器 A,一个用于机器 B,代理(不再是服务器)连接到两者并坐在那里听

Example of implementation 1: proxy.py实现示例1: proxy.py

    sa = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # -> Machine A
    print(ip_machine_a)
    sa.connect((ip_machine_a, proxy_port))
    
    sb = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # -> Machine B
    print(ip_machine_b)
    sb.connect((ip_machine_b, proxy_port))
  1. I create one socket that accepts connection from machine A and one socket that connects to machine B, this way my proxy is a server with bind()我创建了一个接受来自机器 A 的连接的套接字和一个连接到机器 B 的套接字,这样我的代理就是一个带有 bind() 的服务器

Example of implementation 2: proxy.py实现示例2: proxy.py

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.bind('localhost', 4000)
s.listen(1) 
connection, addr = s.accept() # Accept any connection (blocking) -> connection is Machine A

sb = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
sb.connect((ip_machine_b, port_b)) # -> Machine B

Which is the correct way of implementing a simple TCP proxy that listens for packet.这是实现侦听数据包的简单 TCP 代理的正确方法。 s between two machines?在两台机器之间? In my opinion, I doubt it is implementation 2 because, in my currrent situation, I cannot write a script from different machine that connects to proxy, because I don't have access to the machines (A and B).在我看来,我怀疑它是实现 2,因为在我目前的情况下,我无法从连接到代理的不同机器上编写脚本,因为我无法访问机器(A 和 B)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM