tcp-proxy.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. # TODO: Line processing, and line cleaning (remove ANSI color codes)
  18. if chunk is False:
  19. self.cli_queue = None
  20. log.msg("Client: disconnecting from peer")
  21. self.factory.continueTrying = False
  22. self.transport.loseConnection()
  23. elif b"$" == chunk:
  24. self.factory.svr_queue.put(b"HELLO.\r\n")
  25. self.cli_queue.get().addCallback(self.serverDataReceived)
  26. elif self.cli_queue:
  27. log.msg("Client: writing %d bytes to peer" % len(chunk))
  28. log.msg(">>", chunk)
  29. self.transport.write(chunk)
  30. self.cli_queue.get().addCallback(self.serverDataReceived)
  31. else:
  32. self.factory.cli_queue.put(chunk)
  33. def dataReceived(self, chunk):
  34. log.msg("Client: %d bytes received from peer" % len(chunk))
  35. log.msg("<<", chunk)
  36. self.factory.svr_queue.put(chunk)
  37. def connectionLost(self, why):
  38. log.msg("connectionLost because: %s" % why)
  39. # if self.cli_queue:
  40. # self.cli_queue = None
  41. # log.msg("Client: peer disconnect unexpectedly")
  42. self.factory.svr_queue.put(False)
  43. self.cli_queue = None
  44. self.transport.loseConnection()
  45. # class ProxyClientFactory(protocol.ReconnectingClientFactory):
  46. class ProxyClientFactory(protocol.ClientFactory):
  47. maxDelay = 10
  48. # continueTrying = True
  49. # continueTrying = False
  50. protocol = ProxyClientProtocol
  51. def __init__(self, svr_queue, cli_queue):
  52. self.svr_queue = svr_queue
  53. self.cli_queue = cli_queue
  54. class ProxyServer(protocol.Protocol):
  55. def connectionMade(self):
  56. self.svr_queue = defer.DeferredQueue()
  57. self.cli_queue = defer.DeferredQueue()
  58. self.svr_queue.get().addCallback(self.clientDataReceived)
  59. factory = ProxyClientFactory(self.svr_queue, self.cli_queue)
  60. reactor.connectTCP(HOST, PORT, factory)
  61. def clientDataReceived(self, chunk):
  62. if chunk is False:
  63. self.transport.loseConnection()
  64. else:
  65. log.msg("Server: writing %d bytes to original client" % len(chunk))
  66. self.transport.write(chunk)
  67. self.svr_queue.get().addCallback(self.clientDataReceived)
  68. def dataReceived(self, chunk):
  69. log.msg("Server: %d bytes received" % len(chunk))
  70. self.cli_queue.put(chunk)
  71. def connectionLost(self, why):
  72. log.msg("lost connection %s" % why)
  73. self.cli_queue.put(False)
  74. if __name__ == "__main__":
  75. log.startLogging(sys.stdout)
  76. factory = protocol.Factory()
  77. factory.protocol = ProxyServer
  78. reactor.listenTCP(LISTEN_PORT, factory, interface=LISTEN_ON)
  79. reactor.run()