tcp-proxy.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/usr/bin/env python3
  2. import sys
  3. import re
  4. from twisted.internet import defer
  5. from twisted.internet import protocol
  6. from twisted.internet import reactor
  7. from twisted.python import log
  8. from twisted.enterprise import adbapi
  9. import pendulum
  10. # Connect to:
  11. HOST = "127.0.0.1"
  12. PORT = 2002
  13. # Listen on:
  14. LISTEN_PORT = 9999
  15. LISTEN_ON = "127.0.0.1"
  16. cleaner = re.compile(r"\x1b\[[0-9;]*[A-Zm]")
  17. makeNL = re.compile(r"\x1b\[[0-9;]*[J]")
  18. def treatAsNL(line):
  19. global makeNL
  20. return makeNL.sub("\n", line)
  21. def cleanANSI(line):
  22. global cleaner
  23. return cleaner.sub("", line)
  24. # return re.sub(r'\x1b\[([0-9,A-Z]{1,2}(;[0-9]{1,2})?(;[0-9]{3})?)?[m|K]?', '', line)
  25. class PlayerProtocol(protocol.Protocol):
  26. def __init__(self):
  27. self.user = None
  28. self.dbinit = False
  29. self.db = None
  30. self.buffer = ""
  31. def connectionMade(self):
  32. log.msg("Client: connected to peer")
  33. self.queue_twgs = self.factory.queue_twgs
  34. self.queue_twgs.get().addCallback(self.serverDataReceived)
  35. def serverDataReceived(self, chunk):
  36. # rlogin looks like this: \x00 password \x00 username \x00 terminal/speed \x00
  37. # b'\x00up2lat3\x00bugz\x00ansi-bbs/115200\x00'
  38. # We're looking for 4 \x00!
  39. # TODO: Line processing, and line cleaning (remove ANSI color codes)
  40. if chunk is False:
  41. self.queue_twgs = None
  42. log.msg("Client: disconnecting from peer")
  43. self.factory.continueTrying = False
  44. self.transport.loseConnection()
  45. else:
  46. # self.buffer += chunk.decode("utf-8", 'ignore')
  47. if self.user is None:
  48. self.buffer += chunk.decode("utf-8", "ignore")
  49. # Ok, process this
  50. # self.buffer += chunk.decode('utf-8')
  51. # We don't have the username yet
  52. parts = self.buffer.split("\x00")
  53. if len(parts) >= 5:
  54. # Got it!
  55. self.user = parts[1]
  56. log.msg("User: {0}".format(self.user))
  57. # Reset buffer -- remove everything before last \x00
  58. zpos = self.buffer.rindex("\x00")
  59. self.buffer = self.buffer[zpos + 1 :]
  60. self.buffer = ""
  61. # init sqlite db using the username
  62. self.factory.getUser(self.user)
  63. # else:
  64. # process the buffer
  65. # Handle backspaces by deleting previous character.
  66. # Send the received data into the linereader for "automatic" line processing.
  67. #
  68. #
  69. # Strip out ANSI color codes
  70. # self.buffer = re.sub(r'\x1b[\d;?\d+m', '', self.buffer)
  71. # Process lines ...
  72. self.transport.write(chunk)
  73. self.queue_twgs.get().addCallback(self.serverDataReceived)
  74. # elif b"$" == chunk:
  75. # self.factory.svr_queue.put(b"HELLO.\r\n")
  76. # self.cli_queue.get().addCallback(self.serverDataReceived)
  77. # elif self.cli_queue:
  78. # log.msg("Client: writing %d bytes to peer" % len(chunk))
  79. # log.msg(">>", repr(chunk))
  80. # self.transport.write(chunk)
  81. # self.cli_queue.get().addCallback(self.serverDataReceived)
  82. # else:
  83. # self.factory.cli_queue.put(chunk)
  84. def dataReceived(self, chunk):
  85. # log.msg("Client: %d bytes received from peer" % len(chunk))
  86. # clean, strip ANSI, etc.
  87. # log.msg("<<", chunk.decode("utf-8", "ignore"))
  88. # log.msg("<<", repr(chunk))
  89. self.factory.queue_client.put(chunk)
  90. def connectionLost(self, why):
  91. log.msg("connectionLost because: %s" % why)
  92. # if self.cli_queue:
  93. # self.cli_queue = None
  94. # log.msg("Client: peer disconnect unexpectedly")
  95. self.factory.queue_client.put(False)
  96. self.queue_twgs = None
  97. self.transport.loseConnection()
  98. class GlueFactory(protocol.ClientFactory):
  99. # class GlueFactory(protocol.Factory):
  100. maxDelay = 10
  101. protocol = PlayerProtocol
  102. def __init__(self, queue_client, queue_twgs, twgs):
  103. self.queue_client = queue_client
  104. self.queue_twgs = queue_twgs
  105. self.twgs = twgs
  106. self.fpRaw = None
  107. self.fpLines = None
  108. def closeIt(self):
  109. log.msg("closeIt")
  110. self.queue_client.put(False)
  111. def getUser(self, user):
  112. log.msg("getUser( %s )" % user)
  113. self.twgs.logUser(user)
  114. # This was needed when I replaced ClientFactory with Factory.
  115. # def clientConnectionLost(self, connector, why):
  116. # log.msg("clientconnectionlost: %s" % why)
  117. # self.queue_client.put(False)
  118. def clientConnectionFailed(self, connector, why):
  119. log.msg("connectionFailed: %s" % why)
  120. self.queue_client.put(b"Sorry! I'm Unable to connect to the game server.\r\n")
  121. # syncterm gets cranky/locks up if we close this here.
  122. # (Because it is still sending rlogin information?)
  123. reactor.callLater(2, self.closeIt)
  124. # ProxyServer is created for each connection
  125. class TWGSServer(protocol.Protocol):
  126. def __init__(self):
  127. self.buffer = ""
  128. self.fpRaw = None
  129. self.fpLines = None
  130. def connectionMade(self):
  131. self.queue_twgs = defer.DeferredQueue()
  132. self.queue_client = defer.DeferredQueue()
  133. self.queue_client.get().addCallback(self.clientDataReceived)
  134. factory = GlueFactory(self.queue_client, self.queue_twgs, self)
  135. reactor.connectTCP(HOST, PORT, factory, 5)
  136. def logUser(self, user):
  137. now = pendulum.now()
  138. filename = now.format("YYYY-MM-DD_HHmm") + "-" + user.lower()
  139. self.fpRaw = open(filename + ".raw", "ab")
  140. self.fpLines = open(filename + ".lines", "a")
  141. # print("Log created:", now.to_rss_string(), "\n", file=self.fpRaw)
  142. print("Log created:", now.to_rss_string(), "\n", file=self.fpLines)
  143. def gotLine(self, line):
  144. # log.msg(">>> [{0}]".format(line.decode("utf-8", "ignore")))
  145. log.msg(">>> [{0}]".format(line))
  146. if self.fpLines is not None:
  147. print(line, file=self.fpLines)
  148. def clientDataReceived(self, chunk):
  149. if chunk is False:
  150. self.transport.loseConnection()
  151. else:
  152. if self.fpRaw is not None:
  153. self.fpRaw.write(chunk)
  154. self.buffer += chunk.decode("utf-8", "ignore")
  155. # Process any backspaces in the buffer
  156. while "\x08" in self.buffer:
  157. part = self.buffer.partition("\x08")
  158. self.buffer = part[0][:-1] + part[2]
  159. # Treat some ANSI codes as a newline (for purposes of Lines)
  160. self.buffer = treatAsNL(self.buffer)
  161. # Break the buffer into lines
  162. while "\n" in self.buffer:
  163. part = self.buffer.partition("\n")
  164. line = part[0].replace("\r", "")
  165. # Clean ANSI codes from the line
  166. line = cleanANSI(line)
  167. self.gotLine(line)
  168. self.buffer = part[2]
  169. # log.msg("Server: writing %d bytes to original client" % len(chunk))
  170. self.transport.write(chunk)
  171. self.queue_client.get().addCallback(self.clientDataReceived)
  172. def dataReceived(self, chunk):
  173. # log.msg("Server: %d bytes received" % len(chunk))
  174. self.queue_twgs.put(chunk)
  175. def connectionLost(self, why):
  176. log.msg("lost connection %s" % why)
  177. self.queue_twgs.put(False)
  178. if self.fpRaw is not None:
  179. self.fpRaw.close()
  180. self.fpRaw = None
  181. if self.fpLines is not None:
  182. if self.buffer != "":
  183. print(self.buffer, file=self.fpLines)
  184. self.fpLines.close()
  185. self.fpLines = None
  186. def connectionFailed(self, why):
  187. log.msg("connectionFailed: %s" % why)
  188. if __name__ == "__main__":
  189. log.startLogging(sys.stdout)
  190. factory = protocol.Factory()
  191. factory.protocol = TWGSServer
  192. reactor.listenTCP(LISTEN_PORT, factory, interface=LISTEN_ON)
  193. reactor.run()