|
@@ -1,19 +1,30 @@
|
|
|
|
|
|
|
|
|
import sys
|
|
|
+import re
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
from twisted.internet import protocol
|
|
|
from twisted.internet import reactor
|
|
|
from twisted.python import log
|
|
|
+from twisted.enterprise import adbapi
|
|
|
|
|
|
-HOST = '127.0.0.1'
|
|
|
+
|
|
|
+HOST = "127.0.0.1"
|
|
|
PORT = 2002
|
|
|
+
|
|
|
LISTEN_PORT = 9999
|
|
|
-LISTEN_ON = '127.0.0.1'
|
|
|
+LISTEN_ON = "127.0.0.1"
|
|
|
|
|
|
|
|
|
class ProxyClientProtocol(protocol.Protocol):
|
|
|
+ def __init__(self):
|
|
|
+ self.user = None
|
|
|
+ self.dbinit = False
|
|
|
+ self.db = None
|
|
|
+ self.buffer = ""
|
|
|
+ self.lines = list()
|
|
|
+
|
|
|
def connectionMade(self):
|
|
|
log.msg("Client: connected to peer")
|
|
|
self.cli_queue = self.factory.cli_queue
|
|
@@ -23,34 +34,70 @@ class ProxyClientProtocol(protocol.Protocol):
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
if chunk is False:
|
|
|
self.cli_queue = None
|
|
|
log.msg("Client: disconnecting from peer")
|
|
|
self.factory.continueTrying = False
|
|
|
self.transport.loseConnection()
|
|
|
- elif b'\x00' in chunk:
|
|
|
-
|
|
|
- log.msg("Maybe RLOGIN?")
|
|
|
- parts = chunk.split(b'\x00')
|
|
|
- user = parts[1].decode('utf-8')
|
|
|
- log.msg("GREETINGS {0}!".format(user))
|
|
|
-
|
|
|
- self.transport.write(chunk)
|
|
|
- self.cli_queue.get().addCallback(self.serverDataReceived)
|
|
|
- elif b"$" == chunk:
|
|
|
- self.factory.svr_queue.put(b"HELLO.\r\n")
|
|
|
- self.cli_queue.get().addCallback(self.serverDataReceived)
|
|
|
- elif self.cli_queue:
|
|
|
- log.msg("Client: writing %d bytes to peer" % len(chunk))
|
|
|
- log.msg(">>", repr(chunk))
|
|
|
+ else:
|
|
|
+ self.buffer += chunk.decode("utf-8")
|
|
|
+ if self.user is None:
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ parts = self.buffer.split("\x00")
|
|
|
+ if len(parts) >= 5:
|
|
|
+
|
|
|
+ self.user = parts[1]
|
|
|
+ log.msg("User: {0}".format(self.user))
|
|
|
+
|
|
|
+ zpos = self.buffer.rindex("\x00")
|
|
|
+ self.buffer = self.buffer[zpos + 1 :]
|
|
|
+
|
|
|
+ else:
|
|
|
+
|
|
|
+
|
|
|
+ pos = self.buffer.find("\x08")
|
|
|
+ while pos != -1:
|
|
|
+
|
|
|
+ self.buffer = self.buffer[0 : pos - 1] + self.buffer[pos + 1 :]
|
|
|
+ pos = self.buffer.find("\x08")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ pos = self.buffer.find("\r")
|
|
|
+ while pos != -1:
|
|
|
+ line = self.buffer[0:pos]
|
|
|
+ self.lines.append(line)
|
|
|
+ log.msg("L> {0}".format(line))
|
|
|
+ self.buffer = self.buffer[pos + 1 :]
|
|
|
+ pos = self.buffer.find("\r")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
self.transport.write(chunk)
|
|
|
self.cli_queue.get().addCallback(self.serverDataReceived)
|
|
|
- else:
|
|
|
- self.factory.cli_queue.put(chunk)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
def dataReceived(self, chunk):
|
|
|
- log.msg("Client: %d bytes received from peer" % len(chunk))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
log.msg("<<", repr(chunk))
|
|
|
self.factory.svr_queue.put(chunk)
|
|
|
|
|
@@ -63,17 +110,19 @@ class ProxyClientProtocol(protocol.Protocol):
|
|
|
self.cli_queue = None
|
|
|
self.transport.loseConnection()
|
|
|
|
|
|
-
|
|
|
-class ProxyClientFactory(protocol.ClientFactory):
|
|
|
+
|
|
|
+class ProxyClientFactory(protocol.ClientFactory):
|
|
|
maxDelay = 10
|
|
|
-
|
|
|
-
|
|
|
protocol = ProxyClientProtocol
|
|
|
|
|
|
def __init__(self, svr_queue, cli_queue):
|
|
|
self.svr_queue = svr_queue
|
|
|
self.cli_queue = cli_queue
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
class ProxyServer(protocol.Protocol):
|
|
|
def connectionMade(self):
|
|
|
self.svr_queue = defer.DeferredQueue()
|
|
@@ -100,11 +149,9 @@ class ProxyServer(protocol.Protocol):
|
|
|
self.cli_queue.put(False)
|
|
|
|
|
|
|
|
|
-
|
|
|
if __name__ == "__main__":
|
|
|
log.startLogging(sys.stdout)
|
|
|
factory = protocol.Factory()
|
|
|
factory.protocol = ProxyServer
|
|
|
reactor.listenTCP(LISTEN_PORT, factory, interface=LISTEN_ON)
|
|
|
reactor.run()
|
|
|
-
|