Connecting client - netwrapper
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-04-25 16:08:04 +02:00
parent 284cfff0a0
commit c61994a3cf
2 changed files with 77 additions and 64 deletions

View File

@ -6,13 +6,12 @@ import sys
from Crypto.PublicKey import RSA
from Crypto.PublicKey.RSA import RsaKey
from netwrapper import NetWrapper
ABSOLUTE_PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
DOWNLOAD_LOCATION = ABSOLUTE_PATH + os.path.sep + 'download' + os.path.sep
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:
@ -41,6 +40,10 @@ def loadPrivateKey(passphrase: str) -> RsaKey:
return rsaKey
def loadAddress() -> str:
with open(CONFIG_LOCATION) as json_file:
data = json.load(json_file)
return data['address']
def printCommand():
print('\nInvalid command! Available commands:\n' +
@ -81,63 +84,76 @@ 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()
SERVER_PUBLIC_KEY = loadPublicKey()
CLIENT_PRIVATE_KEY = loadPrivateKey(PASSPHRASE)
CLIENT_ADDRESS = loadAddress()
while True:
command = input("Type a command:").split(" ")
if len(command) > 3 or len(command) < 1:
print("Invalid command format!")
continue
command = input("Type a command:")
separatedCommand = command.split(" ")
if command[0] == 'LIN' and len(command) == 3:
print('TODO: Implement!')
LOGGED_IN = True
continue
try:
if len(separatedCommand) > 3 or len(separatedCommand) < 1:
print("Invalid command format!")
continue
if command[0] == 'EXIT':
sys.exit(1)
if separatedCommand[0] == 'LIN' and len(separatedCommand) == 3:
network = NetWrapper(CLIENT_PRIVATE_KEY, CLIENT_ADDRESS, separatedCommand[1], SERVER_PUBLIC_KEY)
if not network.identifyServer():
print('Server identification failed!')
if not LOGGED_IN:
printCommandsWihtoutLogin()
continue
network.createEncryptedChannel()
network.authenticate(password=separatedCommand[2])
LOGGED_IN = True
continue
if command[0] == 'LOUT':
print('TODO: Implement!')
continue
if separatedCommand[0] == 'EXIT':
sys.exit(1)
if command[0] == 'MKD' and len(command) == 2:
print('TODO: Implement!')
continue
if not LOGGED_IN:
printCommandsWihtoutLogin()
continue
if command[0] == 'RMD' and len(command) == 2:
print('TODO: Implement!')
continue
if separatedCommand[0] == 'LOUT' and len(separatedCommand) == 1:
network.sendMessage(command.encode('UTF-8'))
continue
if command[0] == 'GWD' and len(command) == 1:
print('TODO: Implement!')
continue
if separatedCommand[0] == 'MKD' and len(separatedCommand) == 2:
network.sendMessage(command.encode('UTF-8'))
continue
if command[0] == 'CWD' and len(command) == 2:
print('TODO: Implement!')
continue
if separatedCommand[0] == 'RMD' and len(separatedCommand) == 2:
network.sendMessage(command.encode('UTF-8'))
continue
if command[0] == 'LST' and len(command) == 1:
print('TODO: Implement!')
continue
if separatedCommand[0] == 'GWD' and len(separatedCommand) == 1:
network.sendMessage(command.encode('UTF-8'))
continue
if command[0] == 'UPL' and len(command) == 2:
if os.path.isfile(command[1]):
with open(command[1], "rb") as file:
if separatedCommand[0] == 'CWD' and len(separatedCommand) == 2:
network.sendMessage(command.encode('UTF-8'))
continue
if separatedCommand[0] == 'LST' and len(separatedCommand) == 1:
network.sendMessage(command.encode('UTF-8'))
continue
if separatedCommand[0] == 'UPL' and len(separatedCommand) == 2:
if os.path.isfile(separatedCommand[1]):
with open(separatedCommand[1], "rb") as file:
print('TODO: Implement!')
else:
print('Invalid argument for file upload: ' + separatedCommand[1])
continue
if separatedCommand[0] == 'DNL' and len(separatedCommand) == 2:
dnlFilename = separatedCommand[1].rsplit(os.apth.sep, 1)[-1]
with open(DOWNLOAD_LOCATION + dnlFilename, "wb") as file:
print('TODO: Implement!')
else:
print('Invalid argument for file upload: '+command[1])
continue
continue
if command[0] == 'DNL' and len(command) == 2:
dnlFilename = command[1].rsplit(os.apth.sep, 1)[-1]
with open(DOWNLOAD_LOCATION + dnlFilename, "wb") as file:
print('TODO: Implement!')
continue
printCommand()
printCommand()
except Exception:
print('Error: '+Exception.args)