def __init__(self, service_details, ren_details, is_new=True): #communication type = UDP = 0 or TCP = 1 self.service_name = service_details["service_name"] self.ip = service_details["ip"] self.port = service_details["port"] self.public_key_dir = service_details["public_key_dir"] self.special_key = service_details["special_key"] self.serial_number = 0 self.communication_type = 0 self.ren_name = ren_details["ren_name"] self.ren_ip = ren_details["ren_ip"] if is_new: VB.print_data("[SERVICES] adding the service to database", VB.GENERAL_DATA) VB.print_data( db.set_data( service.db_dir, '''INSERT INTO services(service_name , service_port ,ip, ren_ip , ren_name, special_key ,public_key_dir, serial_number) VALUES(?,?,?,?,?,?,?,?)''', args=(self.service_name, self.port, self.ip, self.ren_ip, self.ren_name, self.special_key, self.public_key_dir, self.serial_number)), VB.GENERAL_DATA) else: VB.print_data( 'updating data for %s router' % (self.service_name, ), VB.GENERAL_DATA) VB.print_data( db.set_data( service.db_dir, '''UPDATE services SET ip = \'%s\', port = \'%s\', special_key = \'%s\' , ren_ip = \'%s\' WHERE service_name = \'%s\'\ ''' % (self.ip, self.port, self.special_key, self.ren_ip, self.ren_name, self.service_name)), VB.GENERAL_DATA)
def add_service(self, service_details): #[service_details["service_name"]] = (service_details) if self.services == "None": self.services = str(service_details["service_name"] + "%") else: self.services += str(service_details["service_name"] + "%") db.set_data( onion_router.db_dir, "UPDATE onion_routers SET service_str = \'%s\' WHERE router_name = \'%s\'" % ((self.services), self.router_name))
def add_serial_number(self, serial_num): self.serial_number = serial_num VB.print_data( db.set_data( service.db_dir, '''UPDATE services SET serial_number = \'%s\' WHERE service_name = \'%s\'''' % (self.serial_number, self.service_name)), VB.GENERAL_DATA)
def refresh_last_seen(self, load): self.last_seen = time.time() self.load = load return db.set_data( onion_router.db_dir, str('''UPDATE onion_routers SET last_seen = \'%s\', load = \'%s\' WHERE router_name = \'%s\' ''' % (str(self.last_seen), self.load, self.router_name)))
def delete_service(service_details): global_variables.VB.print_data("SERVICE DELETED SUCCESSFULLY", 0) global_variables.LOAD_LEVEL -= 1 return str( db.set_data( global_variables.SERVICES_DB_DIR, '''DELETE FROM services WHERE service_name = \'%s\'''' % (service_details["service_name"])))
def set_special_key(self): self.special_key = hashlib.md5(self.service_name) VB.print_data( db.set_data( service.db_dir, '''UPDATE services SET special_key = \'%s\' WHERE service_name = \'%s\'\ ''' % (self.special_key, self.service_name)), VB.GENERAL_DATA)
def change_ren(self, ren_details): self.ren_name = ren_details["ren_name"] self.ren_ip = ren_details["ren_ip"] VB.print_data( db.set_data( service.db_dir, '''UPDATE services SET ren_ip = \'%s\', ren_name = \'%s\' WHERE service_name = \'%s\'''' % (self.ren_ip, self.ren_name, self.service_name)), VB.GENERAL_DATA)
def add_service(service_details): upload_done = db.set_data(global_variables.SERVICES_DB_DIR, '''INSERT INTO services(service_name, ip, port, communication_type) VALUES(?,?,?,?)''',\ args= (service_details["service_name"], service_details["service_ip"], service_details["service_port"], service_details["service_communication_type"])) if upload_done: global_variables.VB.print_data("SERVICE UPLOADED SUCCESSFULLY", global_variables.VB.GENERAL_DATA) global_variables.SERVICES[global_variables.SERIAL_NUM_SERVICES] = ( service_details) global_variables.LOAD_LEVEL += 1 return global_variables.SERIAL_NUM_SERVICES return False
def __init__(self, router_details): #services <---> array of (name , ip,port, communication_type) - the router is the ren point of the services self.router_name = router_details["router_name"] self.ip = router_details["ip"] self.load = 0 self.is_available = True self.port = router_details["port"] self.last_seen = time.time() self.public_key_dir = router_details["public_key_dir"] self.is_exist_in_database = False if self.is_exist_in_database: self.services = (db.get_data( onion_router.db_dir, "SELECT service_str from onion_routers WHERE router_name = %s" % (self.router_name, ))) #print 'updating data for %s router'%(self.router_name,) VB.print_data( db.set_data( onion_router.db_dir, '''UPDATE onion_routers SET ip = \'%s\', port = \'%s\', is_available = \'%s\', last_seen = \'%s\', service_str = \'%s\'\ WHERE router_name = \'%s\'''' % (self.ip, self.port, int(self.is_available), self.last_seen, self.router_name, (self.services))), VB.GENERAL_DATA) else: #print 'new router added to the database' self.services = "None" VB.print_data( db.set_data( onion_router.db_dir, '''INSERT INTO onion_routers(router_name, ip, port, load, is_available,last_seen,public_key_dir, service_str)\ VALUES(?,?,?,?,?,?,?,?)''', args=(self.router_name, self.ip, self.port, self.load, int(self.is_available), str(self.last_seen), self.public_key_dir, (self.services))), VB.GENERAL_DATA)
def disconnect(self): VB.print_data( db.set_data( service.db_dir, '''DELETE FROM services WHERE service_name = \'%s\'''' % (self.service_name, ), VB.GENERAL_DATA))
def close_router(self): self.is_available = 0 return db.set_data( ONION_ROUTERS_DB_DIR, '''DELETE FROM onion_routers WHERE router_name = \'%s\'''' % (self.router_name, ))