Ver código fonte

Trying, and failing. Need observer pattern.

Steve Thielemann 5 anos atrás
pai
commit
8413ead7a6
1 arquivos alterados com 68 adições e 17 exclusões
  1. 68 17
      tcp-proxy.py

+ 68 - 17
tcp-proxy.py

@@ -15,11 +15,10 @@ RAW = True
 
 try:
     from config_dev import *
-except ModuleNotFoundError as E:
+except ModuleNotFoundError:
     from config import *
 
 # from config import *
-# from config_dev import *
 
 # Connect to:
 # HOST = "twgs"
@@ -80,8 +79,8 @@ class PlayerProtocol(protocol.Protocol):
             self.factory.continueTrying = False
             self.transport.loseConnection()
         else:
-            # self.buffer += chunk.decode("utf-8", 'ignore')
             if self.user is None:
+                # Decode the rlogin data
                 self.buffer += chunk.decode("utf-8", "ignore")
 
                 # Ok, process this
@@ -99,6 +98,7 @@ class PlayerProtocol(protocol.Protocol):
                     # init sqlite db using the username
                     self.factory.getUser(self.user)
 
+            # Pass received data to the server
             self.transport.write(chunk)
             self.queue_twgs.get().addCallback(self.serverDataReceived)
 
@@ -155,12 +155,14 @@ class GlueFactory(protocol.ClientFactory):
 
 
 class ProxyAction:
-    def __init__(self, queue_client, queue_twgs):
+    def __init__(self, twgs):
         self.active = False
-        self.queue_client = queue_client
-        self.queue_twgs = queue_twgs
+        self.twgs = twgs
+        self.queue_client = twgs.queue_client
+        self.queue_twgs = twgs.queue_twgs
         self.buffer = ""
         self.prompt = ""
+        self.rptstate = 0
 
     def isActive(self):
         return self.active
@@ -170,18 +172,48 @@ class ProxyAction:
             self.queue_twgs.put(b" ")
             reactor.callLater(30, self.keepAlive)
 
-    def activate(self, prompt):
-        self.active = True
-        self.prompt = prompt
+    def menu(self):
         self.send("\r\n**********\r\nTWGS Proxy ready...\r\n")
-        self.send("Prompt seen: [{0}] ...\r\n".format(self.prompt))
         self.send("(T) Display Time\r\n")
+        self.send("(P) CIM Port Report\r\n")
         self.send("(Q) Quit\r\n")
         self.send("  --==> ")
-        reactor.callLater(30, self.keepAlive)
+
+    def activate(self, prompt):
+        cleaned = cleanANSI(prompt)
+        if cleaned.startswith("Command [TL=0"):
+            self.active = True
+            self.prompt = prompt
+            self.menu()
+            reactor.callLater(30, self.keepAlive)
+        else:
+            self.send("\a")
+
+    def sendtwgs(self, text):
+        self.queue_twgs.put(text.encode())
 
     def server(self, line):
-        pass
+        # 2019-11-20 18:37:28-0500 [PlayerProtocol,client] >>> [: ]
+        # 2019-11-20 18:37:28-0500 [PlayerProtocol,client] >>> [ 436   2870 100% - 1520 100% - 2820 100% ]
+        # 2019-11-20 18:37:28-0500 [PlayerProtocol,client] >>> []
+        # 2019-11-20 18:37:42-0500 [PlayerProtocol,client] >>> [: ENDINTERROG]
+        # 2019-11-20 18:37:42-0500 [PlayerProtocol,client] >>> []
+
+        if self.rptstate == 1:
+            if line.startswith(":"):
+                self.sendtwgs("R")
+                self.rptstate == 2
+        if self.rptstate == 2:
+            if line.startswith(":"):
+                self.sendtwgs("Q")
+                self.rptstate == 3
+            else:
+                log.msg("[[{0}]]".format(line))
+        if self.rptstate == 3:
+            if line == ": ENDINTERROG":
+                self.rptstate == 3
+                self.twgs.passon = True
+                self.menu()
 
     def send(self, text):
         self.queue_client.put((text.encode(),))
@@ -191,11 +223,19 @@ class ProxyAction:
         text = chunk.decode("utf-8", "ignore").upper()
 
         if text == "T":
-            now = pendulum.now('America/New_York')
+            now = pendulum.now("America/New_York")
             self.send("\r\nThe time is: {0}.\r\n".format(now.to_rss_string()))
+
+        if text == "P":
+            # Port Report
+            self.sendtwgs("^")
+            self.twgs.passon = False
+            self.rptstate = 1
+
         if text == "Q":
             self.send("\r\nReturning to TWGS.\r\n{0}".format(self.prompt))
             self.active = False
+            self.twgs.passon = True
 
 
 class TWGSServer(protocol.Protocol):
@@ -206,12 +246,13 @@ class TWGSServer(protocol.Protocol):
         self.action = None
         self.user = ""
         self.game = ""
+        self.passon = True
 
     def connectionMade(self):
         self.queue_twgs = defer.DeferredQueue()
         self.queue_client = defer.DeferredQueue()
         self.queue_client.get().addCallback(self.clientDataReceived)
-        self.action = ProxyAction(self.queue_client, self.queue_twgs)
+        self.action = ProxyAction(self)
 
         factory = GlueFactory(self.queue_client, self.queue_twgs, self)
         reactor.connectTCP(HOST, PORT, factory, 5)
@@ -226,6 +267,12 @@ class TWGSServer(protocol.Protocol):
         # print("Log created:", now.to_rss_string(), "\n", file=self.fpRaw)
         print("Log created:", now.to_rss_string(), "\n", file=self.fpLines)
 
+    def setGame(self, game):
+        if self.game != game:
+            log.msg("USER {0} ENTERED {1}".format(self.user, self.game))
+            self.data = {}
+        self.game = game
+
     def gotLine(self, line):
         # log.msg(">>> [{0}]".format(line.decode("utf-8", "ignore")))
         log.msg(">>> [{0}]".format(line))
@@ -250,8 +297,10 @@ class TWGSServer(protocol.Protocol):
         if "Selection (? for menu): " in line:
             game = line[-1]
             if game >= "A" and game < "Q":
-                self.game = game
-                log.msg("USER {0} ENTERED {1}".format(self.user, self.game))
+                self.setGame(game)
+        # If we're not passing it on to the user, we better be looking at it.
+        if self.action and not self.passon:
+            self.action.server(line)
 
     def clientDataReceived(self, chunk):
         if chunk is False:
@@ -288,7 +337,9 @@ class TWGSServer(protocol.Protocol):
                     self.buffer = part[2]
 
                 # log.msg("Server: writing %d bytes to original client" % len(chunk))
-                self.transport.write(chunk)
+                if self.passon:
+                    self.transport.write(chunk)
+
                 self.queue_client.get().addCallback(self.clientDataReceived)
 
     def dataReceived(self, chunk):