def test_sign_syn(self): print("") print("Testing sign syn") print("") self.clean_slate_all() syn = copy.deepcopy(self.syn) signed_syn = self.alice.sign_contract(syn) print(signed_syn) print(self.alice.is_valid_contract_sig(signed_syn)) node_id = self.alice.net.dht_node.get_id() print(node_id) self.assertEqual( self.alice.is_valid_contract_sig(signed_syn, node_id), 1 ) node_id = parse_node_id_from_unl(self.alice.net.unl.value) self.assertEqual( self.alice.is_valid_contract_sig(signed_syn, node_id), 1 ) print(node_id) self.assertTrue(syn[u"src_unl"] == self.alice.net.unl.value) print("Bob's perspective") assert(self.bob.is_valid_contract_sig(signed_syn, node_id)) print("----") print(signed_syn) print("") print("End sign syn") print("")
def handle_requests(node, msg): _log.debug("In handle requests") # Check message type. msg = list_to_ordered_dict(msg) if msg[u"type"] != u"test_bandwidth_request": _log.debug("req: Invalid request") return -1 # Drop request if test already active. if self.test_node_unl is not None: _log.debug("req: test already active") return -2 # Check they got our node unl right. our_unl = self.transfer.net.unl.value if our_unl != msg[u"test_node_unl"]: _log.debug("req: they got our node unl wrong") return -3 # Check sig. src_node_id = parse_node_id_from_unl(msg[u"requester"]) if not verify_signature(msg, self.wif, src_node_id): _log.debug("req: Invalid sig") return -4 # Build response. res = OrderedDict([ (u"type", u"test_bandwidth_response"), (u"timestamp", time.time()), (u"requestee", our_unl), (u"request", msg) ]) # Sign response res = sign(res, self.wif) # Save their node ID! self.test_node_unl = msg[u"requester"] # Add accept handler for bandwidth tests. accept_handler = build_accept_handler(self, msg) self.add_handler("accept", accept_handler) # Add start handler. start_handler = build_start_handler(self, msg) self.add_handler("start", start_handler) # Set start time. self.start_time = time.time() # Save data id. self.data_id = msg[u"data_id"] # Send request back to source. res = ordered_dict_to_list(res) self.api.repeat_relay_message(src_node_id, res) _log.debug("req: got request") return res
def process_rst(client, msg): # Sanity checks. if u"contract_id" not in msg: _log.debug("RST: Contract id not in msg") return -1 if u"src_unl" not in msg: _log.debug("RST: Src unl not in msg") return -2 contract_id = msg[u"contract_id"] if contract_id not in client.contracts: _log.debug("RST: Contract not found") _log.debug("-------------") _log.debug(client.contracts) _log.debug("-------------") _log.debug(str(msg)) return -3 # Check UNLs match for this contract. contract = client.contracts[contract_id] expected_unl = contract["dest_unl"] found_unl = msg[u"src_unl"] if expected_unl != found_unl: _log.debug("RST: UNLs dont match") return -4 # Check sig matches the UNL. node_id = parse_node_id_from_unl(found_unl) if not client.is_valid_contract_sig(msg, node_id): _log.debug("RST: sig doesn't match") return -5 # Raise rejection callback and return! _log.debug("Rejection request received!") if contract_id in client.defers: _log.debug("RST: firing errback") e = RequestDenied("Request was rejected!") client.defers[contract_id].errback(e) del client.defers[contract_id] return 1
def start(self, node_unl, size=1): """ :param node_unl: UNL of target :param size: MB to send in transfer :return: deferred with test results """ # Any tests currently in progress? if self.test_node_unl is not None: d = defer.Deferred() d.errback(Exception("Test already in progress")) return d # Reset test state self.test_size = size # Reset deferred. self.active_test = defer.Deferred() # Generate random file to upload. file_size = size * ONE_MB shard = generate_random_file(file_size) # Hash partial content. self.data_id = get_hash(shard).decode("utf-8") _log.debug("FINGER_log.debug HASH") _log.debug(self.data_id) # File meta data. meta = OrderedDict([ (u"file_size", file_size), (u"algorithm", u"sha256"), (u"hash", self.data_id.decode("utf-8")) ]) _log.debug("UNL") _log.debug(self.transfer.net.unl.value) _log.debug("META") _log.debug(meta) # Sign meta data. sig = sign(meta, self.wif)[u"signature"] _log.debug("SIG") _log.debug(sig) # Add file to storage. storjnode.storage.manager.add(self.transfer.store_config, shard) # Build bandwidth test request. req = OrderedDict([ (u"type", u"test_bandwidth_request"), (u"timestamp", int(time.time())), (u"requester", self.transfer.net.unl.value), (u"test_node_unl", node_unl), (u"data_id", self.data_id), (u"file_size", file_size) ]) # Sign request. req = sign(req, self.wif) # Send request. node_id = parse_node_id_from_unl(node_unl) req = ordered_dict_to_list(req) self.api.relay_message(node_id, req) # Set start time. self.start_time = time.time() # Return deferred. return self.active_test
def process_syn_ack(client, msg): # Valid syn-ack? if u"syn" not in msg: _log.debug("SYN-ACK: syn not in msg.") return -1 # Check length is correct. if len(msg) != 3: _log.debug("incorrect length") return -2 # Is this a reply to our SYN? contract_id = client.contract_id(msg[u"syn"]) if contract_id not in client.contracts: _log.debug("--------------") _log.debug(contract_id) _log.debug("--------------") _log.debug(msg) _log.debug("--------------") _log.debug(client.contracts) _log.debug("--------------") _log.debug("SYN-ACK: contract not found.") return -3 # Check syn is valid. if is_valid_syn(client, msg[u"syn"]) != 1: _log.debug("SYN-ACK: invalid syn.") return -4 # Did I sign this? if not client.is_valid_contract_sig(msg[u"syn"]): _log.debug("SYN-ACK: our sig is invalid.") return -5 # Check their sig is valid. contract = client.contracts[contract_id] their_node_id = parse_node_id_from_unl(contract["dest_unl"]) if not client.is_valid_contract_sig(msg, their_node_id): _log.debug(msg) _log.debug("Their signature was incorrect.") return -6 # Check handshake state is valid. if contract_id not in client.handshake: _log.debug("contract id not in handshake") return -7 if client.handshake[contract_id][u"state"] != u"SYN": _log.debug("state = " + str(client.handshake[contract_id][u"state"])) _log.debug("handshake state invalid") return -8 # Update handshake. client.handshake[contract_id] = {u"state": u"ACK", u"timestamp": time.time()} # Create reply contract. reply = OrderedDict([(u"status", u"ACK"), (u"syn_ack", msg)]) # Sign reply. reply = client.sign_contract(reply) # Are we already connected? is_reliable_con = 0 con = client.net.con_by_unl(contract["dest_unl"], client.cons) if con is not None: # Otherwise the con could be torn down soon. elapsed = time.time() - con.alive _log.debug("Alive duration: " + str(elapsed)) if elapsed <= 40: is_reliable_con = 1 success_wrapper(client, contract_id, contract["host_unl"])(con) else: _log.debug("con is not reliable.") # Disable queued transfers. if not ENABLE_QUEUED_TRANSFERS: is_reliable_con = 0 # Try make TCP con. if not is_reliable_con: client.net.unl.connect( contract["dest_unl"], {"success": success_wrapper(client, contract_id, contract["host_unl"])}, force_master=0, nonce=contract_id, ) # Send reply. client.send_msg(reply, msg[u"syn"][u"dest_unl"]) _log.debug("SYN-ACK") return reply
def process_syn(client, msg, enable_accept_handlers=ENABLE_ACCEPT_HANDLERS): # Check syn is valid. if is_valid_syn(client, msg) != 1: _log.debug("SYN: invalid syn.") return -1 # Check our UNL is correct. if msg[u"dest_unl"] != client.net.unl.value: _log.debug("They got our UNL wrong.") return -3 # Check their sig is valid. contract_id = client.contract_id(msg).decode("utf-8") src_node_id = parse_node_id_from_unl(msg[u"src_unl"]) if not client.is_valid_contract_sig(msg, src_node_id): _log.debug(msg) _log.debug("Their signature was incorrect.") return -4 # Should we accept this? expired_handlers = set() accept_this = 0 for handler in client.handlers[u"accept"]: ret = handler(contract_id, msg[u"src_unl"], msg[u"data_id"], msg[u"file_size"]) if ret == -1: accept_this = 1 expired_handlers.add(handler) break if ret == 1: accept_this = 1 break # Their handshake will timeout. if not accept_this and enable_accept_handlers: _log.debug("Rejected data request") # Build reject reply. reply = OrderedDict([(u"status", u"RST"), (u"contract_id", contract_id), (u"src_unl", client.net.unl.value)]) # Sign reply. reply = client.sign_contract(reply) # Send reply to source. reply = json.dumps(reply, ensure_ascii=True) client.net.dht_node.relay_message(src_node_id, reply) # Quit. return -2 # Remove expired accept handler. for handler in expired_handlers: client.handlers[u"accept"].remove(handler) # Check handshake state. if contract_id in client.handshake: return -5 # Save contract. client.save_contract(msg) client.handshake[contract_id] = {u"state": u"SYN-ACK", u"timestamp": time.time()} # Create reply. reply = OrderedDict([(u"status", u"SYN-ACK"), (u"syn", msg)]) # Sign reply. reply = client.sign_contract(reply) # Save reply. client.send_msg(reply, msg[u"src_unl"]) _log.debug("SYN") # Success. return reply
def handle_responses(node, msg): # Check message type. msg = list_to_ordered_dict(msg) if msg[u"type"] != u"test_bandwidth_response": _log.debug("res: Invalid response") return -1 # Transfer already active. if self.test_node_unl is not None: _log.debug("res: transfer already active") return -2 # Check we sent the request. req = msg[u"request"] _log.debug(req) # Check node IDs match. if req[u"test_node_unl"] != msg[u"requestee"]: _log.debug("res: node ids don't match") return -4 # Check signature. valid_sig = verify_signature( msg[u"request"], self.wif, self.api.get_id() ) # Quit if sig is invalid. if not valid_sig: _log.debug("res: our request sig was invalid") return -3 # Check their sig. src_node_id = parse_node_id_from_unl(msg[u"requestee"]) if not verify_signature(msg, self.wif, src_node_id): _log.debug("res: their sig did not match") return -5 # Set active node ID. self.test_node_unl = msg[u"requestee"] # Register accept handler. accept_handler = build_accept_handler(self, req) self.add_handler("accept", accept_handler) # Register start handler. start_handler = build_start_handler(self, req) self.add_handler("start", start_handler) # Send upload request to remote host! contract_id = self.transfer.data_request( "download", req[u"data_id"], req[u"file_size"], req[u"test_node_unl"] ) # Fire error. def errback(ret): if self.active_test is not None: self.active_test.errback(ret) self.reset_state() # Register error handler for transfer. self.transfer.defers[contract_id].addErrback(errback) # Build completion handler. completion_handler = build_completion_handler( self, req, accept_handler ) # Register completion handler. self.add_handler("complete", completion_handler) _log.debug("res: got response")