def _handle_packet(self, onion_pkt): udp_data = onion_pkt[UDP].payload.build() try: seperator_index = udp_data.index(':') key_comm_header = udp_data[seperator_index + 1:seperator_index + onion_encryption_decryption.KEYS_LEN + 1] dec_sym_key = onion_encryption_decryption.RSA_Decryption( key_comm_header, self.private_key) encrypted_pkt = bytes( udp_data[seperator_index + onion_encryption_decryption.KEYS_LEN + 1:]) recv_data = json_handler.recieve_json( onion_encryption_decryption.sym_decryption( encrypted_pkt, dec_sym_key)) if recv_data["serial_num"] != 'End': self.splitted_data[recv_data["serial_num"]] = ( recv_data["data"]) else: self.splitted_data[recv_data["serial_num"]] = int( recv_data["data"]) return self.connect_data() if time.time() > self.timeout: self.data_from_service = None return True except: self.data_from_service = None return True
def ask_to_service(self, onion_url): while 1: self.server_sock.send(json_handler.create_json(onion_url)) data = json_handler.recieve_json(self.server_sock.recv( self.bufsiz)) if data["state"] == json_handler.STATE_FAILED: raise ClientErrors('Check your url') break elif data["state"] == json_handler.STATE_SEND_AGAIN: self.server_sock.send(json_handler.create_json(onion_url)) pass else: self.communication_details = data["args"] print self.communication_details break self.server_sock.close() if self.send_key_to_service(): #self.sent_key_semaphore.release() self.data_from_service = '' self.splitted_data = {} print 'yep' if self.is_web_client: webbrowser.open(self.reqs_url) return True return False
def handle_keep_alive(server_sock): """ handle the keep alive connection between the server """ #print server_sock.stillconnected() keep_alive_details = { "load": global_variables.LOAD_LEVEL, "router_name": global_variables.ROUTER_NAME } while 1: try: msg = json_handler.create_json( json_handler.ONION_ROUTER_KEEP_ALIVE, keep_alive_details) global_variables.VB.print_data(msg, global_variables.VB.KEEP_ALIVE) print msg server_sock.send(msg) d = server_sock.recv(global_variables.BUFSIZ) data = json_handler.recieve_json(d) global_variables.VB.print_data("RECIEVED KEEP ALIVE MSG", global_variables.VB.KEEP_ALIVE) print data if not data: break elif "new_service" in data["args"].keys(): keep_alive_details["service_added"] = ( add_service(data["args"]["new_service"]) ) #need to create handle for errors. keep_alive_details["load"] = global_variables.LOAD_LEVEL global_variables.SERIAL_NUM_SERVICES += 1 elif "delete_service" in data["args"].keys(): keep_alive_details["service_deleted"] = delete_service( data["args"]["delete_service"]) keep_alive_details["load"] = global_variables.LOAD_LEVEL else: if "service_added" in keep_alive_details.keys(): del keep_alive_details["service_added"] if "service_deleted" in keep_alive_details.keys(): del keep_alive_details["service_deleted"] time.sleep(4) except Exception as e: global_variables.VB.print_data(e, global_variables.VB.ERRORS) break
def register_service(self): """ handle the first communication with the dir_server comm_type: - 0-UDP 1-TCp """ def error_handle(recv_data): print recv_data return False service_details = { "service_name": self.service_name, "ip": self.udp_ip, "port": self.udp_port, "communication_type": 0, #only udp yet "public_key": self.public_key, "special_key": self.special_key } print service_details state_handler = { json_handler.STATE_SUCCEED: lambda recv_data: True, json_handler.STATE_SEND_AGAIN: lambda sock, register_json: self.dir_server_socket.send( register_json), json_handler.STATE_FAILED: lambda recv_data: error_handle(recv_data) } register_json = json_handler.create_json(json_handler.SERVICE_REGISTER, service_details) self.dir_server_socket.send(register_json) while 1: data = json_handler.recieve_json( self.dir_server_socket.recv(self.bufsiz)) if not data: return False elif data["state"] != json_handler.STATE_SEND_AGAIN: return state_handler[data["state"]](data['args']) state_handler[data["state"]](self.dir_server_socket, register_json)
def disconnect_network(self): details = { "special_key": self.special_key, "service_name": self.service_name } disconnect_json = json_handler.create_json( json_handler.SERVICE_DISCONNECT, details) self.dir_server_socket.send(disconnect_json) while 1: data = json_handler.recieve_json( self.dir_server_socket.recv(self.bufsiz)) if not data: self.dir_server_socket.close() break elif data["state"] != json_handler.STATE_SEND_AGAIN: self.dir_server_socket.close() return True else: self.dir_server_socket.send(disconnect_json) return False