def test_00001(self): dht_node = DHT() content = u"content" dht_node.send_direct_message(dht_node.node_id, content) replies = dht_node.list(dht_node.node_id, dht_node.password) assert (len(replies) == 1) assert (replies[0] == content) if sys.version_info >= (3, 0, 0): assert (type(replies[0]) == str) else: assert (type(replies[0]) == unicode)
def test_00001(self): dht_node = DHT() content = u"content" dht_node.send_direct_message(dht_node.node_id, content) replies = dht_node.list(dht_node.node_id, dht_node.password) print(len(replies)) assert (len(replies) == 1) assert (replies[0] == content) dht_node.send_direct_message(dht_node.node_id, content) replies = dht_node.list(dht_node.node_id, dht_node.password) print(replies)
def test_00001(self): dht_node = DHT() content = u"content" dht_node.send_direct_message(dht_node.node_id, content) replies = dht_node.list(dht_node.node_id, dht_node.password) print(len(replies)) assert len(replies) == 1 assert replies[0] == content dht_node.send_direct_message(dht_node.node_id, content) replies = dht_node.list(dht_node.node_id, dht_node.password) print(replies)
def test_relay_link(self): bob = DHT(networking=0) alice = DHT(networking=0) alice.protocol.messages_received = Queue() bob.protocol.messages_received = Queue() bob.add_relay_link(alice) alice.add_relay_link(bob) msg = u"test" bob.send_direct_message(alice.get_id(), msg) assert (alice.has_messages()) assert (alice.get_messages()[0] == msg)
from pyp2p.net import * from pyp2p.unl import UNL from pyp2p.dht_msg import DHT import time #Start Alice's direct server. alice_dht = DHT() alice_direct = Net(passive_bind="192.168.0.45", passive_port=44444, interface="eth0:2", net_type="direct", dht_node=alice_dht, debug=1) alice_direct.start() #Start Bob's direct server. bob_dht = DHT() bob_direct = Net(passive_bind="192.168.0.44", passive_port=44445, interface="eth0:1", net_type="direct", node_type="active", dht_node=bob_dht, debug=1) bob_direct.start() #Callbacks. def success(con): print("Alice successfully connected to Bob.") con.send_line("Sup Bob.")
from pyp2p.net import * from pyp2p.unl import UNL from pyp2p.dht_msg import DHT #Setup Bob's p2p node. #bob = Net(passive_bind="192.168.0.44", passive_port=44445, interface="eth0:1", node_type="passive", debug=1) #bob = Net(passive_port=44445, node_type="passive", debug=1) bob_dht = DHT() bob = Net(passive_bind="192.168.1.105", passive_port=44445, interface="en1", node_type="passive", dht_node=alice_dht, debug=1) bob.start() bob.bootstrap() bob.advertise() #Event loop. while 1: for con in bob: con.send_line("test") time.sleep(1)
def test_relay_link(self): bob = DHT(networking=0) alice = DHT(networking=0) alice.protocol.messages_received = Queue() bob.protocol.messages_received = Queue() bob.add_relay_link(alice) alice.add_relay_link(bob) msg = u"test" bob.send_direct_message(alice.get_id(), msg) assert alice.has_messages() assert alice.get_messages()[0] == msg
request = string_to_bytes('X-Client-STUN-Address: {}:{}'.format(ip, port)) connection.send_line(request) def failure(connection): logger.debug('Cant connect to pool server, shutdown') if __name__ == "__main__": logger.debug('Starting miner application') init_logger(settings) ip, port, interface = get_address() client_dht = DHT() client_node = Net(dht_node=client_dht, passive_bind=ip, passive_port=port, interface=interface, net_type='direct', debug=1) client_node.start() logger.debug('Connecting to pool server') client_node.unl.connect( (settings['pool_server']['ip'], settings['pool_server']['port']), { 'success': success, 'failure': failure })
def test_relay_link(self): bob = DHT(networking=0) alice = DHT(networking=0) bob.add_relay_link(alice) alice.add_relay_link(bob) msg = u"test" bob.send_direct_message(alice.get_id(), msg) assert(alice.has_messages()) assert(alice.get_messages()[0][u"message"] == msg)
def test_reverse_connect(self): # Setup Alice as master. alice_dht = DHT() alice_direct = Net( net_type="direct", node_type="passive", nat_type="preserving", passive_port="34005", dht_node=alice_dht, debug=1 ).start() assert(alice_direct.node_type == "passive") # Setup Bob as slave. bob_dht = DHT() bob_direct = Net( net_type="direct", node_type="active", nat_type="preserving", passive_port="34009", dht_node=bob_dht, debug=1 ).start() assert(bob_direct.node_type == "active") # Setup connection handlers. def success_builder(): def success(con): print("IN SUCCESS HANDLER \a") global found_con found_con = 1 return success events = { "success": success_builder() } # Make Bob connect back to Alice. alice_direct.unl.connect(bob_direct.unl.value, events, hairpin=0) # Process connections. end_time = time.time() + 15 while time.time() < end_time: for direct in [alice_direct, bob_direct]: direct.dht_node.get_messages() for con in direct: print(con) for reply in con: print("Reply in con = ") print(reply) time.sleep(0.5) print("Found con = " + str(found_con)) assert(found_con == 1) assert(len(alice_direct.pending_reverse_queries) == 0) # Close networking. for direct in [alice_direct, bob_direct]: direct.stop()
def test_nonce_synchronization(self): # Setup Alice as master. alice_dht = DHT() alice_direct = Net( net_type="direct", node_type="passive", nat_type="preserving", passive_port="34003", dht_node=alice_dht, debug=1 ).start() assert(alice_direct.node_type == "passive") # Setup Bob as slave. bob_dht = DHT() bob_port = 34010 bob_direct = Net( net_type="direct", node_type="active", nat_type="preserving", passive_port=str(bob_port), dht_node=bob_dht, debug=1 ).start() assert(bob_direct.node_type == "active") # Setup bogus connection on bob. first_con = bob_direct.add_node(get_lan_ip(), bob_port, "passive") assert(first_con is not None) # Accept connections. alice_direct.synchronize() # Setup connection handlers. def success_builder(first_con): def success(con): global success_no success_no += 1 assert(con != first_con) return success def failure(con): assert(0) events = { "success": success_builder(first_con), "failure": failure } # Tell alice to connect but wait on specific connection. nonce = b"something" nonce = hashlib.sha256(nonce).hexdigest() bob_direct.unl.connect(alice_direct.unl.value, events, nonce=nonce, hairpin=0, force_master=0) alice_direct.unl.connect(bob_direct.unl.value, events, nonce=nonce, hairpin=0, force_master=0) # Process connections. end_time = time.time() + 5 while time.time() < end_time: for direct in [alice_direct, bob_direct]: for con in direct: print(con) for reply in con: print("Reply in con = ") print(reply) time.sleep(0.5) assert(success_no == 2) # Close networking. for direct in [alice_direct, bob_direct]: direct.stop()
def test_reverse_connect(self): # Setup Alice as master. alice_dht = DHT() bob_dht = DHT() alice_dht.protocol.messages_received = Queue() bob_dht.protocol.messages_received = Queue() bob_dht.add_relay_link(alice_dht) alice_dht.add_relay_link(bob_dht) alice_direct = Net( net_type="direct", node_type="passive", nat_type="preserving", passive_port=0, dht_node=alice_dht, debug=1 ).start() assert(alice_direct.node_type == "passive") # Setup Bob as slave. bob_direct = Net( net_type="direct", node_type="active", nat_type="preserving", passive_port=0, dht_node=bob_dht, debug=1 ).start() assert(bob_direct.node_type == "active") # Setup connection handlers. def success_builder(): def success(con): print("IN SUCCESS HANDLER \a") global found_con found_con = 1 return success events = { "success": success_builder() } # Make Bob connect back to Alice. alice_direct.unl.connect(bob_direct.unl.value, events, hairpin=0) # Process connections. end_time = time.time() + 15 while time.time() < end_time: for direct in [alice_direct, bob_direct]: direct.dht_node.get_messages() for con in direct: print(con) for reply in con: print("Reply in con = ") print(reply) time.sleep(0.5) print("Found con = " + str(found_con)) assert(found_con == 1) assert(len(alice_direct.pending_reverse_queries) == 0) # Close networking. for direct in [alice_direct, bob_direct]: direct.stop()