Browse Source

Filter out backspace. Break by line.

Steve Thielemann 5 years ago
parent
commit
c1e7ec26fb
1 changed files with 22 additions and 15 deletions
  1. 22 15
      tcp-proxy.py

+ 22 - 15
tcp-proxy.py

@@ -29,7 +29,6 @@ class PlayerProtocol(protocol.Protocol):
         self.queue_twgs = self.factory.queue_twgs
         self.queue_twgs.get().addCallback(self.serverDataReceived)
 
-
     def serverDataReceived(self, chunk):
         # rlogin looks like this:  \x00 password \x00 username \x00 terminal/speed \x00
         # b'\x00up2lat3\x00bugz\x00ansi-bbs/115200\x00'
@@ -43,7 +42,7 @@ class PlayerProtocol(protocol.Protocol):
         else:
             # self.buffer += chunk.decode("utf-8", 'ignore')
             if self.user is None:
-                self.buffer += chunk.decode("utf-8", 'ignore')
+                self.buffer += chunk.decode("utf-8", "ignore")
 
                 # Ok, process this
                 # self.buffer += chunk.decode('utf-8')
@@ -59,16 +58,16 @@ class PlayerProtocol(protocol.Protocol):
                     self.buffer = ""
                     # init sqlite db using the username
             # else:
-                # process the buffer
-                # Handle backspaces by deleting previous character.
+            # process the buffer
+            # Handle backspaces by deleting previous character.
 
-                # Send the received data into the linereader for "automatic" line processing.
+            # Send the received data into the linereader for "automatic" line processing.
 
-                #
-                #
-                # Strip out ANSI color codes
-                # self.buffer = re.sub(r'\x1b[\d;?\d+m', '', self.buffer)
-                # Process lines ...
+            #
+            #
+            # Strip out ANSI color codes
+            # self.buffer = re.sub(r'\x1b[\d;?\d+m', '', self.buffer)
+            # Process lines ...
 
             self.transport.write(chunk)
             self.queue_twgs.get().addCallback(self.serverDataReceived)
@@ -103,7 +102,7 @@ class PlayerProtocol(protocol.Protocol):
 
 
 class GlueFactory(protocol.ClientFactory):
-# class GlueFactory(protocol.Factory):
+    # class GlueFactory(protocol.Factory):
     maxDelay = 10
     protocol = PlayerProtocol
 
@@ -151,11 +150,19 @@ class TWGSServer(protocol.Protocol):
         if chunk is False:
             self.transport.loseConnection()
         else:
-            self.buffer += chunk.decode('utf-8', 'ignore')
+            self.buffer += chunk.decode("utf-8", "ignore")
+
+            # Process any backspaces in the buffer
+
+            while "\x08" in self.buffer:
+                part = self.buffer.partition("\x08")
+                self.buffer = part[0][:-1] + part[2]
 
-            while '\n' in self.buffer:
-                part = self.buffer.partition('\n')
-                line = part[0].replace('\r', '')
+            # Break the buffer into lines
+            
+            while "\n" in self.buffer:
+                part = self.buffer.partition("\n")
+                line = part[0].replace("\r", "")
                 self.gotLine(line)
                 self.buffer = part[2]