Example #1
0
    def dataReceived(self, data):
        """
        Handle incoming data over the wire.

        This method will split the incoming data depending on if it
        starts with IAC (a telnet command) or not. All other data will
        be handled in line mode. Some clients also sends an erroneous
        line break after IAC, which we must watch out for.

        Args:
            data (str): Incoming data.

        Notes:
            OOB protocols (MSDP etc) already intercept subnegotiations on
            their own, never entering this method. They will relay their
            parsed data directly to self.data_in.

        """
        if data and data[0] == IAC or self.iaw_mode:
            try:
                super(TelnetProtocol, self).dataReceived(data)
                if len(data) == 1:
                    self.iaw_mode = True
                else:
                    self.iaw_mode = False
                return
            except Exception as err1:
                conv = ""
                try:
                    for b in data:
                        conv += " " + repr(ord(b))
                except Exception as err2:
                    conv = str(err2) + ":", str(data)
                out = "Telnet Error (%s): %s (%s)" % (err1, data, conv)
                logger.log_trace(out)
                return

        if data and data.strip() == NULL:
            # This is an ancient type of keepalive still used by some
            # legacy clients. There should never be a reason to send
            # a lone NULL character so this seems ok to support for
            # backwards compatibility.
            data = _IDLE_COMMAND

        if self.no_lb_mode and _RE_LEND.search(data):
            # we are in no_lb_mode and receive a line break;
            # this means we should empty the buffer and send
            # the command.
            data = "".join(self.line_buffer) + data
            data = data.rstrip("\r\n") + "\n"
            self.line_buffer = []
            self.no_lb_mode = False
        elif not _RE_LEND.search(data):
            # no line break at the end of the command, buffer instead.
            self.line_buffer.append(data)
            self.no_lb_mode = True
            return

        # if we get to this point the command should end with a linebreak.
        StatefulTelnetProtocol.dataReceived(self, data)
Example #2
0
    def dataReceived(self, data):
        """
        Handle incoming data over the wire.

        This method will split the incoming data depending on if it
        starts with IAC (a telnet command) or not. All other data will
        be handled in line mode. Some clients also sends an erroneous
        line break after IAC, which we must watch out for.

        Args:
            data (str): Incoming data.

        Notes:
            OOB protocols (MSDP etc) already intercept subnegotiations on
            their own, never entering this method. They will relay their
            parsed data directly to self.data_in.

        """
        if data and data[0] == IAC or self.iaw_mode:
            try:
                super(TelnetProtocol, self).dataReceived(data)
                if len(data) == 1:
                    self.iaw_mode = True
                else:
                    self.iaw_mode = False
                return
            except Exception as err1:
                conv = ""
                try:
                    for b in data:
                        conv += " " + repr(ord(b))
                except Exception as err2:
                    conv = str(err2) + ":", str(data)
                out = "Telnet Error (%s): %s (%s)" % (err1, data, conv)
                logger.log_trace(out)
                return

        if data and data.strip() == NULL:
            # This is an ancient type of keepalive still used by some
            # legacy clients. There should never be a reason to send
            # a lone NULL character so this seems ok to support for
            # backwards compatibility.
            data = _IDLE_COMMAND

        if self.no_lb_mode and _RE_LEND.search(data):
            # we are in no_lb_mode and receive a line break;
            # this means we should empty the buffer and send
            # the command.
            data = "".join(self.line_buffer) + data
            data = data.rstrip("\r\n") + "\n"
            self.line_buffer = []
            self.no_lb_mode = False
        elif not _RE_LEND.search(data):
            # no line break at the end of the command, buffer instead.
            self.line_buffer.append(data)
            self.no_lb_mode = True
            return

        # if we get to this point the command should end with a linebreak.
        StatefulTelnetProtocol.dataReceived(self, data)
Example #3
0
    def dataReceived(self, data):
        """
        This method will split the incoming data depending on if it
        starts with IAC (a telnet command) or not. All other data will
        be handled in line mode. Some clients also sends an erroneous
        line break after IAC, which we must watch out for.
        """
        #print "dataRcv (%s):" % data,
        #try:
        #    for b in data:
        #        print ord(b),
        #    print ""
        #except Exception, e:
        #    print str(e) + ":", str(data)

        if data and data[0] == IAC or self.iaw_mode:
            try:
                #print "IAC mode"
                super(TelnetProtocol, self).dataReceived(data)
                if len(data) == 1:
                    self.iaw_mode = True
                else:
                    self.iaw_mode = False
                return
            except Exception:
                logger.log_trace()
        # if we get to this point the command must end with a linebreak.
        # We make sure to add it, to fix some clients messing this up.
        data = data.rstrip("\r\n") + "\n"
        #print "line data in:", repr(data)
        StatefulTelnetProtocol.dataReceived(self, data)
Example #4
0
 def connectionLost(self, reason):
     print "Connection lost."
     StatefulTelnetProtocol.connectionLost(self, reason)
     try:
         # noinspection PyUnresolvedReferences
         reactor.stop()
     except ReactorNotRunning:
         pass
Example #5
0
    def dataReceived(self, data):
        """
        Handle incoming data over the wire.

        This method will split the incoming data depending on if it
        starts with IAC (a telnet command) or not. All other data will
        be handled in line mode. Some clients also sends an erroneous
        line break after IAC, which we must watch out for.

        Args:
            data (str): Incoming data.

        Notes:
            OOB protocols (MSDP etc) already intercept subnegotiations on
            their own, never entering this method. They will relay their
            parsed data directly to self.data_in.

        """
        if data and data[0] == IAC or self.iaw_mode:
            try:
                super(TelnetProtocol, self).dataReceived(data)
                if len(data) == 1:
                    self.iaw_mode = True
                else:
                    self.iaw_mode = False
                return
            except Exception as err1:
                conv = ""
                try:
                    for b in data:
                        conv += " " + repr(ord(b))
                except Exception as err2:
                    conv = str(err2) + ":", str(data)
                out = "Telnet Error (%s): %s (%s)" % (err1, data, conv)
                logger.log_trace(out)
                return

        if self.no_lb_mode and _RE_LEND.match(data):
            # we are in no_lb_mode and we get a single line break
            # - this line break should have come with the previous
            # command - it was already added so we drop it here
            self.no_lb_mode = False
            return
        elif not _RE_LEND.search(data):
            # no line break at the end of the command, note this.
            data = data.rstrip("\r\n") + "\n"
            self.no_lb_mode = True

        # if we get to this point the command should end with a linebreak.
        # We make sure to add it, to fix some clients messing this up.
        StatefulTelnetProtocol.dataReceived(self, data)
Example #6
0
    def dataReceived(self, data):
        """
        Handle incoming data over the wire.

        This method will split the incoming data depending on if it
        starts with IAC (a telnet command) or not. All other data will
        be handled in line mode. Some clients also sends an erroneous
        line break after IAC, which we must watch out for.

        Args:
            data (str): Incoming data.

        Notes:
            OOB protocols (MSDP etc) already intercept subnegotiations on
            their own, never entering this method. They will relay their
            parsed data directly to self.data_in.

        """
        if data and data[0] == IAC or self.iaw_mode:
            try:
                super(TelnetProtocol, self).dataReceived(data)
                if len(data) == 1:
                    self.iaw_mode = True
                else:
                    self.iaw_mode = False
                return
            except Exception as err1:
                conv = ""
                try:
                    for b in data:
                        conv += " " + repr(ord(b))
                except Exception as err2:
                    conv = str(err2) + ":", str(data)
                out = "Telnet Error (%s): %s (%s)" % (err1, data, conv)
                logger.log_trace(out)
                return

        if self.no_lb_mode and _RE_LEND.match(data):
            # we are in no_lb_mode and we get a single line break
            # - this line break should have come with the previous
            # command - it was already added so we drop it here
            self.no_lb_mode = False
            return
        elif not _RE_LEND.search(data):
            # no line break at the end of the command, note this.
            data = data.rstrip("\r\n") + "\n"
            self.no_lb_mode = True

        # if we get to this point the command should end with a linebreak.
        # We make sure to add it, to fix some clients messing this up.
        StatefulTelnetProtocol.dataReceived(self, data)
Example #7
0
 def connectionLost(self, reason):
     StatefulTelnetProtocol.connectionLost(self, reason)
     if self.from_addr is not None:
         self.vumi_transport.deregister_client(self)
Example #8
0
        if self.no_lb_mode and _RE_LEND.match(data):
            # we are in no_lb_mode and we get a single line break
            # - this line break should have come with the previous
            # command - it was already added so we drop it here
            self.no_lb_mode = False
            return
        elif not _RE_LEND.search(data):
            # no line break at the end of the command, note this.
            data = data.rstrip("\r\n") + "\n"
            self.no_lb_mode = True

        # if we get to this point the command should end with a linebreak.
        # We make sure to add it, to fix some clients messing this up.
        #print "line data in:", repr(data)
        StatefulTelnetProtocol.dataReceived(self, data)

    def _write(self, data):
        "hook overloading the one used in plain telnet"
        # print "_write (%s): %s" % (self.state,  " ".join(str(ord(c)) for c in data))
        data = data.replace('\n', '\r\n').replace('\r\r\n', '\r\n')
        #data = data.replace('\n', '\r\n')
        super(TelnetProtocol, self)._write(mccp_compress(self, data))

    def sendLine(self, line):
        "hook overloading the one used by linereceiver"
        #print "sendLine (%s):\n%s" % (self.state, line)
        #escape IAC in line mode, and correctly add \r\n
        line += self.delimiter
        line = line.replace(IAC, IAC + IAC).replace('\n', '\r\n')
        return self.transport.write(mccp_compress(self, line))
Example #9
0
 def lineReceived(self, p_line):
     StatefulTelnetProtocol.lineReceived(self, p_line)
     LOG.info('Line Received.\n\tData:{}'.format(p_line))
Example #10
0
 def connectionLost(self, reason):
     StatefulTelnetProtocol.connectionLost(self, reason)
     if self.from_addr is not None:
         self.vumi_transport.deregister_client(self)
Example #11
0
                return
            except Exception, err1:
                conv = ""
                try:
                    for b in data:
                        conv += " " + repr(ord(b))
                except Exception, err2:
                    conv = str(err2) + ":", str(data)
                out = "Telnet Error (%s): %s (%s)" % (err1, data, conv)
                logger.log_trace(out)
                return
        # if we get to this point the command must end with a linebreak.
        # We make sure to add it, to fix some clients messing this up.
        data = data.rstrip("\r\n") + "\n"
        #print "line data in:", repr(data)
        StatefulTelnetProtocol.dataReceived(self, data)

    def _write(self, data):
        "hook overloading the one used in plain telnet"
        # print "_write (%s): %s" % (self.state,  " ".join(str(ord(c)) for c in data))
        data = data.replace('\n', '\r\n').replace('\r\r\n', '\r\n')
        #data = data.replace('\n', '\r\n')
        super(TelnetProtocol, self)._write(mccp_compress(self, data))

    def sendLine(self, line):
        "hook overloading the one used by linereceiver"
        #print "sendLine (%s):\n%s" % (self.state, line)
        #escape IAC in line mode, and correctly add \r\n
        line += self.delimiter
        line = line.replace(IAC, IAC + IAC).replace('\n', '\r\n')
        return self.transport.write(mccp_compress(self, line))
Example #12
0
 def sendLine(self, line):
     # Override, perform filters and fall back to original functionality
     StatefulTelnetProtocol.sendLine(self, line)
Example #13
0
 def unhandledSubnegotiation(self, command, by):
     rr = StatefulTelnetProtocol.unhandledSubnegotiation(self, command, by)
     logger.log("SUB NEG Command: %s  %s" % (command, by))
     return rr