Beispiel #1
0
    def found_terminator(self):
        '''
        Invoked by asynchat when either the end of a packet header has
        been reached, or when the end of packet data has been reached.
        '''
        datastr = ''.join(self.data)

        # Flip between receive header/data...
        # either the entire header has just arrived
        if self.getting_header:
            self.getting_header = False

            self.header = to_storage(
                unpack_named(*(header_desc + tuple([datastr]))))
            if self.header.ymsg != 'YMSG' or self.header.size < 0:
                return log.warning('invalid packet')

            if self.header.size > 0:
                # Tell asynchat to read _size_ more bytes of data.
                self.set_terminator(self.header.size)
            else:
                # In this case, there is no data--handle the packet now.
                self.getting_header = True
                self.set_terminator(header_size)
                self.handle_packet(self.header, datastr)
                self.data = []

        # or else we just got the rest of the packet
        else:
            self.getting_header = True
            self.set_terminator(header_size)
            self.handle_packet(self.header, datastr[header_size:])
            self.data = []
Beispiel #2
0
    def s5b_ok(self):
        ok = yield (2, pack('BBB', 5,1,0))

        _head, authmethod = unpack('BB', ok)

        if authmethod != 0x00:
            raise ProxyFailure()

        out = pack('!BBBBB40sH', 0x05, 0x01, 0, 3, 40, self.hash, 0)

        in_fmt = ('!BBBB', 's5head', 'status', 'reserved', 'addrtype')
        in_ = yield (4, out)

        head = structures.unpack_named(*(in_fmt+ (in_,)))
        if head.addrtype == 3:
            head.addrlen = yield (1, '')
            head.addrlen = ord(head.addrlen)

            if head.addrlen > 0:
                address = yield (head.addrlen, '') #@UnusedVariable
            else:
                address = '' #@UnusedVariable
        _port =  yield (2, '')

        if head.status != 0x00:
            raise ProxyFailure()
Beispiel #3
0
    def s5b_ok(self):
        """generator conversation for S5B socket 'proxy' bytes.
        This is necessary because it must be done first, and must be done quickly.
        This function is really part of the socket setup, not part of the
        data which flows across."""
        ok = yield (2, pack('BBB', 5,1,0))
        _head, authmethod = unpack('BB', ok)

        if authmethod != 0x00:
            raise ProxyFailure()

        out = pack('!BBBBB40sH', 5,1,0,3, 40,self.hash,0)
        in_fmt = ('!BBBB', 's5head', 'status', 'reserved', 'addrtype')
        in_ = yield (4, out)
        head = structures.unpack_named(*(in_fmt+ (in_,)))
        if head.addrtype == 3:
            head.addrlen = yield (1, '')
            head.addrlen = ord(head.addrlen)
            if head.addrlen > 0:
                address = yield (head.addrlen, '') #@UnusedVariable
            else:
                address = '' #@UnusedVariable
        _port =  yield (2, '')
        if head.status != 0x00:
            raise ProxyFailure()
        log.info('proxy ok, calling on_success')
        self.event("connected")
Beispiel #4
0
    def found_terminator(self):
        '''
        Invoked by asynchat when either the end of a packet header has
        been reached, or when the end of packet data has been reached.
        '''
        datastr = ''.join(self.data)

        # Flip between receive header/data...
        # either the entire header has just arrived
        if self.getting_header:
            self.getting_header = False

            self.header = to_storage(unpack_named(
                                      *(header_desc + tuple([datastr]))))
            if self.header.ymsg != 'YMSG' or self.header.size < 0:
                return log.warning('invalid packet')

            if self.header.size > 0:
                # Tell asynchat to read _size_ more bytes of data.
                self.set_terminator(self.header.size)
            else:
                # In this case, there is no data--handle the packet now.
                self.getting_header = True
                self.set_terminator(header_size)
                self.handle_packet(self.header, datastr)
                self.data = []

        # or else we just got the rest of the packet
        else:
            self.getting_header = True
            self.set_terminator(header_size)
            self.handle_packet(self.header, datastr[header_size:])
            self.data = []
Beispiel #5
0
    def testUnpackNames(self):
        testdata = struct.pack("!HIB", 1, 4000L, 3) + "some extraneous data"
        magic_hash = structures.unpack_named("!HIBR", "one",
                                             "four thousand long", "three",
                                             "extra", testdata)

        self.assertEquals(magic_hash['extra'], 'some extraneous data')
Beispiel #6
0
    def testUnpackNames(self):
        testdata = struct.pack("!HIB", 1,4000L,3) + "some extraneous data"
        magic_hash = structures.unpack_named("!HIBR", "one", "four thousand long", "three", "extra", testdata)

        self.assertEquals(magic_hash['extra'], 'some extraneous data')