server/server/server.py

111 lines
4.3 KiB
Python
Raw Normal View History

2021-04-15 12:15:16 +02:00
#!/usr/bin/env python3
2021-04-25 17:12:20 +02:00
from authentication import Authetication
2021-04-19 18:19:34 +02:00
from executor import Executor
2021-04-25 17:12:20 +02:00
from netwrapper import NetWrapper
2021-04-19 18:19:34 +02:00
class Server:
2021-04-25 17:09:37 +02:00
def __init__(self, homeDirectory: str = "", sessionTimeout: int = 120, availableServer: bool = True):
2021-04-19 18:19:34 +02:00
self.isAuthenticated = False
self.homeDirectory = homeDirectory
self.sessionTimeout = sessionTimeout
self.availableServer = availableServer
self.executor = Executor(homeDirectory)
2021-04-25 17:20:19 +02:00
self.auth=Authetication()
2021-04-25 17:29:50 +02:00
self.networkInstance = NetWrapper(self.auth.loadUserPublicKeys(),self.auth.loadServerPrivateKey(),self)
2021-04-19 18:19:34 +02:00
2021-04-19 18:46:23 +02:00
def login(self, username: str, password: str) -> bool:
self.isAuthenticated = True
2021-04-25 17:20:19 +02:00
home_directory = self.auth.login(username, password)
2021-04-25 17:09:37 +02:00
self.executor.baseDir=Executor(home_directory)
if not home_directory:
return False
else:
return True
2021-04-19 18:19:34 +02:00
2021-04-19 18:46:23 +02:00
def logout(self) -> None:
self.isAuthenticated = False
self.availableServer = False
self.homeDirectory = ""
2021-04-25 17:09:37 +02:00
self.executor.baseDir=Executor(self.homeDirectory)
2021-04-19 18:19:34 +02:00
2021-04-19 18:46:23 +02:00
def parseCommand(self, command: str) -> str:
2021-04-25 17:35:39 +02:00
if command == "LINOK":
return "LINOK"
elif command == "LINERROR":
return "LINERROR"
2021-04-19 18:46:23 +02:00
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:
2021-04-25 17:15:11 +02:00
if self.homeDirectory == "" or self.executor.currentDirectory == "" or self.executor.baseDir == "":
2021-04-19 18:46:23 +02:00
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
2021-04-25 17:35:39 +02:00
# 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
2021-04-25 17:36:19 +02:00
# Ha sikeres, OK kuldese, kulonben ERROR kuldese
2021-04-19 18:46:23 +02:00
pass
elif command == "DNL":
if secondParam != "":
return "ERROR"
else:
# TODO
2021-04-25 17:35:39 +02:00
# 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
2021-04-25 17:36:19 +02:00
# Ha sikeres, OK valaszt megkapod, kulonben ERROR valaszt megkapod
2021-04-19 18:46:23 +02:00
pass
else:
return "ERROR"
2021-04-25 17:29:50 +02:00
def startServer(self):
2021-04-25 17:35:39 +02:00
while True:
message = self.networkInstance.recieveMessage().decode('UTF-8')
self.parseCommand(message)