def get_scrape(self, paramslist): fs = {} if paramslist.has_key('info_hash'): if self.config['tracker_scrape_allowed'] not in [ITRACKSCRAPE_ALLOW_SPECIFIC, ITRACKSCRAPE_ALLOW_FULL]: return (400, 'Not Authorized', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, bencode({'failure reason': 'specific scrape function is not available with this tracker.'})) for hash in paramslist['info_hash']: if self.allowed is not None: if self.allowed.has_key(hash): fs[hash] = self.scrapedata(hash) elif self.downloads.has_key(hash): fs[hash] = self.scrapedata(hash) else: if self.config['tracker_scrape_allowed'] != ITRACKSCRAPE_ALLOW_FULL: return (400, 'Not Authorized', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, bencode({'failure reason': 'full scrape function is not available with this tracker.'})) if self.allowed is not None: keys = self.allowed.keys() else: keys = self.downloads.keys() for hash in keys: fs[hash] = self.scrapedata(hash) return (200, 'OK', {'Content-Type': 'text/plain'}, bencode({'files': fs}))
def check_allowed(self, infohash, paramslist): if self.aggregator_key is not None and not (paramslist.has_key('password') and paramslist['password'][0] == self.aggregator_key): return (200, 'Not Authorized', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, bencode({'failure reason': 'Requested download is not authorized for use with this tracker.'})) if self.allowed is not None: if not self.allowed.has_key(infohash): return (200, 'Not Authorized', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, bencode({'failure reason': 'Requested download is not authorized for use with this tracker.'})) if self.config['tracker_allowed_controls']: if self.allowed[infohash].has_key('failure reason'): return (200, 'Not Authorized', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, bencode({'failure reason': self.allowed[infohash]['failure reason']})) if paramslist.has_key('tracker'): if self.config['tracker_multitracker_allowed'] == ITRACKMULTI_ALLOW_NONE or paramslist['peer_id'][0] == self.trackerid: return (200, 'Not Authorized', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, bencode({'failure reason': 'disallowed'})) if self.config['tracker_multitracker_allowed'] == ITRACKMULTI_ALLOW_AUTODETECT and not self.allowed[infohash].has_key('announce-list'): return (200, 'Not Authorized', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, bencode({'failure reason': 'Requested download is not authorized for multitracker use.'}))
def natcheckOK(self, infohash, peerid, ip, port, not_seed): if DEBUG: print >> sys.stderr, 'tracker: natcheck: Recorded succes' bc = self.becache.setdefault(infohash, [[{}, {}], [{}, {}], [{}, {}]]) bc[0][not not_seed][peerid] = Bencached(bencode({'ip': ip, 'port': port, 'peer id': peerid})) bc[1][not not_seed][peerid] = Bencached(bencode({'ip': ip, 'port': port})) bc[2][not not_seed][peerid] = compact_peer_info(ip, port)
def check_allowed(self, infohash, paramslist): if self.aggregator_key is not None and not ( paramslist.has_key('password') and paramslist['password'][0] == self.aggregator_key): return ( 200, 'Not Authorized', { 'Content-Type': 'text/plain', 'Pragma': 'no-cache' }, bencode({ 'failure reason': 'Requested download is not authorized for use with this tracker.' })) if self.allowed is not None: if not self.allowed.has_key(infohash): return ( 200, 'Not Authorized', { 'Content-Type': 'text/plain', 'Pragma': 'no-cache' }, bencode({ 'failure reason': 'Requested download is not authorized for use with this tracker.' })) if self.config['tracker_allowed_controls']: if self.allowed[infohash].has_key('failure reason'): return (200, 'Not Authorized', { 'Content-Type': 'text/plain', 'Pragma': 'no-cache' }, bencode({ 'failure reason': self.allowed[infohash]['failure reason'] })) if paramslist.has_key('tracker'): if self.config[ 'tracker_multitracker_allowed'] == ITRACKMULTI_ALLOW_NONE or paramslist[ 'peer_id'][0] == self.trackerid: return (200, 'Not Authorized', { 'Content-Type': 'text/plain', 'Pragma': 'no-cache' }, bencode({'failure reason': 'disallowed'})) if self.config[ 'tracker_multitracker_allowed'] == ITRACKMULTI_ALLOW_AUTODETECT and not self.allowed[ infohash].has_key('announce-list'): return ( 200, 'Not Authorized', { 'Content-Type': 'text/plain', 'Pragma': 'no-cache' }, bencode({ 'failure reason': 'Requested download is not authorized for multitracker use.' }))
def gotUdpConnectRequest(self, permid, selversion, channel_id, mh_msg, reply_callback): if DEBUG: print >> sys.stderr, 'NatCheckMsgHandler: gotUdpConnectRequest from', show_permid_short(permid) try: mh_data = bdecode(mh_msg) except: print_exc() print >> sys.stderr, 'NatCheckMsgHandler: bad encoded data:', mh_msg return False if DEBUG: print >> sys.stderr, 'NatCheckMsgHandler: gotUdpConnectRequest is', mh_data try: request_id, host, port = mh_data.split(':') except: print_exc() print >> sys.stderr, 'NatCheckMsgHandler: error in received data:', mh_data return False coordinator = (host, int(port)) if DEBUG: print >> sys.stderr, 'NatCheckMsgHandler: coordinator address is', coordinator mhr_data = request_id + ':' + tryConnect(coordinator) try: mhr_msg = bencode(mhr_data) except: print_exc() print >> sys.stderr, 'NatCheckMsgHandler: error in encoding data:', mhr_data return False reply_callback(mhr_msg, callback=self.udpConnectReplySendCallback)
def send_overlap(self, permid, oldict): try: body = bencode(oldict) self.overlay_bridge.send(permid, SOCIAL_OVERLAP + body, self.send_callback) except: if DEBUG: print_exc(file=sys.stderr)
def serialize(self): lst = [self.torrent_id, self.torrent_pub_key, self.node_pub_key, self.expire_time, self.signature] return bencode(lst)
def make_torrent_file(input, userabortflag = None, userprogresscallback = lambda x: None): info, piece_length = makeinfo(input, userabortflag, userprogresscallback) if userabortflag is not None and userabortflag.isSet(): return (None, None) if info is None: return (None, None) check_info(info) metainfo = {'info': info, 'encoding': input['encoding']} if input['nodes'] is None and input['announce'] is None: raise ValueError('No tracker set') for key in ['announce', 'announce-list', 'nodes', 'comment', 'created by', 'httpseeds', 'url-list', 'authorized-peers']: if input.has_key(key) and input[key] is not None and len(input[key]) > 0: metainfo[key] = input[key] if key == 'comment': metainfo['comment.utf-8'] = uniconvert(input['comment'], 'utf-8') if input['torrentsigkeypairfilename'] is not None: from freestream.Core.Overlay.permid import create_torrent_signature create_torrent_signature(metainfo, input['torrentsigkeypairfilename']) if 'url-compat' in input: metainfo['info']['url-compat'] = input['url-compat'] if 'x-ts-properties' in input: metainfo['x-ts-properties'] = input['x-ts-properties'] if 'ogg-headers' in input: metainfo['ogg-headers'] = input['ogg-headers'] infohash = sha(bencode(info)).digest() return (infohash, metainfo)
def create_remote_query_reply(self, id, hits, selversion): getsize = os.path.getsize join = os.path.join d = {} d['id'] = id d2 = {} for torrent in hits: r = {} r['content_name'] = torrent['name'].encode('UTF-8') r['length'] = torrent['length'] r['leecher'] = torrent['num_leechers'] r['seeder'] = torrent['num_seeders'] r['category'] = torrent['category'] if selversion >= OLPROTO_VER_NINETH: if torrent['torrent_file_name']: file = join(self.torrent_dir, torrent['torrent_file_name']) if isfile(file): r['torrent_size'] = getsize(file) else: continue else: continue if selversion >= OLPROTO_VER_ELEVENTH: r['channel_permid'] = torrent['channel_permid'] r['channel_name'] = torrent['channel_name'].encode('UTF-8') if selversion >= OLPROTO_VER_TWELFTH and 'metadata' in torrent: if DEBUG: print >> sys.stderr, 'rqmh: create_query_reply: Adding torrent file' r['metatype'] = torrent['metatype'] r['metadata'] = torrent['metadata'] d2[torrent['infohash']] = r d['a'] = d2 return bencode(d)
def gotUdpConnectRequest(self, permid, selversion, channel_id, mh_msg, reply_callback): if DEBUG: print >> sys.stderr, 'NatCheckMsgHandler: gotUdpConnectRequest from', show_permid_short( permid) try: mh_data = bdecode(mh_msg) except: print_exc() print >> sys.stderr, 'NatCheckMsgHandler: bad encoded data:', mh_msg return False if DEBUG: print >> sys.stderr, 'NatCheckMsgHandler: gotUdpConnectRequest is', mh_data try: request_id, host, port = mh_data.split(':') except: print_exc() print >> sys.stderr, 'NatCheckMsgHandler: error in received data:', mh_data return False coordinator = (host, int(port)) if DEBUG: print >> sys.stderr, 'NatCheckMsgHandler: coordinator address is', coordinator mhr_data = request_id + ':' + tryConnect(coordinator) try: mhr_msg = bencode(mhr_data) except: print_exc() print >> sys.stderr, 'NatCheckMsgHandler: error in encoding data:', mhr_data return False reply_callback(mhr_msg, callback=self.udpConnectReplySendCallback)
def query_initiator(self, permid, selversion, request_callback): if DEBUG: print >> sys.stderr, 'FriendshipCrawler: friendship_query_initiator' get_last_updated_time = self.friendshipStatistics_db.getLastUpdateTimeOfThePeer(permid) msg_dict = {'current time': get_last_updated_time} msg = bencode(msg_dict) return request_callback(CRAWLER_FRIENDSHIP_STATS, msg)
def search_torrents(self, kws, maxhits = None, sendtorrents = False): if DEBUG: print >> sys.stderr, 'rquery: search for torrents matching', `kws` allhits = self.torrent_db.searchNames(kws, local=False) if maxhits is None: hits = allhits else: hits = allhits[:maxhits] colltorrdir = self.session.get_torrent_collecting_dir() if sendtorrents: print >> sys.stderr, 'rqmh: search_torrents: adding torrents' for hit in hits: filename = os.path.join(colltorrdir, hit['torrent_file_name']) try: tdef = TorrentDef.load(filename) if tdef.get_url_compat(): metatype = URL_MIME_TYPE metadata = tdef.get_url() else: metatype = TSTREAM_MIME_TYPE metadata = bencode(tdef.get_metainfo()) except: print_exc() metadata = None hit['metatype'] = metatype hit['metadata'] = metadata newhits = [] for hit in hits: if hit['metadata'] is not None: newhits.append(hit) hits = newhits return hits
def createAndSendBarterCastMessage(self, target_permid, selversion, active=False): if selversion <= OLPROTO_VER_FIFTH: return if DEBUG: print >> sys.stderr, '===========bartercast: Sending BarterCast msg to ', self.bartercastdb.getName( target_permid) bartercast_data = self.createBarterCastMessage(target_permid) if LOG: self.logMsg(bartercast_data, target_permid, 'out', logfile=self.logfile) try: bartercast_msg = bencode(bartercast_data) except: print_exc() print >> sys.stderr, 'error bartercast_data:', bartercast_data return self.overlay_bridge.send(target_permid, BARTERCAST + bartercast_msg, self.bartercastSendCallback) self.blockPeer(target_permid, self.send_block_list, self.block_interval)
def ValueToString(self, value, typex): if typex == 'boolean': if value: text = '1' else: text = '0' elif typex == 'color': red = str(value.Red()) while len(red) < 3: red = '0' + red green = str(value.Green()) while len(green) < 3: green = '0' + green blue = str(value.Blue()) while len(blue) < 3: blue = '0' + blue text = str(red) + str(green) + str(blue) elif typex.startswith('bencode'): text = bencode(value) elif type(value) is unicode: text = value else: text = str(value) return text
def search_torrents(self, kws, maxhits=None, sendtorrents=False): if DEBUG: print >> sys.stderr, 'rquery: search for torrents matching', ` kws ` allhits = self.torrent_db.searchNames(kws, local=False) if maxhits is None: hits = allhits else: hits = allhits[:maxhits] colltorrdir = self.session.get_torrent_collecting_dir() if sendtorrents: print >> sys.stderr, 'rqmh: search_torrents: adding torrents' for hit in hits: filename = os.path.join(colltorrdir, hit['torrent_file_name']) try: tdef = TorrentDef.load(filename) if tdef.get_url_compat(): metatype = URL_MIME_TYPE metadata = tdef.get_url() else: metatype = TSTREAM_MIME_TYPE metadata = bencode(tdef.get_metainfo()) except: print_exc() metadata = None hit['metatype'] = metatype hit['metadata'] = metadata newhits = [] for hit in hits: if hit['metadata'] is not None: newhits.append(hit) hits = newhits return hits
def _createSingleResponseMessage(self, responseData): orderedKeys = sorted(responseData['subtitles'].keys()) payload = list() for lang in orderedKeys: fileContent = responseData['subtitles'][lang] if fileContent is not None and len(fileContent) <= self._maxSubSize: payload.append(fileContent) else: print >> sys.stderr, 'Warning: Subtitle in % for ch: %s, infohash:%s dropped. Bigger then %d' % (lang, responseData['channel_id'], responseData['infohash'], self._maxSubSize) if not len(payload) > 0: if DEBUG: print >> sys.stderr, SUBS_LOG_PREFIX + 'No payload to send in SUBS' return bitmask = self._languagesUtility.langCodesToMask(orderedKeys) binaryBitmask = pack('!L', bitmask) header = (responseData['channel_id'], responseData['infohash'], binaryBitmask) message = bencode((header[0], header[1], header[2], payload)) return SUBS + message
def make_torrent_file(input, userabortflag=None, userprogresscallback=lambda x: None): info, piece_length = makeinfo(input, userabortflag, userprogresscallback) if userabortflag is not None and userabortflag.isSet(): return (None, None) if info is None: return (None, None) check_info(info) metainfo = {'info': info, 'encoding': input['encoding']} if input['nodes'] is None and input['announce'] is None: raise ValueError('No tracker set') for key in [ 'announce', 'announce-list', 'nodes', 'comment', 'created by', 'httpseeds', 'url-list', 'authorized-peers' ]: if input.has_key(key) and input[key] is not None and len( input[key]) > 0: metainfo[key] = input[key] if key == 'comment': metainfo['comment.utf-8'] = uniconvert(input['comment'], 'utf-8') if input['torrentsigkeypairfilename'] is not None: from freestream.Core.Overlay.permid import create_torrent_signature create_torrent_signature(metainfo, input['torrentsigkeypairfilename']) if 'url-compat' in input: metainfo['info']['url-compat'] = input['url-compat'] if 'x-ts-properties' in input: metainfo['x-ts-properties'] = input['x-ts-properties'] if 'ogg-headers' in input: metainfo['ogg-headers'] = input['ogg-headers'] infohash = sha(bencode(info)).digest() return (infohash, metainfo)
def natcheckOK(self, infohash, peerid, ip, port, not_seed): if DEBUG: print >> sys.stderr, 'tracker: natcheck: Recorded succes' bc = self.becache.setdefault(infohash, [[{}, {}], [{}, {}], [{}, {}]]) bc[0][not not_seed][peerid] = Bencached( bencode({ 'ip': ip, 'port': port, 'peer id': peerid })) bc[1][not not_seed][peerid] = Bencached( bencode({ 'ip': ip, 'port': port })) bc[2][not not_seed][peerid] = compact_peer_info(ip, port)
def generate_response1(randomB, peeridB, keypairA): randomA = Rand.rand_bytes(num_random_bits / 8) response1 = {} response1['certA'] = str(keypairA.pub().get_der()) response1['rA'] = randomA response1['B'] = peeridB response1['SA'] = sign_response(randomA, randomB, peeridB, keypairA) return [randomA, bencode(response1)]
def create_torrent_signature(metainfo, keypairfilename): keypair = EC.load_key(keypairfilename) bmetainfo = bencode(metainfo) digester = sha(bmetainfo[:]) digest = digester.digest() sigstr = keypair.sign_dsa_asn1(digest) metainfo['signature'] = sigstr metainfo['signer'] = str(keypair.pub().get_der())
def do_send_metadata(self, permid, torrent, selversion): metadata_request = bencode(torrent) if DEBUG: print >> sys.stderr, 'metadata: send metadata', len(metadata_request) self.overlay_bridge.send(permid, METADATA + metadata_request, self.metadata_send_callback) if permid != None and BARTERCAST_TORRENTS: self.overlay_bridge.add_task(lambda : self.olthread_bartercast_torrentexchange(permid, 'uploaded'), 0) return len(metadata_request)
def olthread_proxy_have_connect_callback(self, exc, dns, permid, selversion, aggregated_string): if exc is None: message = PROXY_HAVE + self.torrent_hash + bencode(aggregated_string) if DEBUG: print >> sys.stderr, 'helper: olthread_proxy_have_connect_callback: Sending PROXY_HAVE to', show_permid_short(permid) self.overlay_bridge.send(permid, message, self.olthread_proxy_have_send_callback) elif DEBUG: print >> sys.stderr, 'helper: olthread_proxy_have_connect_callback: error connecting to', show_permid_short(permid), exc
def write_extend_message(self, metadata_message_id, payload): if DEBUG: print >> sys.stderr, self._address, 'MiniBitTorrent.write_extend_message()' payload = bencode(payload) self._socket.write(''.join((tobinary(2 + len(payload)), EXTEND, metadata_message_id, payload)))
def verify_torrent_signature(metainfo): r = deepcopy(metainfo) signature = r['signature'] signer = r['signer'] del r['signature'] del r['signer'] bmetainfo = bencode(r) digester = sha(bmetainfo[:]) digest = digester.digest() return do_verify_torrent_signature(digest, signature, signer)
def _create_poa_message(self, msg_id, nonce_a, nonce_b): if not self.poa: raise MissingCertificateException('Missing certificate') msg = [msg_id] + self.poa.serialize_to_list() lst = [nonce_a, nonce_b, self.poa.serialize()] b_list = bencode(lst) digest = permid.sha(b_list).digest() sig = self.my_keypair.sign_dsa_asn1(digest) msg.append(sig) return msg
def forward_connect_callback(self, exc, dns, permid, selversion, destpermid, msgid, msg): if exc is None: if selversion < OLPROTO_VER_SEVENTH: return send_callback = lambda exc, permid: self.forward_send_callback(exc, permid, destpermid, msgid) if DEBUG: print >> sys.stderr, 'friendship: forward_connect_callback: Sending', `msg` self.overlay_bridge.send(permid, FRIENDSHIP + bencode(msg), send_callback) elif DEBUG: print >> sys.stderr, 'friendship: forward: Could not connect to helper', show_permid_short(permid)
def get_scrape(self, paramslist): fs = {} if paramslist.has_key('info_hash'): if self.config['tracker_scrape_allowed'] not in [ ITRACKSCRAPE_ALLOW_SPECIFIC, ITRACKSCRAPE_ALLOW_FULL ]: return ( 400, 'Not Authorized', { 'Content-Type': 'text/plain', 'Pragma': 'no-cache' }, bencode({ 'failure reason': 'specific scrape function is not available with this tracker.' })) for hash in paramslist['info_hash']: if self.allowed is not None: if self.allowed.has_key(hash): fs[hash] = self.scrapedata(hash) elif self.downloads.has_key(hash): fs[hash] = self.scrapedata(hash) else: if self.config['tracker_scrape_allowed'] != ITRACKSCRAPE_ALLOW_FULL: return ( 400, 'Not Authorized', { 'Content-Type': 'text/plain', 'Pragma': 'no-cache' }, bencode({ 'failure reason': 'full scrape function is not available with this tracker.' })) if self.allowed is not None: keys = self.allowed.keys() else: keys = self.downloads.keys() for hash in keys: fs[hash] = self.scrapedata(hash) return (200, 'OK', { 'Content-Type': 'text/plain' }, bencode({'files': fs}))
def _packData(self): if self.description is not None: pass if self.description is None: self.description = u'' bitmask, checksums = self._getSubtitlesMaskAndChecksums() tosign = (self.channel, self.infohash, self.description.encode('utf-8'), self.timestamp, pack('!L', bitmask), checksums) bencoding = bencode(tosign) return bencoding
def do_send_metadata(self, permid, torrent, selversion): metadata_request = bencode(torrent) if DEBUG: print >> sys.stderr, 'metadata: send metadata', len( metadata_request) self.overlay_bridge.send(permid, METADATA + metadata_request, self.metadata_send_callback) if permid != None and BARTERCAST_TORRENTS: self.overlay_bridge.add_task( lambda: self.olthread_bartercast_torrentexchange( permid, 'uploaded'), 0) return len(metadata_request)
def verify(self): if self.expire_time and self.expire_time < time.mktime(time.gmtime()): raise POAExpiredException('Expired') try: lst = [self.torrent_id, self.torrent_pub_key, self.node_pub_key] b_list = bencode(lst) digest = permid.sha(b_list).digest() pub = pub_key_from_der(self.torrent_pub_key) if not pub.verify_dsa_asn1(digest, self.signature): raise InvalidPOAException('Proof of access verification failed') except Exception as e: raise InvalidPOAException('Bad POA: %s' % e)
def fmsg_connect_callback(self, exc, dns, permid, selversion, type = None): if exc is None: if selversion < OLPROTO_VER_SEVENTH: self.remove_msgs_for_ltv7_peer(permid) return sendlist = self.get_msgs_as_sendlist(targetpermid=permid) if DEBUG: print >> sys.stderr, 'friendship: fmsg_connect_callback: sendlist len', len(sendlist) for i in range(0, len(sendlist)): tuple = sendlist[i] permid, msgid, msg = tuple send_callback = lambda exc, permid: self.fmsg_send_callback(exc, permid, msgid) if DEBUG: print >> sys.stderr, 'friendship: fmsg_connect_callback: Sending', `msg`, msgid mypermid = self.session.get_permid() commit = i == len(sendlist) - 1 isForwarder = 0 no_of_helpers = 0 if type == F_FORWARD_MSG: isForwarder = 1 no_of_helpers = 10 no_of_attempts = 0 if permid in self.currmsgs: msgid2rec = self.currmsgs[permid] if msgid in msgid2rec: msgrec = msgid2rec[msgid] no_of_attempts = msgrec['attempt'] self.friendshipStatistics_db.insertOrUpdateFriendshipStatistics(bin2str(mypermid), bin2str(permid), int(time()), isForwarder, no_of_attempts, no_of_helpers, commit=commit) self.overlay_bridge.send(permid, FRIENDSHIP + bencode(msg), send_callback) else: if DEBUG: peer = self.peerdb.getPeer(permid) if peer is None: print >> sys.stderr, 'friendship: Could not connect to peer', show_permid_short(permid), peer else: print >> sys.stderr, 'friendship: Could not connect to peer', show_permid_short(permid), peer['name'] print >> sys.stderr, exc mypermid = self.session.get_permid() isForwarder = 0 no_of_helpers = 0 if type == F_FORWARD_MSG: isForwarder = 1 no_of_helpers = 10 no_of_attempts = 0 if permid in self.currmsgs: msgid2rec = self.currmsgs[permid] for msgid in msgid2rec: msgrec = msgid2rec[msgid] no_of_attempts = msgrec['attempt'] self.friendshipStatistics_db.insertOrUpdateFriendshipStatistics(bin2str(mypermid), bin2str(permid), int(time()), isForwarder, no_of_attempts, no_of_helpers)
def verify(self): if self.expire_time and self.expire_time < time.mktime(time.gmtime()): raise POAExpiredException('Expired') try: lst = [self.torrent_id, self.torrent_pub_key, self.node_pub_key] b_list = bencode(lst) digest = permid.sha(b_list).digest() pub = pub_key_from_der(self.torrent_pub_key) if not pub.verify_dsa_asn1(digest, self.signature): raise InvalidPOAException( 'Proof of access verification failed') except Exception as e: raise InvalidPOAException('Bad POA: %s' % e)
def get_metadata_connect_callback(self, exc, dns, permid, selversion, infohash): if exc is None: if DEBUG: print >> sys.stderr, 'metadata: Sending GET_METADATA to', show_permid_short(permid) try: metadata_request = bencode(infohash) self.overlay_bridge.send(permid, GET_METADATA + metadata_request, self.get_metadata_send_callback) self.requested_torrents.add(infohash) except: print_exc() elif DEBUG: print >> sys.stderr, 'metadata: GET_METADATA: error connecting to', show_permid_short(permid)
def olthread_proxy_have_connect_callback(self, exc, dns, permid, selversion, aggregated_string): if exc is None: message = PROXY_HAVE + self.torrent_hash + bencode( aggregated_string) if DEBUG: print >> sys.stderr, 'helper: olthread_proxy_have_connect_callback: Sending PROXY_HAVE to', show_permid_short( permid) self.overlay_bridge.send(permid, message, self.olthread_proxy_have_send_callback) elif DEBUG: print >> sys.stderr, 'helper: olthread_proxy_have_connect_callback: error connecting to', show_permid_short( permid), exc
def save(self, filename = None): if not self.readonly: self.finalize() if 'initial peers' in self.metainfo: del self.metainfo['initial peers'] bdata = bencode(self.metainfo) if self.protected: bdata = m2_AES_encrypt(bdata, 'tslive_key') bdata = chr(1) + chr(2) + chr(3) + chr(4) + bdata if filename is not None: f = open(filename, 'wb') f.write(bdata) f.close() return bdata
def forward_connect_callback(self, exc, dns, permid, selversion, destpermid, msgid, msg): if exc is None: if selversion < OLPROTO_VER_SEVENTH: return send_callback = lambda exc, permid: self.forward_send_callback( exc, permid, destpermid, msgid) if DEBUG: print >> sys.stderr, 'friendship: forward_connect_callback: Sending', ` msg ` self.overlay_bridge.send(permid, FRIENDSHIP + bencode(msg), send_callback) elif DEBUG: print >> sys.stderr, 'friendship: forward: Could not connect to helper', show_permid_short( permid)
def _create(metainfo, protected = False): metainfo = validTorrentFile(metainfo) t = TorrentDef() t.protected = protected t.metainfo = metainfo t.ts_metainfo_valid = True t.metainfo_valid = True maketorrent.copy_metainfo_to_input(t.metainfo, t.input) if t.get_url_compat(): t.infohash = makeurl.metainfo2swarmid(t.metainfo) else: t.infohash = sha(bencode(metainfo['info'], params={'skip_dict_sorting': True})).digest() if DEBUG: print >> sys.stderr, 'TorrentDef::_create: infohash:', `(t.infohash)` return t
def handle_crawler_request(self, permid, selversion, channel_id, message, reply_callback): if DEBUG: print >> sys.stderr, 'FriendshipCrawler: handle_friendship_crawler_database_query_request', message try: d = bdecode(message) stats = self.getStaticsFromFriendshipStatisticsTable(self.session.get_permid(), d['current time']) msg_dict = {'current time': d['current time'], 'stats': stats} msg = bencode(msg_dict) reply_callback(msg) except Exception as e: print_exc() reply_callback(str(e), 1) return True
def save(self, filename = None): torrent_list = [] for t in self.torrents: torrent_list.append({'name': t['name'], 'data': t['tdef'].get_metainfo()}) bdata = bencode({'qualities': torrent_list}) if self.protected: bdata = m2_AES_encrypt(bdata, '=Atl6GD#Vb+#QwW9zJy34lBOcM-7R7G)') bdata = chr(17) + chr(2) + chr(101) + chr(46) + bdata if filename is not None: f = open(filename, 'wb') f.write(bdata) f.close() return bdata
def save_state(self): self.rawserver.add_task(self.save_state, self.save_dfile_interval) h = None try: h = open(self.dfile, 'wb') if self.config['tracker_dfile_format'] == ITRACKDBFORMAT_BENCODE: h.write(bencode(self.state)) else: pickle.dump(self.state, h, -1) h.close() except: if DEBUG: print_exc() finally: if h is not None: h.close()
def createAndSendBarterCastMessage(self, target_permid, selversion, active = False): if selversion <= OLPROTO_VER_FIFTH: return if DEBUG: print >> sys.stderr, '===========bartercast: Sending BarterCast msg to ', self.bartercastdb.getName(target_permid) bartercast_data = self.createBarterCastMessage(target_permid) if LOG: self.logMsg(bartercast_data, target_permid, 'out', logfile=self.logfile) try: bartercast_msg = bencode(bartercast_data) except: print_exc() print >> sys.stderr, 'error bartercast_data:', bartercast_data return self.overlay_bridge.send(target_permid, BARTERCAST + bartercast_msg, self.bartercastSendCallback) self.blockPeer(target_permid, self.send_block_list, self.block_interval)
def get_metadata_connect_callback(self, exc, dns, permid, selversion, infohash): if exc is None: if DEBUG: print >> sys.stderr, 'metadata: Sending GET_METADATA to', show_permid_short( permid) try: metadata_request = bencode(infohash) self.overlay_bridge.send(permid, GET_METADATA + metadata_request, self.get_metadata_send_callback) self.requested_torrents.add(infohash) except: print_exc() elif DEBUG: print >> sys.stderr, 'metadata: GET_METADATA: error connecting to', show_permid_short( permid)
def validChannelCastMsg(channelcast_data): if not isinstance(channelcast_data, dict): return False for signature, ch in channelcast_data.items(): if not isinstance(ch, dict): if DEBUG: print >> sys.stderr, 'validChannelCastMsg: value not dict' return False length = len(ch) if not 6 <= length <= 7: if DEBUG: print >> sys.stderr, 'validChannelCastMsg: #keys!=7' return False if not ('publisher_id' in ch and 'publisher_name' in ch and 'infohash' in ch and 'torrenthash' in ch and 'torrentname' in ch and 'time_stamp' in ch): if DEBUG: print >> sys.stderr, 'validChannelCastMsg: key missing' return False if length == 7: if 'rich_metadata' not in ch: if DEBUG: print >> sys.stderr, 'validChannelCastMsg: key missing' return False if not validMetadataEntry(ch['rich_metadata']): print >> sys.stderr, 'validChannelCastMsg: invalid rich metadata' return False if not (validPermid(ch['publisher_id']) and isinstance( ch['publisher_name'], str) and validInfohash(ch['infohash']) and validInfohash(ch['torrenthash']) and isinstance(ch['torrentname'], str) and validTimestamp(ch['time_stamp'])): if DEBUG: print >> sys.stderr, 'validChannelCastMsg: something not valid' return False l = (ch['publisher_id'], ch['infohash'], ch['torrenthash'], ch['time_stamp']) if not verify_data(bencode(l), ch['publisher_id'], signature): if DEBUG: print >> sys.stderr, 'validChannelCastMsg: verification failed!' return False return True
def natthreadcb_natCheckReplyCallback(self, ncr_data): if DEBUG: print >> sys.stderr, 'NAT type: ', ncr_data if self.doNatCheckSender is not None: try: ncr_msg = bencode(ncr_data) except: print_exc() if DEBUG: print >> sys.stderr, 'error ncr_data:', ncr_data return False if DEBUG: print >> sys.stderr, 'NatCheckMsgHandler:', ncr_data for reply_callback in self.crawler_reply_callbacks: reply_callback(ncr_msg, callback=self.natCheckReplySendCallback) self.crawler_reply_callbacks = []
def network_returnconn_reply_connect_callback(self, exc, dns): if not currentThread().getName().startswith('NetworkThread'): print >> sys.stderr, 'dialback: network_returnconn_reply_connect_callback: called by', currentThread( ).getName(), ' not NetworkThread' print_stack() if exc is None: hisip = str(dns[0]) try: reply = bencode(hisip) if DEBUG: print >> sys.stderr, 'dialback: DIALBACK_REPLY: sending to', dns self.returnconnhand.send( dns, DIALBACK_REPLY + reply, self.network_returnconn_reply_send_callback) except: print_exc() return False elif DEBUG: print >> sys.stderr, 'dialback: DIALBACK_REPLY: error connecting to', dns, exc