finish netwrapper of server and executor improvements
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2021-04-25 00:42:36 +02:00
parent 90396c2826
commit a5b77d0306
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
2 changed files with 25 additions and 13 deletions

View File

@ -29,6 +29,7 @@ class Executor:
def removeDirectory(self, dirName: str) -> str: def removeDirectory(self, dirName: str) -> str:
dirName = self.sanitizeDirectory(dirName) dirName = self.sanitizeDirectory(dirName)
actualDirName = os.path.join(self.currentDirectory, dirName) actualDirName = os.path.join(self.currentDirectory, dirName)
if actualDirName:
os.rmdir(actualDirName) os.rmdir(actualDirName)
return actualDirName return actualDirName
@ -40,16 +41,19 @@ class Executor:
if self.currentDirectory == self.baseDir: if self.currentDirectory == self.baseDir:
return self.currentDirectory return self.currentDirectory
else: else:
directories = self.currentDirectory.split("/") directories = self.currentDirectory.split(os.path.sep)
strdirectory = "" strdirectory = ""
for dir in directories: for dir in directories:
strdirectory += dir + "/" strdirectory += dir + os.path.sep
strdirectory = strdirectory[:-3] strdirectory = strdirectory[:-3]
if os.path.exists(strdirectory):
self.currentDirectory = strdirectory self.currentDirectory = strdirectory
return self.currentDirectory return self.currentDirectory
else: else:
dirName = self.sanitizeDirectory(dirName) dirName = self.sanitizeDirectory(dirName)
self.currentDirectory = os.path.join(self.currentDirectory, dirName) joinedDir = os.path.join(self.currentDirectory, dirName)
if os.path.exists(joinedDir):
self.currentDirectory = joinedDir
return self.currentDirectory return self.currentDirectory
def listCurrentDirectoryContent(self) -> str: def listCurrentDirectoryContent(self) -> str:
@ -71,10 +75,14 @@ class Executor:
def getFileInCurrentDirectory(self, file: str): def getFileInCurrentDirectory(self, file: str):
file = self.sanitizeFile(file) file = self.sanitizeFile(file)
currentfile = os.path.join(self.currentDirectory, file) currentfile = os.path.join(self.currentDirectory, file)
if os.path.exists(currentfile):
return open(currentfile, "r") return open(currentfile, "r")
else:
raise Exception('File not found')
def removeFileInCurrentDirectory(self, file: str) -> str: def removeFileInCurrentDirectory(self, file: str) -> str:
file = self.sanitizeFile(file) file = self.sanitizeFile(file)
currentfile = os.path.join(self.currentDirectory, file) currentfile = os.path.join(self.currentDirectory, file)
if os.path.exists(currentfile):
os.remove(currentfile) os.remove(currentfile)
return currentfile return currentfile

View File

@ -25,6 +25,8 @@ class NetWrapper:
def serverIdentify(self, msg: bytes) -> None: def serverIdentify(self, msg: bytes) -> None:
incommingJson = json.loads(msg.decode('UTF-8')) incommingJson = json.loads(msg.decode('UTF-8'))
if incommingJson['type'] != "IDY":
raise Exception('Wrong message type encountered')
self.clientAddr = incommingJson['source'] self.clientAddr = incommingJson['source']
self.currentUser = incommingJson['username'] self.currentUser = incommingJson['username']
self.currentClientPublicKey = self.clientPublicKey[self.currentUser] self.currentClientPublicKey = self.clientPublicKey[self.currentUser]
@ -78,7 +80,8 @@ class NetWrapper:
retciphertext = b64decode(b64['message']) retciphertext = b64decode(b64['message'])
retcipher = ChaCha20.new(self.cipherkey, nonce=retnonce) retcipher = ChaCha20.new(self.cipherkey, nonce=retnonce)
plaintext = retcipher.decrypt(retciphertext).decode('UTF-8').split(' ') plaintext = retcipher.decrypt(retciphertext).decode('UTF-8').split(' ')
linsuccess = (not (len(plaintext) != 3 or plaintext[0] != "LIN" or plaintext[1] != self.currentUser)) and self.serverInstance.login(plaintext[1],plaintext[2]) linsuccess = (not (len(plaintext) != 3 or plaintext[0] != "LIN" or plaintext[
1] != self.currentUser)) and self.serverInstance.login(plaintext[1], plaintext[2])
if linsuccess: if linsuccess:
message = "OK".encode('UTF-8') message = "OK".encode('UTF-8')
else: else:
@ -87,7 +90,8 @@ class NetWrapper:
ciphertext = cipher.encrypt(message) ciphertext = cipher.encrypt(message)
nonce = b64encode(cipher.nonce).decode('UTF-8') nonce = b64encode(cipher.nonce).decode('UTF-8')
ct = b64encode(ciphertext).decode('UTF-8') ct = b64encode(ciphertext).decode('UTF-8')
sendjson = json.dumps({'type': 'AUT', 'source': self.network.own_addr, 'nonce': nonce, 'message': ct}).encode( sendjson = json.dumps(
{'type': 'AUT', 'source': self.network.own_addr, 'nonce': nonce, 'message': ct}).encode(
'UTF-8') 'UTF-8')
self.network.send_msg(self.clientAddr, sendjson) self.network.send_msg(self.clientAddr, sendjson)
return linsuccess return linsuccess