tcp-proxy.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. import sys
  3. from twisted.internet import defer
  4. from twisted.internet import protocol
  5. from twisted.internet import reactor
  6. from twisted.python import log
  7. HOST = '127.0.0.1'
  8. PORT = 2002
  9. LISTEN_PORT = 9999
  10. LISTEN_ON = '127.0.0.1'
  11. class ProxyClientProtocol(protocol.Protocol):
  12. def connectionMade(self):
  13. log.msg("Client: connected to peer")
  14. self.cli_queue = self.factory.cli_queue
  15. self.cli_queue.get().addCallback(self.serverDataReceived)
  16. def serverDataReceived(self, chunk):
  17. if chunk is False:
  18. self.cli_queue = None
  19. log.msg("Client: disconnecting from peer")
  20. self.factory.continueTrying = False
  21. self.transport.loseConnection()
  22. elif self.cli_queue:
  23. log.msg("Client: writing %d bytes to peer" % len(chunk))
  24. self.transport.write(chunk)
  25. self.cli_queue.get().addCallback(self.serverDataReceived)
  26. else:
  27. self.factory.cli_queue.put(chunk)
  28. def dataReceived(self, chunk):
  29. log.msg("Client: %d bytes received from peer" % len(chunk))
  30. self.factory.svr_queue.put(chunk)
  31. def connectionLost(self, why):
  32. log.msg("connectionLost because: %s" % why)
  33. # if self.cli_queue:
  34. # self.cli_queue = None
  35. # log.msg("Client: peer disconnect unexpectedly")
  36. self.factory.svr_queue.put(False)
  37. self.cli_queue = None
  38. self.transport.loseConnection()
  39. # class ProxyClientFactory(protocol.ReconnectingClientFactory):
  40. class ProxyClientFactory(protocol.ClientFactory):
  41. maxDelay = 10
  42. # continueTrying = True
  43. # continueTrying = False
  44. protocol = ProxyClientProtocol
  45. def __init__(self, svr_queue, cli_queue):
  46. self.svr_queue = svr_queue
  47. self.cli_queue = cli_queue
  48. class ProxyServer(protocol.Protocol):
  49. def connectionMade(self):
  50. self.svr_queue = defer.DeferredQueue()
  51. self.cli_queue = defer.DeferredQueue()
  52. self.svr_queue.get().addCallback(self.clientDataReceived)
  53. factory = ProxyClientFactory(self.svr_queue, self.cli_queue)
  54. reactor.connectTCP(HOST, PORT, factory)
  55. def clientDataReceived(self, chunk):
  56. if chunk is False:
  57. self.transport.loseConnection()
  58. else:
  59. log.msg("Server: writing %d bytes to original client" % len(chunk))
  60. self.transport.write(chunk)
  61. self.svr_queue.get().addCallback(self.clientDataReceived)
  62. def dataReceived(self, chunk):
  63. log.msg("Server: %d bytes received" % len(chunk))
  64. self.cli_queue.put(chunk)
  65. def connectionLost(self, why):
  66. log.msg("lost connection %s" % why)
  67. self.cli_queue.put(False)
  68. if __name__ == "__main__":
  69. log.startLogging(sys.stdout)
  70. factory = protocol.Factory()
  71. factory.protocol = ProxyServer
  72. reactor.listenTCP(LISTEN_PORT, factory, interface=LISTEN_ON)
  73. reactor.run()