Пример #1
0
    def got_handshake(self, data):
        if options.verbose > 2:
            logging.info('got handshake %s' % [data])
        self.handshake = parse_handshake(data)
        if options.verbose > 1:
            logging.info('parsed handshake %s' % [self.handshake])
        if self.handshake:
            self.peerid = self.handshake['peerid']
            self.peer = Peer.instantiate({'peerid':self.peerid})
            self.infohash = self.handshake['infohash']
            if not self.torrent:

                # check if this torrent is started, and in the current client's list of torrents!

                #self.torrent = Torrent.instantiate( binascii.hexlify(self.handshake['infohash']) )
                self.torrent = Torrent.instantiate( self.handshake['infohash'] )
                self.torrent.connections.append(self)
                logging.info('connection has torrent %s with hash %s%s' % (self.torrent, [self.torrent.hash], ' (with metadata)' if self.torrent.meta else ''))
                if not self._sent_handshake:
                    self.send_handshake()
                if not self._sent_extension_handshake:
                    self.send_extension_handshake()
                if self.torrent and self.torrent.meta:
                    self.send_bitmask()
                self.get_more_messages()
            else:
                self.get_more_messages()
        else:
            logging.info('invalid/unrecognized handshake')
            self.stream.close()
Пример #2
0
    def handle_pex(self):
        info = bencode.bdecode(self.request.payload[1:])
        if 'added' in info:
            num_added = len(info['added']) / 6

        peers = []
        for i in range(num_added):
            peerinfo = info['added'][6 * i:6 * (i + 1)]
            peer = Peer.instantiate({'compact': decode_peer(peerinfo)})
            peers.append(peer)

        if 'added_f' in info:
            for i in range(num_added):
                flags = list(bin(ord(info['added_f'][i]))[2:])
                flags = map(lambda x: x == '1', flags)
                peers[i].add_flags(flags)

        self.request.connection.torrent.handle_pex(peers, raw=info)