Z
Z
zax20322018-02-05 19:08:28
Python
zax2032, 2018-02-05 19:08:28

How to create a "reproxy"?

I want to make a script that will be accessed by the proxy client, like a regular proxy, and it will transfer all the information to a real proxy and then everything in reverse order. I don't know Python very well. So far, I've remade the server socket example and it only works with http requests. When the client sends the CONNECT method for an https connection, the server returns 200 OK, then the client makes another request in the same session and no response comes from it. Help, what's the problem?

import socket
import threading

class LocalProxy(object):
  def __init__(self, host, port):
    self.host = host
    self.port = port
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.sock.bind((self.host, self.port))

  def listen(self):
    self.sock.listen(10)
    while True:
      client, address = self.sock.accept()
      client.settimeout(60)
      threading.Thread(target = self.listenToClient,args = (client,address)).start()

  def listenToClient(self, client, address):
    #Client session oppened
    print('Client connected')
    
    #Open server session
    server = socket.socket()
    server.connect(('proxy ip', 8123))
    
    #Client listen
    while True:
      #Attempt to receive client data
      data = client.recv(8192)
      if data:
        server.send(data)
        while True:
          resp = server.recv(8192)
          if not resp or len(resp) == 0:
            break
          print(resp)
          client.send(resp)
      else:
        server.close()
        client.close()
        break;

if __name__ == "__main__":
  LocalProxy('127.0.0.1',2001).listen()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question