Commit 39c9d17c authored by Denis Bilenko's avatar Denis Bilenko

backdoor: re-implement BackdoorServer as StreamServer subclass

parent 3b90a8ee
...@@ -27,6 +27,7 @@ from code import InteractiveConsole ...@@ -27,6 +27,7 @@ from code import InteractiveConsole
from gevent import socket from gevent import socket
from gevent.greenlet import Greenlet from gevent.greenlet import Greenlet
from gevent.server import StreamServer
try: try:
sys.ps1 sys.ps1
...@@ -40,11 +41,11 @@ except AttributeError: ...@@ -40,11 +41,11 @@ except AttributeError:
class SocketConsole(Greenlet): class SocketConsole(Greenlet):
def __init__(self, desc, locals): def __init__(self, locals, conn):
Greenlet.__init__(self) Greenlet.__init__(self)
self.locals = locals self.locals = locals
# mangle the socket # mangle the socket
self.desc = desc desc = self.desc = _fileobject(conn)
readline = desc.readline readline = desc.readline
self.old = {} self.old = {}
self.fixups = { self.fixups = {
...@@ -90,25 +91,15 @@ class SocketConsole(Greenlet): ...@@ -90,25 +91,15 @@ class SocketConsole(Greenlet):
self.finalize() self.finalize()
class BackdoorServer(Greenlet): class BackdoorServer(StreamServer):
def __init__(self, address, locals=None): def __init__(self, listener, locals=None, **server_args):
Greenlet.__init__(self) StreamServer.__init__(self, listener, spawn=None, **server_args)
if isinstance(address, socket.socket):
self.socket = address
else:
self.socket = socket.tcp_listener(address)
self.locals = locals self.locals = locals
# QQQ passing pool instance as 'spawn' is not possible; should it be fixed?
def __str__(self): def handle(self, conn, address):
return '<BackdoorServer on %s>' % (self.socket, ) SocketConsole.spawn(self.locals, conn)
def _run(self):
while True:
(conn, address) = self.socket.accept()
print 'accepted connection from %s' % (address, )
fileobj = _fileobject(conn)
SocketConsole.spawn(fileobj, self.locals)
class _fileobject(socket._fileobject): class _fileobject(socket._fileobject):
...@@ -121,10 +112,5 @@ if __name__ == '__main__': ...@@ -121,10 +112,5 @@ if __name__ == '__main__':
if not sys.argv[1:]: if not sys.argv[1:]:
print 'USAGE: %s PORT' % sys.argv[0] print 'USAGE: %s PORT' % sys.argv[0]
else: else:
server = BackdoorServer.spawn(('127.0.0.1', int(sys.argv[1]))) BackdoorServer(('127.0.0.1', int(sys.argv[1]))).serve_forever()
print server
try:
server.join()
except KeyboardInterrupt:
pass
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment