From 9d43cc3f24175524c798b7ac91fb194a5babb49d Mon Sep 17 00:00:00 2001 From: "DESKTOP-DPA61F8\\Benedek" Date: Wed, 21 Apr 2021 00:45:42 +0200 Subject: [PATCH] DONE: Client command reciever ERROR: On RSA decoding TODO: Implement functions --- client/client.py | 116 ++++++++++++++++++++++++++++++++++++++++++ client/client_test.py | 2 + 2 files changed, 118 insertions(+) create mode 100644 client/client.py create mode 100644 client/client_test.py diff --git a/client/client.py b/client/client.py new file mode 100644 index 0000000..8c38a7a --- /dev/null +++ b/client/client.py @@ -0,0 +1,116 @@ +import os, sys, getopt, json +from Crypto.PublicKey import RSA +from Crypto.PublicKey.RSA import RsaKey + +ABSOLUTE_PATH = os.path.abspath(os.path.dirname(sys.argv[0])) +CONFIG_LOCATION = ABSOLUTE_PATH + os.path.sep + 'config' + os.path.sep + 'config.txt' +PASSPHRASE = '' +LOGGED_IN = False +SERVER_PUBLIC_KEY = None +CLIENT_PRIVATE_KEY = None + + +def loadPublicKey() -> RsaKey: + with open(CONFIG_LOCATION) as json_file: + data = json.load(json_file) + return RSA.import_key(data['publicServerKey']) + + +def loadPrivateKey(passphrase: str) -> RsaKey: + with open(CONFIG_LOCATION) as json_file: + data = json.load(json_file) + return RSA.import_key(data['privateClientKey'], passphrase) + + +def printCommand(): + print('\nInvalid command! Available commands:\n' + + ' Create directory -> MKD \n' + + ' Remove directory -> RMV \n' + + ' Get current directory -> GWD \n' + + ' Change current directory -> CWD \n' + + ' List content of current directory -> LST \n' + + ' Upload file to current directory -> UPL \n' + + ' Download file from current directory -> DNL \n' + + ' Login -> LIN \n' + + ' Logout -> LOUT \n') + +def printCommandsWihtoutLogin(): + print('\nYou must log in before issuing other commads!\n', + ' Login -> LIN \n', + ' Exit -> EXIT\n') + + +try: + opts, args = getopt.getopt(sys.argv[1:], 'hp:') +except getopt.GetoptError: + print('Error: Unknown option detected.') + sys.exit(1) + +for opt, arg in opts: + if opt in '-p': + PASSPHRASE = arg + +if PASSPHRASE == '': + print('Key required to start client!') + print('Usage:') + print(' client.py -p ') + sys.exit(1) + +if not os.path.isfile(CONFIG_LOCATION) or os.stat(CONFIG_LOCATION).st_size == 0: + print('Invalid client config file') + sys.exit(1) + +# SERVER_PUBLIC_KEY = loadPublicKey() +# CLIENT_PRIVATE_KEY = loadPrivateKey(PASSPHRASE) + + +while True: + command = input("Type a command:").split(" ") + if len(command) > 3 or len(command) < 1: + print("Invalid command format!") + + if command[0] == 'LIN': + print('TODO: Implement!') + LOGGED_IN = True + continue + + if command[0] == 'EXIT': + sys.exit(1) + + if not LOGGED_IN: + printCommandsWihtoutLogin() + continue + + if command[0] == 'LOUT': + print('TODO: Implement!') + continue + + if command[0] == 'MKD': + print('TODO: Implement!') + continue + + if command[0] == 'RMD': + print('TODO: Implement!') + continue + + if command[0] == 'GWD': + print('TODO: Implement!') + continue + + if command[0] == 'CWD': + print('TODO: Implement!') + continue + + if command[0] == 'LST': + print('TODO: Implement!') + continue + + if command[0] == 'UPL': + print('TODO: Implement!') + continue + + if command[0] == 'DNL': + print('TODO: Implement!') + continue + + printCommand() diff --git a/client/client_test.py b/client/client_test.py new file mode 100644 index 0000000..139597f --- /dev/null +++ b/client/client_test.py @@ -0,0 +1,2 @@ + +