def read_and_send_metadata(self, permid, infohash, torrent_path, selversion): torrent_data = self.read_torrent(torrent_path) if torrent_data: # Arno: Don't send private torrents try: metainfo = bdecode(torrent_data) if 'info' in metainfo and 'private' in metainfo[ 'info'] and metainfo['info']['private']: if DEBUG: print >> sys.stderr, "metadata: Not sending torrent", ` torrent_path `, "because it is private" return 0 except: print_exc() return 0 if DEBUG: print >> sys.stderr, "metadata: sending torrent", ` torrent_path `, len( torrent_data) torrent = {} torrent['torrent_hash'] = infohash # P2PURLs: If URL compat then send URL tdef = TorrentDef.load_from_dict(metainfo) if selversion >= OLPROTO_VER_ELEVENTH and tdef.get_url_compat(): torrent['metatype'] = URL_MIME_TYPE torrent['metadata'] = tdef.get_url() else: torrent['metatype'] = TSTREAM_MIME_TYPE torrent['metadata'] = torrent_data if selversion >= OLPROTO_VER_FOURTH: data = self.torrent_db.getTorrent(infohash) if data is None: # DB inconsistency return 0 nleechers = data.get('leecher', -1) nseeders = data.get('seeder', -1) last_check_ago = int(time()) - data.get('last_check_time', 0) # relative time if last_check_ago < 0: last_check_ago = 0 status = data.get('status', 'unknown') torrent.update({ 'leecher': nleechers, 'seeder': nseeders, 'last_check_time': last_check_ago, 'status': status }) return self.do_send_metadata(permid, torrent, selversion) else: # deleted before sending it self.torrent_db.deleteTorrent(infohash, delete_file=True, updateFlag=True) if DEBUG: print >> sys.stderr, "metadata: GET_METADATA: no torrent data to send" return 0
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 read_and_send_metadata(self, permid, infohash, torrent_path, selversion): torrent_data = self.read_torrent(torrent_path) if torrent_data: # Arno: Don't send private torrents try: metainfo = bdecode(torrent_data) if 'info' in metainfo and 'private' in metainfo['info'] and metainfo['info']['private']: if DEBUG: print >> sys.stderr,"metadata: Not sending torrent", `torrent_path`,"because it is private" return 0 except: print_exc() return 0 if DEBUG: print >> sys.stderr,"metadata: sending torrent", `torrent_path`, len(torrent_data) torrent = {} torrent['torrent_hash'] = infohash # P2PURLs: If URL compat then send URL tdef = TorrentDef.load_from_dict(metainfo) if selversion >= OLPROTO_VER_ELEVENTH and tdef.get_url_compat(): torrent['metatype'] = URL_MIME_TYPE torrent['metadata'] = tdef.get_url() else: torrent['metatype'] = TSTREAM_MIME_TYPE torrent['metadata'] = torrent_data if selversion >= OLPROTO_VER_FOURTH: data = self.torrent_db.getTorrent(infohash) if data is None: # DB inconsistency return 0 nleechers = data.get('leecher', -1) nseeders = data.get('seeder', -1) last_check_ago = int(time()) - data.get('last_check_time', 0) # relative time if last_check_ago < 0: last_check_ago = 0 status = data.get('status', 'unknown') torrent.update({'leecher':nleechers, 'seeder':nseeders, 'last_check_time':last_check_ago, 'status':status}) return self.do_send_metadata(permid, torrent, selversion) else: # deleted before sending it self.torrent_db.deleteTorrent(infohash, delete_file=True, updateFlag=True) if DEBUG: print >> sys.stderr,"metadata: GET_METADATA: no torrent data to send" return 0
def valid_metadata(self, infohash, metadata): try: metainfo = bdecode(metadata) tdef = TorrentDef.load_from_dict(metainfo) got_infohash = tdef.get_infohash() if infohash != got_infohash: print >> sys.stderr, "metadata: infohash doesn't match the torrent " + \ "hash. Required: " + `infohash` + ", but got: " + `got_infohash` return False return True except: print_exc() #print >> sys.stderr, "problem metadata:", repr(metadata) return False
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 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