server/server/server.py
Torma Kristóf 7c0e66c450
All checks were successful
continuous-integration/drone/push Build is passing
add main
2021-04-25 17:36:19 +02:00

111 lines
4.3 KiB
Python

#!/usr/bin/env python3
from authentication import Authetication
from executor import Executor
from netwrapper import NetWrapper
class Server:
def __init__(self, homeDirectory: str = "", sessionTimeout: int = 120, availableServer: bool = True):
self.isAuthenticated = False
self.homeDirectory = homeDirectory
self.sessionTimeout = sessionTimeout
self.availableServer = availableServer
self.executor = Executor(homeDirectory)
self.auth=Authetication()
self.networkInstance = NetWrapper(self.auth.loadUserPublicKeys(),self.auth.loadServerPrivateKey(),self)
def login(self, username: str, password: str) -> bool:
self.isAuthenticated = True
home_directory = self.auth.login(username, password)
self.executor.baseDir=Executor(home_directory)
if not home_directory:
return False
else:
return True
def logout(self) -> None:
self.isAuthenticated = False
self.availableServer = False
self.homeDirectory = ""
self.executor.baseDir=Executor(self.homeDirectory)
def parseCommand(self, command: str) -> str:
if command == "LINOK":
return "LINOK"
elif command == "LINERROR":
return "LINERROR"
parsedCommand = command.split(" ")
if len(parsedCommand) > 3:
return "ERROR"
# 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 = "") -> str:
if self.homeDirectory == "" or self.executor.currentDirectory == "" or self.executor.baseDir == "":
raise Exception("Directories must not be empty string. Did the user log in?")
if command == "MKD":
if secondParam != "":
return "ERROR"
else:
return self.executor.createDirectory(firstParam)
elif command == "RMD":
if secondParam != "":
return "ERROR"
else:
return self.executor.removeDirectory(firstParam)
elif command == "GWD":
if secondParam != "" or firstParam != "":
return "ERROR"
else:
return self.executor.getCurrentDirectory()
elif command == "CWD":
if secondParam != "":
return "ERROR"
else:
return self.executor.setCurrentDirectory(firstParam)
elif command == "LST":
if secondParam != "" or firstParam != "":
return "ERROR"
else:
return self.executor.listCurrentDirectoryContent()
elif command == "RMF":
if secondParam != "":
return "ERROR"
else:
return self.executor.removeFileInCurrentDirectory(firstParam)
elif command == "UPL":
if secondParam != "":
return "ERROR"
else:
# TODO
# Megkapod a filenevet argumentumneknt
# Fogadni kell egy uzenetet, ami a fajl tartalma
# Fogadni kell egy uzenetet, ami "EOF"
# Mindig minden legyen UTF-8-kent kodolva, kiveve a falj, az marard
# Ha sikeres, OK kuldese, kulonben ERROR kuldese
pass
elif command == "DNL":
if secondParam != "":
return "ERROR"
else:
# TODO
# Megkapod a filenevet arguemntumkent
# Kuldeni kell egy uzenetet, ami a falj tartalma
# Kuldeni kell egy uzenetet, ami EOF
# Mindig minden legyen UTF-8-kent kodolva, kiveve a falj, az marard
# Ha sikeres, OK valaszt megkapod, kulonben ERROR valaszt megkapod
pass
else:
return "ERROR"
def startServer(self):
while True:
message = self.networkInstance.recieveMessage().decode('UTF-8')
self.parseCommand(message)