Files
server/server/server.py
Torma Kristóf b6b3dd3a35
All checks were successful
continuous-integration/drone/push Build is passing
fixd
2021-04-25 18:20:55 +02:00

122 lines
5.4 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)
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)
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) -> None:
if command == "LINOK":
self.networkInstance.sendMessage("LINOK".encode('UTF-8'))
elif command == "LINERROR":
self.networkInstance.sendMessage("LINERROR".encode('UTF-8'))
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.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 != "":
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:
# 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:
self.networkInstance.sendMessage("ERROR".encode('UTF-8'))
def startServer(self):
while True:
message = self.networkInstance.recieveMessage().decode('UTF-8')
self.parseCommand(message)