Files
server/server/server.py
Torma Kristóf ae743cde15
All checks were successful
continuous-integration/drone/push Build is passing
some more login rework
2021-04-26 13:18:50 +02:00

126 lines
5.6 KiB
Python

#!/usr/bin/env python3
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("")
def initServer(self):
print("Please enter your private key passphrase")
passphrase = input()
self.auth = Authetication()
self.networkInstance = NetWrapper(self.auth.loadUserPublicKeys(), self.auth.loadServerPrivateKey(passphrase),
self.auth)
def login(self, homeDir: str) -> None:
self.isAuthenticated = True
self.executor.baseDir = Executor(homeDir)
def logout(self) -> None:
self.networkInstance.logout()
self.isAuthenticated = False
self.availableServer = False
self.homeDirectory = ""
self.executor.baseDir = Executor(self.homeDirectory)
def parseCommand(self, command: str) -> None:
if command == "LINOK":
self.login(self.networkInstance.homeDirectory)
self.networkInstance.sendMessage("LINOK".encode('UTF-8'))
elif command == "LINERROR":
self.networkInstance.sendMessage("LINERROR".encode('UTF-8'))
elif command == "ERROR":
return None
parsedCommand = command.split(" ")
if len(parsedCommand) > 3:
self.networkInstance.sendMessage("ERROR".encode('UTF-8'))
# TODO: Ez igy gecironda, ezt meg kene csinalni rendesen.
elif len(parsedCommand) == 1:
return self.execute(parsedCommand[0])
elif len(parsedCommand) == 2:
return self.execute(parsedCommand[0], parsedCommand[1])
elif len(parsedCommand) == 3:
return self.execute(parsedCommand[0], parsedCommand[1], parsedCommand[2])
def execute(self, command: str, firstParam: str = "", secondParam: str = "") -> None:
if self.executor.currentDirectory == "" or self.executor.baseDir == "":
raise Exception("Directories must not be empty string. Did the user log in?")
if command == "LOUT":
if secondParam != "" or firstParam != "":
self.networkInstance.sendMessage("ERROR".encode('UTF-8'))
else:
self.logout()
self.networkInstance.sendMessage("OK".encode('UTF-8'))
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 secondParam != "" or firstParam != "":
self.networkInstance.sendMessage("ERROR".encode('UTF-8'))
else:
self.executor.getCurrentDirectory()
self.networkInstance.sendMessage("OK".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 secondParam != "" or firstParam != "":
self.networkInstance.sendMessage("ERROR".encode('UTF-8'))
else:
self.executor.listCurrentDirectoryContent()
self.networkInstance.sendMessage("OK".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'))
#TODO varj OK vagy ERROR uzenetre
except:
self.networkInstance.sendMessage("ERROR".encode('UTF-8'))
else:
self.networkInstance.sendMessage("ERROR".encode('UTF-8'))
def startServer(self):
while True:
message = self.networkInstance.recieveMessage().decode('UTF-8')
self.parseCommand(message)