def listen(self): while True: clientsock, addr = self._sock.accept() address = Address(addr[0], addr[1]) self._clients[str(address)] = clientsock self.Logger.info('Connected from:', addr) listenThread = threading.Thread(target=self.handler, args=(clientsock, address)) listenThread.start()
def connect(): ipaddr = input("Enter the ip address [127.0.0.1]: ") if not ipaddr: ipaddr = '127.0.0.1' port = input("Enter the port number [2983]: ") if not port: port = '2983' addr = Address(ipaddr, int(port)) global CLIENT CLIENT = Client(addr)
def encoded(self, value): end_size = struct.calcsize(self.tag_struct) h = value[:-end_size] raw = struct.unpack(self.tag_struct, value[-end_size:]) contact = self.translator( Address('.'.join(str(x) for x in raw[:4]), raw[4])) try: contact.set_hash(Hash(h)) # Happens if the hash has already been set. except ValueError: pass self._value = contact
def __init__(self): import common.config as config self.config = config.get() from common import Contact, Address, Hash self.dir = self.config['general']['config_dir'] self.bitsize = self.config['kademlia']['keysize'] print("Using dir: %s" % self.dir) from crypto.rsa import KeyPair # TODO: Re-enable cert loading # Get the cert and the hash for it # keyblob = open(self.config['kademlia']['keyfile'], 'r').read() # self.keypair = KeyPair(private=keyblob) # TODO: Load pub / priv, base hash off of this. hash_ = Hash(os.urandom(self.bitsize // 8)) # TODO: load this from config self.dh_group = self.config['kademlia']['group'] # Set up the networking core if self.config['net']['randomize_port']: port = self.new_port() else: port = self.config['net']['port'] import net.ipfinder from net import Stack from kademlia import Kademlia addr = Address(net.ipfinder.check_in(), port) self.contact = Contact(addr, hash_) self.net = Stack(port, self.dh_group) self.kademlia = Kademlia(self.net, self.contact, self.dir, self.config['kademlia']['bucket_size'], self.bitsize, self.config['kademlia']['paralellism']) try: from ui.server import UIServer self.uiserver = UIServer(self.config['net']['ui_port'], self.config['net']['max_ui_conns']) # Bind all the properties # This should probably be moved to a model somewhere props = {'contact_table': 'net._contacts'} for name, var_path in props.items(): self.uiserver.add_property(name, var_path, self) except socket.error: print("UI server port taken. Starting without UI")
def listen(self): """ Main while (True) loop to receive messages. """ while (True): raw_data, address = self._sock.recvfrom(65536) # print('received '+ str(len(data)) +' bytes from '+ str(address)) try: address = Address(address[0], address[1]) self.on_data(address, raw_data) except: Logger.error("----- Receive Error -----") Logger.error("From: %s, Data: %s", address, raw_data) Logger.error(traceback.format_exc())
def add_contact(): s = state.get() ip = input("IP Address [127.0.0.1]: ") if (not ip): ip = '127.0.0.1' port = int(input("Port: ")) contact = s.net._contacts.translate(Address(ip, port)) s.net.send_data(contact, 'hello', {'hash': s.contact.hash}) # Make it so we seed once we join the DHT def start_search(contact): s.kademlia.buckets.on_added -= start_search s.kademlia.init_search(s.contact.hash) s.kademlia.buckets.on_added += start_search
This function watches for any RPCs that must be made. It is the only one that should modify the datavalues of the shorts. .. note:: This call is blocking, so run it threaded. """ while (True): while (self._task_queue.qsize() > 0): task = self._task_queue.get() if (task[0] != self._clean_lists): Logger.debug('TASK: %s', str(task)) task[0](*(task[1])) sleep(0.5) self._task_queue.put((self._clean_lists, ())) # Placeholder for when a search is triggered. # Used for testing only. def Search(contact): print('SEARCH:', contact) from os import urandom if (__name__ == '__main__'): shortie = Shortlists(Search) while (True): shortie.start_search(Hash(urandom(20)), [ Contact(Address('192.168.0.1', 4000), Hash(b'12345678901234567890')) ]) sleep(1)
def encoded(self, value): raw = struct.unpack(self.tag_struct, value) self._value = Address('.'.join(str(x) for x in raw[:4]), raw[4])