简体   繁体   中英

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). 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).

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

Example of implementation 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()

Example of implementation 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. 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).

I'm trying to build a simple TCP proxy that listens to the communication between two machines (machine can be either PC or server). 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).

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

Example of implementation 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()

Example of implementation 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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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