Skip to content

Instantly share code, notes, and snippets.

@iamargentum
Created October 17, 2021 12:06
Show Gist options
  • Save iamargentum/11bbf8a753d19fba3c40aebcca1ee8e2 to your computer and use it in GitHub Desktop.
Save iamargentum/11bbf8a753d19fba3c40aebcca1ee8e2 to your computer and use it in GitHub Desktop.
import socket
from threading import Thread
class Proxy2Server(Thread):
def __init__(self, host, port):
super(Proxy2Server, self).__init__()
self.client = None #client socket not known yet
self.port = port
self.host = host
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.connect((host, port))
def run(self):
while True:
data = self.server.recv(4096)
if data:
#forward to client
self.client.sendall(data)
class Client2Proxy(Thread):
def __init__(self, host, port):
super(Client2Proxy, self).__init__()
self.server = None
self.port = port
self.host = host
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(1)
self.tallyProxy, addr = sock.accept()
def run(self):
while True:
data = self.tallyProxy.recv(4096)
if data:
self.server.sendall(data)
class Proxy(Thread):
def __init__(self, fromHost, toHost, fromPort, toPort):
super(Proxy, self).__init__()
self.fromHost = fromHost
self.toHost = toHost
self.fromPort = fromPort
self.toPort = toPort
def run(self):
while True:
print("proxy {} setting up ".format(self.fromPort))
self.c2p = Client2Proxy(self.fromHost, self.fromPort)
self.p2s = Proxy2Server(self.toHost, self.toPort)
print("proxy {} connection established".format(self.fromPort))
self.c2p.server = self.p2s.server
self.p2s.client = self.c2p.tallyProxy
self.c2p.start()
self.p2s.start()
masterServer = Proxy('0.0.0.0', '127.0.0.1', 1234, 9999)
masterServer.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment