From 295c79d44824327c5bf3e21e4b776aa3793772c3 Mon Sep 17 00:00:00 2001 From: Griatch Date: Tue, 12 Nov 2013 17:07:35 +0100 Subject: [PATCH] Added a 1-second delay to initial connection in order to allow telnet custom protocol handshakes to complete before server sync. This is a dumb patch that works but it's hardly ideal - but without it, none of the telnet options are properly synced to the server. Making a callback chain for the various handhakes is not trivial, so using this until a more elegant solution can be found. --- src/server/portal/portalsessionhandler.py | 4 +++- src/server/portal/telnet.py | 23 +++++++++++++++++++---- src/server/portal/ttype.py | 11 ++++++----- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/server/portal/portalsessionhandler.py b/src/server/portal/portalsessionhandler.py index fffbf28c2..2418f2fc5 100644 --- a/src/server/portal/portalsessionhandler.py +++ b/src/server/portal/portalsessionhandler.py @@ -48,7 +48,9 @@ class PortalSessionHandler(SessionHandler): sessdata = session.get_sync_data() self.sessions[sessid] = session # sync with server-side - self.portal.amp_protocol.call_remote_ServerAdmin(sessid, + if self.portal.amp_protocol: # this is a timing issue + print "syncing", sessdata + self.portal.amp_protocol.call_remote_ServerAdmin(sessid, operation=PCONN, data=sessdata) def disconnect(self, session): diff --git a/src/server/portal/telnet.py b/src/server/portal/telnet.py index ca886690e..58a03b359 100644 --- a/src/server/portal/telnet.py +++ b/src/server/portal/telnet.py @@ -9,6 +9,7 @@ sessions etc. import re from twisted.conch.telnet import Telnet, StatefulTelnetProtocol, IAC, LINEMODE +from twisted.internet.defer import inlineCallbacks, returnValue from src.server.session import Session from src.server.portal import ttype, mssp, msdp from src.server.portal.mccp import Mccp, mccp_compress, MCCP @@ -33,16 +34,29 @@ class TelnetProtocol(Telnet, StatefulTelnetProtocol, Session): client_address = self.transport.client self.init_session("telnet", client_address, self.factory.sessionhandler) # negotiate mccp (data compression) - #self.mccp = Mccp(self) + self.mccp = Mccp(self) # negotiate ttype (client info) - #self.ttype = ttype.Ttype(self) + self.ttype = ttype.Ttype(self) # negotiate mssp (crawler communication) - #self.mssp = mssp.Mssp(self) + self.mssp = mssp.Mssp(self) # msdp self.msdp = msdp.Msdp(self) # add this new connection to sessionhandler so # the Server becomes aware of it. - self.sessionhandler.connect(self) + + # This is a fix to make sure the connection does not + # continue until the handshakes are done. This is a + # dumb delay of 1 second. This solution is not ideal (and + # potentially buggy for slow connections?) but + # adding a callback chain to all protocols (and notably + # to their handshakes, which in some cases are multi-part) + # is not trivial. Without it, the protocol will default + # to their defaults since sessionhandler.connect will sync + # before the handshakes have had time to finish. Keeping this patch + # until coming up with a more elegant solution /Griatch + from src.utils.utils import delay + delay(1, self, self.sessionhandler.connect) + #self.sessionhandler.connect(self) def enableRemote(self, option): """ @@ -187,4 +201,5 @@ class TelnetProtocol(Telnet, StatefulTelnetProtocol, Session): self.sendLine(text) else: # we need to make sure to kill the color at the end in order to match the webclient output. + #print "telnet data out:", self.protocol_flags, id(self.protocol_flags), id(self) self.sendLine(ansi.parse_ansi(_RE_N.sub("", text) + "{n", strip_ansi=nomarkup, xterm256=ttype.get('256 COLORS'))) diff --git a/src/server/portal/ttype.py b/src/server/portal/ttype.py index 6c72b3514..ffd7a2a94 100644 --- a/src/server/portal/ttype.py +++ b/src/server/portal/ttype.py @@ -73,7 +73,8 @@ class Ttype(object): certain piece of information about the client. All data is stored on protocol.protocol_flags under the TTYPE key. """ - if self.protocol.protocol_flags['TTYPE']['init_done']: + options = self.protocol.protocol_flags.get('TTYPE') + if options and options.get('init_done'): return self.ttype_step += 1 @@ -106,10 +107,10 @@ class Ttype(object): for codenum, standard in MTTS: if option == 0: break - status = option % codenum < option + status = option & codenum self.protocol.protocol_flags['TTYPE'][standard] = status - if status: - option = option % codenum + #if status: + # option = option % codenum self.protocol.protocol_flags['TTYPE']['init_done'] = True - #print "ttype results:", self.protocol.protocol_flags['TTYPE'] + #print "ttype results:", self.protocol.protocol_flags['TTYPE'],id(self.protocol)