def isValidRemoteVal(d, selversion): if not isinstance(d, dict): if DEBUG: print >> sys.stderr, "rqmh: reply: a: value not dict" return False if selversion >= OLPROTO_VER_TWELFTH: if not ('content_name' in d and 'length' in d and 'leecher' in d and 'seeder' in d and 'category' in d and 'torrent_size' in d and 'channel_permid' in d and 'channel_name' in d): if DEBUG: print >> sys.stderr, "rqmh: reply: torrentrec12: key missing, got", d.keys( ) return False if 'metatype' in d and 'metadata' in d: try: metatype = d['metatype'] metadata = d['metadata'] if metatype == URL_MIME_TYPE: tdef = TorrentDef.load_from_url(metadata) else: metainfo = bdecode(metadata) tdef = TorrentDef.load_from_dict(metainfo) except: if DEBUG: print >> sys.stderr, "rqmh: reply: torrentrec12: metadata invalid" print_exc() return False elif selversion >= OLPROTO_VER_ELEVENTH: if not ('content_name' in d and 'length' in d and 'leecher' in d and 'seeder' in d and 'category' in d and 'torrent_size' in d and 'channel_permid' in d and 'channel_name' in d): if DEBUG: print >> sys.stderr, "rqmh: reply: torrentrec11: key missing, got", d.keys( ) return False elif selversion >= OLPROTO_VER_NINETH: if not ('content_name' in d and 'length' in d and 'leecher' in d and 'seeder' in d and 'category' in d and 'torrent_size' in d): if DEBUG: print >> sys.stderr, "rqmh: reply: torrentrec9: key missing, got", d.keys( ) return False else: if not ('content_name' in d and 'length' in d and 'leecher' in d and 'seeder' in d and 'category' in d): if DEBUG: print >> sys.stderr, "rqmh: reply: torrentrec6: key missing, got", d.keys( ) return False # if not (isinstance(d['content_name'],str) and isinstance(d['length'],int) and isinstance(d['leecher'],int) and isinstance(d['seeder'],int)): # return False # if len(d) > 4: # no other keys # return False return True
def setUpPostSession(self): """ override TestAsServer """ TestAsServer.setUpPostSession(self) self.mypermid = str(self.my_keypair.pub().get_der()) self.hispermid = str(self.his_keypair.pub().get_der()) # Create URL compat torrents and save in Torrent database. self.tdef1 = TorrentDef.load_from_url(P2PURL_SCHEME+'://127.2.3.42:7764/announce?SjaakCam.mpegts&k=MHowDQYJKoZIhvcNAQEBBQADaQAwZgJhAN0Khlp5ZhWC7VfLynCkKts71b8h8tZXH87PkDtJUTJaX_SS1Cddxkv63PRmKOvtAHhkTLSsWOZbSeHkOlPIq_FGg2aDLDJ05g3lQ-8mSmo05ff4SLqNUTShWO2CR2TPhQIBAw&l=HCAAAA&s=15&a=RSA&b=AAIAAA') self.torrentfn1 = os.path.join(self.session.get_torrent_collecting_dir(),"live.torrent") self.tdef1.save(self.torrentfn1) self.tdef2 = TorrentDef.load_from_url(P2PURL_SCHEME+'://127.1.0.10:6969/announce?trailer.mkv&r=TTgcifG0Ot7STCY2JL8SUOxROFo&l=AKK35A&s=15&b=AAFnGg') self.torrentfn2 = os.path.join(self.session.get_torrent_collecting_dir(),"vod.torrent") self.tdef2.save(self.torrentfn2) self.torrent_db = self.session.open_dbhandler(NTFY_TORRENTS) extra_info = {'status':'good'} self.torrent_db.addExternalTorrent(self.torrentfn1, source='',extra_info=extra_info) self.torrent_db.addExternalTorrent(self.torrentfn2, source='',extra_info=extra_info)
def isValidRemoteVal(d,selversion): if not isinstance(d,dict): if DEBUG: print >>sys.stderr,"rqmh: reply: a: value not dict" return False if selversion >= OLPROTO_VER_TWELFTH: if not ('content_name' in d and 'length' in d and 'leecher' in d and 'seeder' in d and 'category' in d and 'torrent_size' in d and 'channel_permid' in d and 'channel_name' in d): if DEBUG: print >>sys.stderr,"rqmh: reply: torrentrec12: key missing, got",d.keys() return False if 'metatype' in d and 'metadata' in d: try: metatype = d['metatype'] metadata = d['metadata'] if metatype == URL_MIME_TYPE: tdef = TorrentDef.load_from_url(metadata) else: metainfo = bdecode(metadata) tdef = TorrentDef.load_from_dict(metainfo) except: if DEBUG: print >>sys.stderr,"rqmh: reply: torrentrec12: metadata invalid" print_exc() return False elif selversion >= OLPROTO_VER_ELEVENTH: if not ('content_name' in d and 'length' in d and 'leecher' in d and 'seeder' in d and 'category' in d and 'torrent_size' in d and 'channel_permid' in d and 'channel_name' in d): if DEBUG: print >>sys.stderr,"rqmh: reply: torrentrec11: key missing, got",d.keys() return False elif selversion >= OLPROTO_VER_NINETH: if not ('content_name' in d and 'length' in d and 'leecher' in d and 'seeder' in d and 'category' in d and 'torrent_size' in d): if DEBUG: print >>sys.stderr,"rqmh: reply: torrentrec9: key missing, got",d.keys() return False else: if not ('content_name' in d and 'length' in d and 'leecher' in d and 'seeder' in d and 'category' in d): if DEBUG: print >>sys.stderr,"rqmh: reply: torrentrec6: key missing, got",d.keys() return False # if not (isinstance(d['content_name'],str) and isinstance(d['length'],int) and isinstance(d['leecher'],int) and isinstance(d['seeder'],int)): # return False # if len(d) > 4: # no other keys # return False return True
def recv_query_reply(self,permid,message,selversion): #print "****** recv query reply", len(message) if selversion < OLPROTO_VER_SIXTH: return False #if len(message) > MAX_QUERY_REPLY_LEN: # return True # don't close # Unpack try: d = bdecode(message[1:]) except: if DEBUG: print >>sys.stderr,"rquery: Cannot bdecode QUERY_REPLY message", selversion return False if not isValidQueryReply(d,selversion): if DEBUG: print >>sys.stderr,"rquery: not valid QUERY_REPLY message", selversion return False # Check auth queryrec = self.is_registered_query_id(d['id']) if not queryrec: if DEBUG: print >>sys.stderr,"rquery: QUERY_REPLY has unknown query ID", selversion return False if selversion >= OLPROTO_VER_TWELFTH: if queryrec['query'].startswith('SIMPLE+METADATA'): for infohash,torrentrec in d['a'].iteritems(): if not 'metatype' in torrentrec: if DEBUG: print >>sys.stderr,"rquery: QUERY_REPLY has no metatype field", selversion return False if not 'metadata' in torrentrec: if DEBUG: print >>sys.stderr,"rquery: QUERY_REPLY has no metadata field", selversion return False if torrentrec['torrent_size'] != len(torrentrec['metadata']): if DEBUG: print >>sys.stderr,"rquery: QUERY_REPLY torrent_size != len metadata", selversion return False try: # Validity test if torrentrec['metatype'] == URL_MIME_TYPE: tdef = TorrentDef.load_from_url(torrentrec['metadata']) else: metainfo = bdecode(torrentrec['metadata']) tdef = TorrentDef.load_from_dict(metainfo) except: if DEBUG: print_exc() return False # Process self.process_query_reply(permid,queryrec['query'],queryrec['usercallback'],d) return True
def got_metadata(self, permid, message, selversion): """ receive torrent file from others """ # Arno, 2007-06-20: Disabled the following code. What's this? Somebody sends # us something and we refuse? Also doesn't take into account download help #and remote-query extension. #if self.upload_rate <= 0: # if no upload, no download, that's the game # return True # don't close connection try: message = bdecode(message[1:]) except: print_exc() return False if not isinstance(message, dict): return False try: infohash = message['torrent_hash'] if not isValidInfohash(infohash): return False if not infohash in self.requested_torrents: # got a torrent which was not requested return True if self.torrent_db.hasMetaData(infohash): return True # P2PURL goturl = False if selversion >= OLPROTO_VER_ELEVENTH: if 'metatype' in message and message['metatype'] == URL_MIME_TYPE: try: tdef = TorrentDef.load_from_url(metadata['metadata']) # Internal storage format is still .torrent file metainfo = tdef.get_metainfo() metadata = bencode(metainfo) goturl = True except: print_exc() return False else: metadata = message['metadata'] else: metadata = message['metadata'] if not self.valid_metadata(infohash, metadata): return False if DEBUG: torrent_size = len(metadata) if goturl: mdt = "URL" else: mdt = "torrent" print >> sys.stderr,"metadata: Recvd",mdt,`infohash`,sha(infohash).hexdigest(), torrent_size extra_info = {} if selversion >= OLPROTO_VER_FOURTH: try: extra_info = {'leecher': message.get('leecher', -1), 'seeder': message.get('seeder', -1), 'last_check_time': message.get('last_check_time', -1), 'status':message.get('status', 'unknown')} except Exception, msg: print_exc() print >> sys.stderr, "metadata: wrong extra info in msg - ", message extra_info = {} filename = self.save_torrent(infohash, metadata, extra_info=extra_info) self.requested_torrents.remove(infohash) #if DEBUG: # print >>sys.stderr,"metadata: Was I asked to dlhelp someone",self.dlhelper if filename is not None: self.notify_torrent_is_in(infohash,metadata,filename) # BarterCast: add bytes of torrent to BarterCastDB # Save exchanged KBs in BarterCastDB if permid is not None and BARTERCAST_TORRENTS: self.overlay_bridge.add_task(lambda:self.olthread_bartercast_torrentexchange(permid, 'downloaded'), 0)
def got_metadata(self, permid, message, selversion): """ receive torrent file from others """ # Arno, 2007-06-20: Disabled the following code. What's this? Somebody sends # us something and we refuse? Also doesn't take into account download help #and remote-query extension. #if self.upload_rate <= 0: # if no upload, no download, that's the game # return True # don't close connection try: message = bdecode(message[1:]) except: print_exc() return False if not isinstance(message, dict): return False try: infohash = message['torrent_hash'] if not isValidInfohash(infohash): return False if not infohash in self.requested_torrents: # got a torrent which was not requested return True if self.torrent_db.hasMetaData(infohash): return True # P2PURL goturl = False if selversion >= OLPROTO_VER_ELEVENTH: if 'metatype' in message and message[ 'metatype'] == URL_MIME_TYPE: try: tdef = TorrentDef.load_from_url(metadata['metadata']) # Internal storage format is still .torrent file metainfo = tdef.get_metainfo() metadata = bencode(metainfo) goturl = True except: print_exc() return False else: metadata = message['metadata'] else: metadata = message['metadata'] if not self.valid_metadata(infohash, metadata): return False if DEBUG: torrent_size = len(metadata) if goturl: mdt = "URL" else: mdt = "torrent" print >> sys.stderr, "metadata: Recvd", mdt, ` infohash `, sha( infohash).hexdigest(), torrent_size extra_info = {} if selversion >= OLPROTO_VER_FOURTH: try: extra_info = { 'leecher': message.get('leecher', -1), 'seeder': message.get('seeder', -1), 'last_check_time': message.get('last_check_time', -1), 'status': message.get('status', 'unknown') } except Exception, msg: print_exc() print >> sys.stderr, "metadata: wrong extra info in msg - ", message extra_info = {} filename = self.save_torrent(infohash, metadata, extra_info=extra_info) self.requested_torrents.remove(infohash) #if DEBUG: # print >>sys.stderr,"metadata: Was I asked to dlhelp someone",self.dlhelper if filename is not None: self.notify_torrent_is_in(infohash, metadata, filename) # BarterCast: add bytes of torrent to BarterCastDB # Save exchanged KBs in BarterCastDB if permid is not None and BARTERCAST_TORRENTS: self.overlay_bridge.add_task( lambda: self.olthread_bartercast_torrentexchange( permid, 'downloaded'), 0)
def recv_query_reply(self, permid, message, selversion): #print "****** recv query reply", len(message) if selversion < OLPROTO_VER_SIXTH: return False #if len(message) > MAX_QUERY_REPLY_LEN: # return True # don't close # Unpack try: d = bdecode(message[1:]) except: if DEBUG: print >> sys.stderr, "rquery: Cannot bdecode QUERY_REPLY message", selversion return False if not isValidQueryReply(d, selversion): if DEBUG: print >> sys.stderr, "rquery: not valid QUERY_REPLY message", selversion return False # Check auth queryrec = self.is_registered_query_id(d['id']) if not queryrec: if DEBUG: print >> sys.stderr, "rquery: QUERY_REPLY has unknown query ID", selversion return False if selversion >= OLPROTO_VER_TWELFTH: if queryrec['query'].startswith('SIMPLE+METADATA'): for infohash, torrentrec in d['a'].iteritems(): if not 'metatype' in torrentrec: if DEBUG: print >> sys.stderr, "rquery: QUERY_REPLY has no metatype field", selversion return False if not 'metadata' in torrentrec: if DEBUG: print >> sys.stderr, "rquery: QUERY_REPLY has no metadata field", selversion return False if torrentrec['torrent_size'] != len( torrentrec['metadata']): if DEBUG: print >> sys.stderr, "rquery: QUERY_REPLY torrent_size != len metadata", selversion return False try: # Validity test if torrentrec['metatype'] == URL_MIME_TYPE: tdef = TorrentDef.load_from_url( torrentrec['metadata']) else: metainfo = bdecode(torrentrec['metadata']) tdef = TorrentDef.load_from_dict(metainfo) except: if DEBUG: print_exc() return False # Process self.process_query_reply(permid, queryrec['query'], queryrec['usercallback'], d) return True