def on_key_request(self, messages): for message in messages: if not message.source.startswith(u"circuit_"): # The intropoint receives the message over a socket, and forwards it to the seeder self.tunnel_logger.info("On key request: relay key request") cache = self.request_cache.add(KeyRelayCache(self, message.payload.identifier, message.candidate.sock_addr)) meta = self.get_meta_message(u'key-request') message = meta.impl(distribution=(self.global_time,), payload=(cache.number, message.payload.info_hash)) relay_circuit = self.intro_point_for[message.payload.info_hash] relay_circuit.tunnel_data(self.dispersy.wan_address, TUNNEL_PREFIX + message.packet) else: # The seeder responds with keys back to the intropoint info_hash = message.payload.info_hash key = self.session_keys[info_hash] circuit = self.circuits[int(message.source[8:])] if self.notifier: self.notifier.notify(NTFY_TUNNEL, NTFY_KEY_RESPOND, info_hash.encode('hex')[:6], circuit.circuit_id) self.tunnel_logger.info("On key request: respond with keys to %s" % repr(message.candidate.sock_addr)) meta = self.get_meta_message(u'key-response') pex_peers = self.infohash_pex.get(info_hash, set()) response = meta.impl(distribution=(self.global_time,), payload=( message.payload.identifier, key.pub().key_to_bin(), encode(list(pex_peers)[:50]))) circuit.tunnel_data(message.candidate.sock_addr, TUNNEL_PREFIX + response.packet)
def _encode_encr_response(self, message): str_identifer = pack("!H", message.payload.identifier) str_prefs = pack( "!" + "128s" * len(message.payload.preference_list), *[long_to_bytes(preference, 128) for preference in message.payload.preference_list] ) str_hprefs = pack("!" + "20s" * len(message.payload.his_preference_list), *message.payload.his_preference_list) return (encode([str_identifer, str_prefs, str_hprefs]),)
def _encode_moderation(self, message): dict = {"text": message.payload.text, "timestamp": message.payload.timestamp, "severity": message.payload.severity} dict["cause-mid"] = message.payload.cause_mid dict["cause-global-time"] = message.payload.cause_global_time return encode(dict),
def _encode_decode(self, encode, decode, message): result = encode(message) try: decode(None, 0, result[0]) except DropPacket: raise except: pass return result
def _encode_search_request(self, message): packet = pack("!HH", message.payload.identifier, message.payload.ttl), message.payload.keywords if message.payload.bloom_filter: packet = packet + ( message.payload.bloom_filter.functions, message.payload.bloom_filter.prefix, message.payload.bloom_filter.bytes, ) packet = encode(packet) return (packet,)
def report(self): if DEBUG: print >> sys.stderr, "TUDelftReporter: report" events = self.get_events() if events: events = [{"name": event.get_name(), "time": event.get_time(), "values": event.get_values()} for event in events] data = (time(), self.device_id.encode("HEX"), events) compressed = compress(encode(data)) if DEBUG: print >> sys.stderr, "TUDelftReporter: posting", len(compressed), "bytes payload" self.post(compressed) else: if DEBUG: print >> sys.stderr, "TUDelftReporter: Nothing to report"
def create_created_e2e(self, rendezvous_point, message): info_hash = message.payload.info_hash key = self.session_keys[info_hash] circuit = self.circuits[int(message.source[8:])] shared_secret, Y, AUTH = self.crypto.generate_diffie_shared_secret(message.payload.key, key) rendezvous_point.circuit.hs_session_keys = self.crypto.generate_session_keys(shared_secret) rp_info_enc = self.crypto.encrypt_str( encode((rendezvous_point.rp_info, rendezvous_point.cookie)), *self.get_session_keys(rendezvous_point.circuit.hs_session_keys, EXIT_NODE)) meta = self.get_meta_message(u'created-e2e') response = meta.impl(distribution=(self.global_time,), payload=( message.payload.identifier, Y, AUTH, rp_info_enc)) circuit.tunnel_data(message.candidate.sock_addr, TUNNEL_PREFIX + response.packet)
def _encode_modification(self, message): modification_on = message.payload.modification_on.load_message() dict = {"modification-type": message.payload.modification_type, "modification-value": message.payload.modification_value, "timestamp": message.payload.timestamp, "modification-on-mid": modification_on.authentication.member.mid, "modification-on-global-time": modification_on.distribution.global_time} prev_modification = message.payload.prev_modification_packet if prev_modification: message = prev_modification.load_message() dict["prev-modification-mid"] = message.authentication.member.mid dict["prev-modification-global-time"] = message.distribution.global_time return encode(dict),
def _encode_simi_response(self, message): def _encode_response(mid, preference_list, his_preference_list): str_mid = pack("!20s", mid) if mid else "" str_prefs = pack( "!" + "128s" * len(preference_list), *[long_to_bytes(preference, 128) for preference in preference_list] ) str_hprefs = pack("!" + "20s" * len(his_preference_list), *his_preference_list) return (str_mid, str_prefs, str_hprefs) responses = [] responses.append(_encode_response(None, message.payload.preference_list, message.payload.his_preference_list)) for mid, list_tuple in message.payload.bundled_responses: responses.append(_encode_response(mid, list_tuple[0], list_tuple[1])) packet = pack("!H", message.payload.identifier), responses return (encode(packet),)
def _encode_comment(self, message): dict = {"text": message.payload.text, "timestamp": message.payload.timestamp} playlist_packet = message.payload.playlist_packet infohash = message.payload.infohash if message.payload.reply_to_mid: dict["reply-to-mid"] = message.payload.reply_to_mid dict["reply-to-global-time"] = message.payload.reply_to_global_time if message.payload.reply_after_mid: dict["reply-after-mid"] = message.payload.reply_after_mid dict["reply-after-global-time"] = message.payload.reply_after_global_time if playlist_packet: message = playlist_packet.load_message() dict["playlist-mid"] = message.authentication.member.mid dict["playlist-global-time"] = message.distribution.global_time if infohash: dict['infohash'] = infohash return encode(dict),
def create_msg(): normal_msg = pack('!20sQ', message.payload.infohash, message.payload.timestamp), message.payload.name, tuple(files), tuple(trackers) normal_msg = encode(normal_msg) return zlib.compress(normal_msg)
def _encode_search_response(self, message): packet = pack('!H', message.payload.identifier), message.payload.results return encode(packet),
def dht_callback(info_hash, peers, _): if not peers: peers = [] meta = self.get_meta_message(u'dht-response') circuit_id = message.payload.circuit_id # Send the list of peers for this info_hash back to the requester dht_response_message = meta.impl(distribution=(self.global_time,), payload=(message.payload.circuit_id, message.payload.identifier, message.payload.info_hash, encode(peers))) if circuit_id in self.exit_sockets: circuit = self.exit_sockets[circuit_id] circuit.tunnel_data(message.candidate.sock_addr, TUNNEL_PREFIX + dht_response_message.packet) else: self.tunnel_logger.info("Circuit %d is not existing anymore, can't send back dht-response" % circuit_id)
def _encode_channel(self, message): return encode((message.payload.name, message.payload.description)),
def create_msg(): return encode(message.payload.torrents)
def create_msg(): normal_msg = (pack('!20sQ', message.payload.infohash, message.payload.timestamp), message.payload.name, tuple(files), tuple(trackers)) normal_msg = encode(normal_msg) return zlib.compress(normal_msg)
def _encode_channelsearch_response(self, message): packet = encode((message.payload.keywords, message.payload.torrents)) return packet,
def _encode_channelsearch(self, message): packet = encode(message.payload.keywords) return packet,
def test_encode(self): result = encode(42, 'a') self.assertEqual(result, 'a2i42')
def _encode_mark_torrent(self, message): dict = {"infohash": message.payload.infohash, "timestamp": message.payload.timestamp, "type": message.payload.type} return encode(dict),
def test_encode_wrongversion(self): encode(42, 'b')