All checks were successful
continuous-integration/drone/push Build is passing
142 lines
4.1 KiB
Python
142 lines
4.1 KiB
Python
import os, sys, getopt, json
|
|
|
|
from Crypto.PublicKey import RSA
|
|
from Crypto.PublicKey.RSA import RsaKey
|
|
|
|
import ftplib
|
|
|
|
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:
|
|
with open(CONFIG_LOCATION) as json_file:
|
|
data = json.load(json_file)
|
|
key = data['publicServerKey']
|
|
key = bytes.fromhex(key)
|
|
try:
|
|
rsaKey = RSA.import_key(key)
|
|
except ValueError:
|
|
print('Invalid server public key!')
|
|
sys.exit(1)
|
|
return RSA.import_key(key)
|
|
|
|
|
|
def loadPrivateKey(passphrase: str) -> RsaKey:
|
|
with open(CONFIG_LOCATION) as json_file:
|
|
data = json.load(json_file)
|
|
key = data['privateClientKey']
|
|
key = bytes.fromhex(key)
|
|
try:
|
|
rsaKey = RSA.import_key(key, passphrase)
|
|
except ValueError:
|
|
print('Invalid client key!')
|
|
sys.exit(1)
|
|
|
|
return rsaKey
|
|
|
|
|
|
def printCommand():
|
|
print('\nInvalid command! Available commands:\n' +
|
|
' Create directory -> MKD <directory name> \n' +
|
|
' Remove directory -> RMV <directory name> \n' +
|
|
' Get current directory -> GWD \n' +
|
|
' Change current directory -> CWD <path> \n' +
|
|
' List content of current directory -> LST \n' +
|
|
' Upload file to current directory -> UPL <filename> \n' +
|
|
' Download file from current directory -> DNL <filename> \n' +
|
|
' Login -> LIN <username> <password> \n' +
|
|
' Logout -> LOUT \n')
|
|
|
|
|
|
def printCommandsWihtoutLogin():
|
|
print('\nYou must log in before issuing other commads!\n',
|
|
' Login -> LIN <username> <password> \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 <passphrase>')
|
|
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!")
|
|
continue
|
|
|
|
if command[0] == 'LIN' and len(command) == 3:
|
|
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' and len(command) == 2:
|
|
print('TODO: Implement!')
|
|
continue
|
|
|
|
if command[0] == 'RMD' and len(command) == 2:
|
|
print('TODO: Implement!')
|
|
continue
|
|
|
|
if command[0] == 'GWD' and len(command) == 1:
|
|
print('TODO: Implement!')
|
|
continue
|
|
|
|
if command[0] == 'CWD' and len(command) == 2:
|
|
print('TODO: Implement!')
|
|
continue
|
|
|
|
if command[0] == 'LST' and len(command) == 1:
|
|
print('TODO: Implement!')
|
|
continue
|
|
|
|
if command[0] == 'UPL' and len(command) == 2:
|
|
if os.path.isfile(command[1]):
|
|
with open(command[1], "rb") as file:
|
|
print('TODO: Implement!')
|
|
else:
|
|
print('Invalid argument for file upload: '+command[1])
|
|
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() |