def server_run(args): server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind((args.host, args.port)) server_socket.listen() print('Kernel tuning daemon ready') while True: print('Wait for request...') client_socket, addr = server_socket.accept() data_len = struct.unpack("<i", recvall(client_socket, 4))[0] data = recvall(client_socket, data_len) print('Data received (length=%d)' % (data_len)) client_socket.sendall('Task received successfully'.encode()) client_socket.close() # Tuning start data = pickle.loads(data) kernel_task = KernelTask(data['task'], data['target'], data['device_key']) kernel_task.tune() server_socket.close()
def get_block(self): """ download a file block from the server """ print('* Getting a block from the server:') self.soc.send(ut.encode_str(str(self.p_port), ut.data_unit)) # send1-1: port of client recv_buffer = ut.recvall(self.soc, ut.data_unit) self.idx_clt = int( ut.decode_str(recv_buffer)) # receive1-2: index of client print('Index of current peer:', self.idx_clt) recv_buffer = ut.recvall(self.soc, ut.data_unit) self.num_peer = int( ut.decode_str(recv_buffer)) # receive1-3: number of peers print('Number of peers:', self.num_peer) self.info_pool = [None] * self.num_peer recv_buffer = ut.recvall( self.soc, ut.data_unit) # receive1-4: information of all peers info_clts = ut.decode_str(recv_buffer).split('|') for i in range(self.num_peer): info_clt = info_clts[i].split(';') # (ip, port) self.info_pool[i] = (info_clt[0], int(info_clt[1])) print('Information of peers:', self.info_pool) recv_buffer = ut.recvall(self.soc, ut.data_unit) self.file_len = int( ut.decode_str(recv_buffer)) # receive1-5: file length print('Length of the file:', self.file_len) self.block_name = self.downfile_folder + '/' + str(self.idx_clt) print('Name of the block:', self.block_name) recv_buffer = ut.recvall(self.soc, ut.data_unit) self.block_len = int( ut.decode_str(recv_buffer)) # receive1-6: block length print('Length of the block:', self.block_len) sys.stdout.flush() # receive a block of file received_size = 0 local_file = open(self.block_name, 'wb') while received_size != self.block_len: received_data = self.soc.recv( ut.data_unit) # receive1-7: file block # print('received:', len(received_data)) local_file.write(received_data) received_size += len(received_data) local_file.close() print('Downloading of block finished!\n') sys.stdout.flush() self.has_received = [False] * self.num_peer self.has_received[self.idx_clt] = True
def handle(self): try: while not self.server._kill: buf = utils.recvall(self.request, 4) length, = struct.unpack('!I', buf) data = json.loads(utils.recvall(self.request, length)) # create message so that other_name is the car that sent the message message = Message(data['type'], None, data['name'], data['location'], data['frame_num'], priority_val=data['priority_val']) self.server._queue.put(message) time.sleep(utils.THREAD_SLEEP) except socket.error: print "socket died" except Exception as e: print "Something bad happened:", e
def handle(sock, addr): try: req = json.loads(utils.recvall(sock)) resp = shares.get_chunk(req['share_id'], req['chunk_id']) print "sent", (req['share_id'], req['chunk_id']) utils.sendall(sock, resp) except Exception, e: if not e.message == "socket connection broken": msg = "ERROR" print e.message print e.__class__ sock.sendall(msg)
def get_share(share_id): """ Make a TCP connection to the registry and fetch the share """ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((registry_hostname, registry_port)) s.send(json.dumps({ "method": "get_share", "args": { "id": share_id } })) buff = recvall(s) s.close() return json.loads(buff)
def conn_handler(name, conn, db): def tprint(*args, **kwargs): """Custom print function for threads""" print('___' + str(name) + " ".join(map(str, args)) + "XXX", **kwargs) tprint('Thread starting') try: buf = recvall(conn) data = json.loads(buf.decode()) tprint(data) if data['operation'] == 'store': device = json.loads(data['data']) db.save_device(device) db.save_db() except: tprint("error")
def run(self): self.server_conn.settimeout(TIMEOUT_REQ_CONNECTION) try: self.server_conn.connect((EVERYTHING_SERVER_IP, ETP_PORT)) except SOCKET_TIMEOUT: print("[!] [SOCKET_TIMEOUT] No Connection with ETP-Server at " + str((EVERYTHING_SERVER_IP, ETP_PORT)), file=sys.stderr) self.close_connection() return except Exception as e: print("[!] No Connection with ETP-Server at %s. Error: %s" % (str( (EVERYTHING_SERVER_IP, ETP_PORT)), str(e)), file=sys.stderr) self.close_connection() return message_queues = {self.server_conn: Queue(), self.client_conn: Queue()} while self.conns: readable, writable, exceptional = select( self.conns, self.conns, [], TIMEOUT_CLIENT_CONNECTION) if not (readable or writable or exceptional): self.stop() for sck in readable: dst_sck = self.client_conn \ if sck is self.server_conn \ else self.server_conn data = recvall(sck) if not data: self.stop() break # Manipulating the Data: new_data = self.get_manipulated_data(dst_sck, data) message_queues[dst_sck].put(new_data) for sck in writable: if not message_queues[sck].empty(): next_msg = message_queues[sck].get_nowait() prefix = "[server2client]" if sck is self.server_conn: prefix = "[client2server]" vprint(prefix, next_msg) sck.send(next_msg)
def get_file(self, file_name): """ download a file from the server """ print('* Downloading file from the server:') recv_buffer = ut.recvall(self.soc, ut.data_unit) file_len = int(ut.decode_str(recv_buffer)) # receive1: file length print('Length of the file:', str(file_len)) sys.stdout.flush() received_size = 0 local_file = open(file_name, 'wb') while file_len != received_size: received_data = self.soc.recv(ut.data_unit) # receive2: file local_file.write(received_data) received_size += len(received_data) local_file.close() print('Downloading finished!\n') sys.stdout.flush() self.soc.close()
def get_other_block(self, idx): """ require a block from another peer """ p_server_ip = self.info_pool[idx][0] p_server_port = self.info_pool[idx][1] p_clt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) p_clt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) p_clt.connect((p_server_ip, p_server_port)) recv_buffer = ut.recvall(p_clt, ut.data_unit) p_block_len = int( ut.decode_str(recv_buffer)) # receive2-1: block length received_size = 0 local_file = open(self.downfile_folder + '/' + str(idx), 'wb') while received_size != p_block_len: received_data = p_clt.recv(ut.data_unit) # receive2-2: file block local_file.write(received_data) received_size += len(received_data) local_file.close() print('Downloading of block %d finished!\n' % idx) sys.stdout.flush() p_clt.close() self.has_received[idx] = True
def receive_messages(self): while True: # message received from server data = utils.recvall(self.socket) self.task_queue.put(resolve_task(data))
def get_block(sock): data = recvall(sock, header_struct.size) (block_length,) = header_struct.unpack(data) return recvall(sock, block_length)
def send_file(self, clt, addr, idx_clt, file_name): """ send blocks of files """ recv_buffer = ut.recvall(clt, ut.data_unit) # receive1-1: port of client clt_port = int(ut.decode_str(recv_buffer)) self.info_pool[idx_clt] = (addr[0], clt_port) while True: if len(self.clt_pool ) == self.num_peer and None not in self.info_pool: clt.send(ut.encode_str( str(idx_clt), ut.data_unit)) # send1-2: index of client clt.send(ut.encode_str(str( self.num_peer), ut.data_unit)) # send1-3: number of peers all_clt_info = '' for i in range(self.num_peer): ip_i = self.info_pool[i][0] port_i = self.info_pool[i][1] all_clt_info = all_clt_info + ip_i + ';' + str( port_i) + '|' all_clt_info = all_clt_info[:-1] clt.send(ut.encode_str( all_clt_info, ut.data_unit)) # send1-4: information of all clients data_file = open(file_name, 'rb') file_len = len(data_file.read()) clt.send(ut.encode_str(str(file_len), ut.data_unit)) # send1-5: file length if idx_clt == len(self.clt_pool) - 1: block_len = file_len - math.ceil( file_len / self.num_peer) * idx_clt else: block_len = math.ceil(file_len / self.num_peer) clt.send(ut.encode_str(str(block_len), ut.data_unit)) # send1-6: block length # send a block of file send_size = 0 data_file.seek(idx_clt * math.ceil(file_len / self.num_peer), 0) while send_size != block_len: if send_size + ut.data_unit <= block_len: data_trans = data_file.read(ut.data_unit) clt.send(data_trans) # send1-7: file block send_size += len(data_trans) # print('send size', send_size) # print('Transmission progress: %d:%d' % (send_size, block_len)) else: data_trans = data_file.read(block_len - send_size) clt.send(data_trans) # send1-7: file block send_size += len(data_trans) # print('send size', send_size) # print('Transmission progress: %d:%d' % (send_size, block_len)) print('Transmission of block %d completed!\n' % idx_clt) sys.stdout.flush() data_file.close() clt.close() return
def get_block(sock): data = recvall(sock, header_struct.size) (block_length, ) = header_struct.unpack(data) return recvall(sock, block_length)
def do_handshake(root_ca_crt, to_send, HOST, PORT): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: # Connect to server and send data sock.connect((HOST, PORT)) protocol = custom_protocol.DHFernet() # get certificate crt_b = recvall(sock) print("[handshake] got scan certificate") crt: x509.Certificate = x509.load_pem_x509_certificate( crt_b, default_backend()) if crt is None: print("[handshake error] expected certificate, got something else") return False try: # verify certificate # https://cryptography.io/en/latest/x509/reference/#cryptography.x509.Certificate.tbs_certificate_bytes root_ca_crt.public_key().verify( crt.signature, crt.tbs_certificate_bytes, # Depends on the algorithm used to create the certificate padding.PKCS1v15(), crt.signature_hash_algorithm, ) # TODO: verificar not_valid_{after,before} except InvalidSignature: print("[handshake error] invalid certificate signature") return False print("[handshake] certificate ok") # cipher DHFernet public key with certificate public key # https://cryptography.io/en/latest/hazmat/primitives/asymmetric/rsa/#encryption ciphertext = crt.public_key().encrypt( protocol.get_public_key(), padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)) sock.sendall(ciphertext) print("[handshake] sent DH public key") # get peer_public_key signed with certificate private key ( and nonce - 1 ) message = recvall(sock) message_json = json.loads(message.decode()) signature = message_json['signature'] message_content = message_json['message'] try: crt.public_key().verify( bytes.fromhex(signature), bytes.fromhex(message_content), padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH), hashes.SHA256()) except InvalidSignature: print("[handshake error] invalid signature") return False print("[handshake] got DH public key") # decipher with certificate public key and DHFernet calculate shared secret key protocol.set_peer_public_key(bytes.fromhex(message_content)) print("[handshake] got shared secret") random_challenge = os.urandom(16) challenge_int = int.from_bytes(random_challenge, byteorder='little') challenge = str(challenge_int) message = protocol.encrypt(challenge.encode()) sock.sendall(message) message = recvall(sock) response = protocol.decrypt(message) response_int = int(response.decode()) if response_int != (challenge_int - 1): print("[handshake error] failed challenge") return False print("[handshake] challenge response ok") confirm = b"handshake ok" message = protocol.encrypt(confirm) sock.sendall(message) message = recvall(sock) print("[handshake] got confirmation") confirm = protocol.decrypt(message) if confirm.decode() != "handshake ok": return False print("[handshake] done") # send mac address encrypted with passphrase ct = json.dumps(to_send) print("sending ct") message = protocol.encrypt(ct.encode()) sock.sendall(message) return True