def start_server_listener(the_host_name=HOST_NAME, the_max_clients=MAX_CLIENTS): ''' Starts the Secure Server, if successfully also starts listening or a client/chat partner to connect. ''' try: secure_utils.clear_screen() # clear screen for nice output secure_utils.draw_server_logo() # draws logo my_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) my_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) my_server_socket.bind(('', PORT_NUMBER)) my_server_socket.listen(the_max_clients) print "\033[1;43m[+] The Chat Server Successfully Started\033[1;m" print "\033[1;43m[+] Now accepting chat clients\033[1;m\n" # Blocking call to accept connections (my_client_connection, my_client_address) = my_server_socket.accept() print "[+] User {0} at {1} is now connected to the server".format(socket.gethostname(), my_client_address[0]) if (my_client_address[0] in secure_utils.populated_banned_list()): # check if this user is banned from connecting, send appropriate msg print "[+] User at {0} is banned from this server... disconnecting".format(my_client_address[0]) my_client_connection.send("Its unfortunate, but you are banned from connecting to this server") return 0 else: my_client_connection.send("Send Keys") # Tell other party to send their public keys. # wait to receive client's public key (publicKeyTuple, privateKeyTuple) = exchange_keys_with_client(my_client_connection) print '\n\033[1;42m*******Type your message below and hit enter to send. Type \'EXIT\' to end conversation.*******\033[1;m\n' ReadAndDecryptThread = ThreadManager('read', my_client_connection, publicKeyTuple, privateKeyTuple) # Thread for reading data and decrypting WriteAndEcnryptThread = ThreadManager('write', my_client_connection, publicKeyTuple, privateKeyTuple) # Thread for writing data and decrypting ReadAndDecryptThread.start() # Start the read and decrypt thread WriteAndEcnryptThread.start() # Starts the writing and encrypting thread # wait until client dc's ReadAndDecryptThread.join() print '[+] Your partner has left the conversation. Press any key to continue...\n' # stop the write thread WriteAndEcnryptThread.stop_write_loop() WriteAndEcnryptThread.join() # shut down client connection try: my_client_connection.shutdown(socket.SHUT_RDWR) my_client_connection.close() except: # connection already closed pass # shut down server secure_utils.display_processing_cursor('Shutting server down...') my_server_socket.shutdown(socket.SHUT_RDWR) my_server_socket.close() return 0 except socket.error as socket_error: print socket_error[1] raw_input("\nPress [ENTER] To Return To The Main Menu...\n") # print "[-] There was problem starting the server" pass
def connect_to_server(): ''' Starts a connection to the server ''' try: secure_utils.clear_screen() # clear screen for nice output secure_utils.draw_client_logo() # my_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) my_client_socket.connect((HOST_NAME, PORT_NUMBER)) print_welcome_message(socket.gethostname(), my_client_socket.getsockname()[0]) # wait to receive server's public key . WILL RETURN NO KEYS IF SERVER HAS BANNED YOU FROM CONNECTING (publicKeyTuple, privateKeyTuple) = exchange_keys_with_server(my_client_socket) # checks whether server accepted or refused connection. RETURNS NONE IF BANNED if((publicKeyTuple, privateKeyTuple) == ((None, None), (None, None))): my_client_socket.close() # close the connection or session as you cannot carry on. else: # begin interaction with other party. print '\n\033[1;42m*******Type your message below and hit enter to send. Type \'EXIT\' to end conversation.*******\033[1;m\n' ReadAndDecryptThread = ThreadManager('read', my_client_socket, publicKeyTuple, privateKeyTuple) # Thread for reading in data and decrypting WriteAndEncryptThread = ThreadManager('write', my_client_socket, publicKeyTuple, privateKeyTuple) # Thread for writing data and encrypting ReadAndDecryptThread.start() WriteAndEncryptThread.start() ReadAndDecryptThread.join() print '[+] Your partner has left the conversation. Press any key to continue...\n' # stop the write thread WriteAndEncryptThread.stop_write_loop() WriteAndEncryptThread.join() # shut down client connection secure_utils.display_processing_cursor('Shutting client down...') except socket.error as socket_error: secure_utils.clear_screen() print socket_error[1] + "\n" raw_input("Press [ENTER] To Return To The Main Menu...\n") return pass