def start_client(): ## Alice #inputs = [sys.stdin] # Array of all input select has to look for # (standard input and socket, does not work on windows) print('Successfully connected to other user.') # message to the client that the connection worked print("I AM ALICE") alice = Alice(identifier_other='Bob') #print("X3DH status:", alice.x3dh_status) event_list = cf.get_all_saved_events(1) #print('len(event_list):', len(event_list)) content = None if len(event_list) > 0: last_event = event_list[-1] content, meta = last_event feed_id = Meta.from_cbor(meta).feed_id if content[0] == 'ratchet/contactInfo': if alice.x3dh_status == 0: # received_keys = local_sock.recv(224) #received_keys = retrieve_msg_local() key_bundle_to_send = alice.x3dh_create_key_bundle_from_received_key_bundle(content[1]['key_bundle']) # local_sock.send(key_bundle_to_send) #send_msg_local(key_bundle_to_send) #print('befor send to send:', (key_bundle_to_send, ecf.get_feed_id(), feed_id)) send_second_Bacnet_prekey_bundle(key_bundle_to_send, ecf.get_feed_id(), feed_id) if ecf.get_feed_id() != feed_id: own_last_event_sequence_number = function.get_current_seq_no(ecf.get_feed_id()) own_last_event = Event.from_cbor(function.get_event(ecf.get_feed_id(), own_last_event_sequence_number)) last_own_message_not_reached = True for x in event_list: if x[0][0] != 'ratchet/message': continue if last_own_message_not_reached: if x[0][1]['ciphertext'] == own_last_event.content.content[1]['ciphertext']: last_own_message_not_reached = False continue continue #print(x[0][1]['ciphertext']) received_message_raw = expose_message_tcp(x[0][1]['ciphertext'], alice) print("Received:", received_message_raw) while True: try: msg = input('Please enter your message: ') if msg == 'quit': break send_Bacnet_msg(encapsulate_message_tcp(alice, msg), feed_id, ecf.get_feed_id()) except KeyboardInterrupt: print('Interrupted') sys.exit(0) running = True sentKeys = False
def retrieve_new_messages_alice(alice) -> None: #print("X3DH status:", alice.x3dh_status) event_list = cf.get_all_saved_events(1) # print('len(event_list):', len(event_list)) content = None if len(event_list) > 0: last_event = event_list[-1] content, meta = last_event feed_id = Meta.from_cbor(meta).feed_id if content[0] == 'ratchet/contactInfo': if alice.x3dh_status == 0: # received_keys = local_sock.recv(224) # received_keys = retrieve_msg_local() key_bundle_to_send = alice.x3dh_create_key_bundle_from_received_key_bundle(content[1]['key_bundle']) # local_sock.send(key_bundle_to_send) # send_msg_local(key_bundle_to_send) # print('befor send to send:', (key_bundle_to_send, ecf.get_feed_id(), feed_id)) send_second_Bacnet_prekey_bundle(key_bundle_to_send, ecf.get_feed_id(), feed_id) send_Bacnet_msg( encapsulate_message_tcp(alice, "[This is a necessary first message, but you can ignore it.]"), feed_id, ecf.get_feed_id()) send_rsync() alice.x3dh_status = 2 if ecf.get_feed_id() != feed_id: own_last_event_sequence_number = function.get_current_seq_no(ecf.get_feed_id()) own_last_event = Event.from_cbor(function.get_event(ecf.get_feed_id(), own_last_event_sequence_number)) last_own_message_not_reached = True for x in event_list: if x[0][0] != 'ratchet/message': continue if last_own_message_not_reached: if x[0][1]['ciphertext'] == own_last_event.content.content[1]['ciphertext']: last_own_message_not_reached = False continue continue #print(x[0][1]['ciphertext']) received_message_raw = expose_message_tcp(x[0][1]['ciphertext'], alice) print("Received:", received_message_raw)
def retrieve_new_messages_bob(bob) -> None: #print("Status:", bob.x3dh_status) event_list = cf.get_all_saved_events(1) if len(event_list) > 0: last_event = event_list[-1] content, meta = last_event feed_id = Meta.from_cbor(meta).feed_id if ecf.get_feed_id() != feed_id: own_last_event_sequence_number = function.get_current_seq_no(ecf.get_feed_id()) own_last_event = Event.from_cbor(function.get_event(ecf.get_feed_id(), own_last_event_sequence_number)) last_own_message_not_reached = True for x in event_list: if own_last_event.content.content[0] == 'ratchet/contactInfo': if x[0][0] != 'ratchet/message': last_own_message_not_reached = False continue elif own_last_event.content.content[0] == 'ratchet/message': if x[0][0] != 'ratchet/message': continue if last_own_message_not_reached: if x[0][1]['ciphertext'] == own_last_event.content.content[1]['ciphertext']: last_own_message_not_reached = False continue continue # print('print msg') received_message_raw = expose_message_tcp(x[0][1]['ciphertext'], bob) print("Received:", received_message_raw) else: print('no new messages')
def start_server(): ## Bob server_sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM) # Created the datagram socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # checks your own ip address s.connect(("8.8.8.8", 80)) ip_address = s.getsockname()[0] port = 0 server_sock.bind( (ip_address, port)) # bind it to the wished ip and some port server_sock.listen(1) # connection to only one client print('server started with: ip {} port {}'.format( server_sock.getsockname()[0], server_sock.getsockname()[1])) # outputs the local ip and port try: conn, addr = server_sock.accept( ) # Wait for a connection to the client except KeyboardInterrupt: return 1 print('Other user arrived. Connection address:', addr) # prints the ip and port of the clients print("I AM BOB") bob = Bob(identifier_other='Alice') #print("Status:", bob.x3dh_status) if bob.x3dh_status == 0: prekey_bundle = bob.x3dh_1_create_prekey_bundle() # TODO (identifier_other comes from bacnet): save_prekeys(prekey_bundle, identifier_other) conn.send(prekey_bundle) # send_msg_local(prekey_bundle) # exit() bob.x3dh_status = 1 if bob.x3dh_status == 1: alice_key_bundle = conn.recv(64) # alice_key_bundle = retrieve_msg_local() # TODO: delete_prekeys(identifier_other) bob.x3dh_2_complete_transaction_with_alice_keys(alice_key_bundle) bob.x3dh_status = 2 if bob.x3dh_status == 2: pass print("Waiting for an initial message from alice...") recvd_message = conn.recv(buffer_size) print("Received:", expose_message_tcp(message=recvd_message, person=bob)) # msg_hialice = "Hi Alice! How are you?" # send_tcp(socket=conn, person=bob, message=msg_hialice) # print("[Bob] sent:", msg_hialice) inputs = [conn, sys.stdin] # Array of all input select has to look for running = True receivedKeyPair = False while running: try: in_rec, out_rec, ex_rec = select.select( inputs, [], []) # Try to receive input from the socket # and save it in in_rec except KeyboardInterrupt: # Catch Interrupts which happen if one shuts down the program forcefully print('Closed the connection') conn.send('quit'.encode('UTF8')) break for msgs in in_rec: # Work up all the received messages saved in in_rec if msgs is conn: try: new_message = conn.recv( buffer_size) # reads the incoming messages try: msg = new_message.decode('utf-8').rstrip() if 'quit' == msg: print('Connection closed by other user') running = False return except UnicodeDecodeError: pass # print("[Bob] received:", recv_tcp(socket=conn, person=bob)) #prints the messages print( "Received:", expose_message_tcp(message=new_message, person=bob)) # prints the messages except socket.error: print('Could not read from socket') running = False return elif msgs is sys.stdin: line = sys.stdin.readline().rstrip( ) # reads the messages from the server bytes_to_send = encapsulate_message_tcp( person=bob, message=line) # sends the messages conn.send(bytes_to_send) else: break server_sock.close() # Close the socket if while is left
def start_client(local_sock): ## Alice inputs = [local_sock, sys.stdin] # Array of all input select has to look for # (standard input and socket, does not work on windows) print('Successfully connected to other user.' ) # message to the client that the connection worked print("I AM ALICE") alice = Alice(identifier_other='Bob') #print("X3DH status:", alice.x3dh_status) if alice.x3dh_status == 0: received_keys = local_sock.recv(224) # received_keys = retrieve_msg_local() key_bundle_to_send = alice.x3dh_create_key_bundle_from_received_key_bundle( received_keys) local_sock.send(key_bundle_to_send) # send_msg_local(key_bundle_to_send) if alice.x3dh_status == 2: pass # msg_to_bob = 'Hello, Bob!' # send_tcp(socket=local_sock, person=alice, message=msg_to_bob) # print("[Alice] sent:", msg_to_bob) # print("[Alice] received:", recv_tcp(socket=local_sock, person=alice)) running = True sentKeys = False while running: try: in_rec, out_rec, ex_rec = select.select( inputs, [], []) # Let select save all the incoming input to in_rec except KeyboardInterrupt: # Catch Interrupts which happen if one shuts down the program forcefully print('Closed the connection') local_sock.send( 'quit'.encode('UTF8') ) # sends the message to the server that the client socket closes break for msgs in in_rec: # Work up all the received messages saved in in_rec if msgs is local_sock: # Case message is from socket try: new_message = local_sock.recv(buffer_size) try: msg = new_message.decode().rstrip() if 'quit' == msg: # see if it is a quit message print('Connection closed by other user') running = False return except UnicodeDecodeError: pass # We read message event pkg # We extract the # We decipher the message # message = recv_tcp(socket=local_sock, person=alice) message = expose_message_tcp(message=new_message, person=alice) print('Received:', message) # outputs the message except socket.error: print('Could not read from socket') running = False return elif msgs is sys.stdin: # case message is from standard input line = sys.stdin.readline().rstrip( ) # reads the messages from the client # Create message event to be sent # Send message event bytes_to_send = encapsulate_message_tcp( person=alice, message=line) # sends the messages from the client local_sock.send(bytes_to_send) else: break local_sock.close() # Close the socket if while is left
def start_server(): ## Bob print("I AM BOB") bob = Bob(identifier_other='Alice') #print("Status:", bob.x3dh_status) event_list = cf.get_all_saved_events(1) if len(event_list) > 0: last_event = event_list[-1] content, meta = last_event feed_id = Meta.from_cbor(meta).feed_id if bob.x3dh_status == 0: prekey_bundle = bob.x3dh_1_create_prekey_bundle() # TODO (identifier_other comes from bacnet): save_prekeys(prekey_bundle, identifier_other) send_first_Bacnet_prekey_bundle(ecf.get_feed_id(), prekey_bundle) exit() if event_list[1][0][0] == 'ratchet/connect' and bob.x3dh_status == 1: if bob.x3dh_status == 1: #print(event_list[1][0][1]['key_bundle']) bob.x3dh_2_complete_transaction_with_alice_keys(event_list[1][0][1]['key_bundle']) print("Waiting for an initial message from alice...") bob.x3dh_status = 2 if content[0] == 'ratchet/message' or content[0] == 'ratchet/connect': if bob.x3dh_status == 2: if ecf.get_feed_id() != feed_id: own_last_event_sequence_number = function.get_current_seq_no(ecf.get_feed_id()) own_last_event = Event.from_cbor(function.get_event(ecf.get_feed_id(), own_last_event_sequence_number)) last_own_message_not_reached = True for x in event_list: if own_last_event.content.content[0] == 'ratchet/contactInfo': if x[0][0] != 'ratchet/message': last_own_message_not_reached = False continue elif own_last_event.content.content[0] == 'ratchet/message': if x[0][0] != 'ratchet/message': continue if last_own_message_not_reached: if x[0][1]['ciphertext'] == own_last_event.content.content[1]['ciphertext']: last_own_message_not_reached = False continue continue #print('print msg') received_message_raw = expose_message_tcp(x[0][1]['ciphertext'], bob) print("Received:", received_message_raw) else: print('no new messages') while True: try: msg = input('Please enter your message: ') if msg == 'quit': break send_Bacnet_msg(encapsulate_message_tcp(bob, msg), feed_id, ecf.get_feed_id()) except KeyboardInterrupt: print('Interrupted') sys.exit(0)