#!/usr/bin/env python3 import os from authentication import Authetication from executor import Executor from netwrapper import NetWrapper class Server: def __init__(self, sessionTimeout: int = 120, availableServer: bool = True): self.isAuthenticated = False self.sessionTimeout = sessionTimeout self.availableServer = availableServer self.executor = Executor("") self.auth = Authetication() def initServer(self): print("Please enter your private key passphrase") passphrase = input() self.networkInstance = NetWrapper(self.auth.loadUserPublicKeys(), self.auth.loadServerPrivateKey(passphrase), self.auth) def login(self, homeDir: str) -> None: self.isAuthenticated = True self.executor = Executor(homeDir + os.path.sep) def logout(self) -> None: self.networkInstance.logout() self.isAuthenticated = False self.availableServer = False self.executor = Executor("") def parseCommand(self, command: str) -> None: if command == "LINOK": self.login(self.networkInstance.homeDirectory) self.networkInstance.sendMessage("OK".encode('UTF-8')) return None elif command == "LINERROR": return None elif command == "ERROR": self.networkInstance.sendMessage("ERROR".encode('UTF-8')) return None parsedCommand = command.split(" ") if len(parsedCommand) > 3: self.networkInstance.sendMessage("ERROR".encode('UTF-8')) elif len(parsedCommand) == 1: self.execute(parsedCommand[0]) elif len(parsedCommand) == 2: self.execute(parsedCommand[0], parsedCommand[1]) elif len(parsedCommand) == 3: self.execute(parsedCommand[0], parsedCommand[1], parsedCommand[2]) def execute(self, command: str, firstParam: str = "", secondParam: str = "") -> None: if self.executor.baseDir == "": raise Exception("Home directory must not be empty string. Did the user log in?") if command == "LOUT": if not (secondParam == "" and firstParam == ""): self.networkInstance.sendMessage("ERROR".encode('UTF-8')) else: self.networkInstance.sendMessage("OK".encode('UTF-8')) self.logout() elif command == "MKD": if secondParam != "": self.networkInstance.sendMessage("ERROR".encode('UTF-8')) else: self.executor.createDirectory(firstParam) self.networkInstance.sendMessage("OK".encode('UTF-8')) elif command == "RMD": if secondParam != "": self.networkInstance.sendMessage("ERROR".encode('UTF-8')) else: self.executor.removeDirectory(firstParam) self.networkInstance.sendMessage("OK".encode('UTF-8')) elif command == "GWD": if not (secondParam == "" and firstParam == ""): self.networkInstance.sendMessage("ERROR".encode('UTF-8')) else: self.networkInstance.sendMessage(self.executor.getCurrentDirectory().encode('UTF-8')) elif command == "CWD": if secondParam != "": self.networkInstance.sendMessage("ERROR".encode('UTF-8')) else: self.executor.setCurrentDirectory(firstParam) self.networkInstance.sendMessage("OK".encode('UTF-8')) elif command == "LST": if not (secondParam == "" and firstParam == ""): self.networkInstance.sendMessage("ERROR".encode('UTF-8')) else: self.networkInstance.sendMessage(self.executor.listCurrentDirectoryContent().encode('UTF-8')) elif command == "RMF": if secondParam != "": self.networkInstance.sendMessage("ERROR".encode('UTF-8')) else: self.executor.removeFileInCurrentDirectory(firstParam) self.networkInstance.sendMessage("OK".encode('UTF-8')) elif command == "UPL": if secondParam != "": self.networkInstance.sendMessage("ERROR".encode('UTF-8')) else: fileMessage = self.networkInstance.recieveMessage() eof = self.networkInstance.recieveMessage().decode('UTF-8') if eof == "EOF": self.executor.putFileInCurrentDirectory(firstParam, fileMessage) self.networkInstance.sendMessage("OK".encode('UTF-8')) else: self.networkInstance.sendMessage("ERROR".encode('UTF-8')) elif command == "DNL": if secondParam != "": self.networkInstance.sendMessage("ERROR".encode('UTF-8')) else: try: file = self.executor.getFileInCurrentDirectory(firstParam) self.networkInstance.sendMessage(file) self.networkInstance.sendMessage("EOF".encode('UTF-8')) except Exception: self.networkInstance.sendMessage("ERROR".encode('UTF-8')) else: self.networkInstance.sendMessage("ERROR".encode('UTF-8')) def startServer(self): while True: try: message = self.networkInstance.recieveMessage().decode('UTF-8') self.parseCommand(message) except Exception as e: self.networkInstance.sendMessage("ERROR".encode('UTF-8')) print(e)