From 2712bd5be6909f39669832e711e6bfa7ddcc281e Mon Sep 17 00:00:00 2001 From: Neocky Date: Sun, 30 May 2021 18:52:35 +0200 Subject: [PATCH] Added `.jar` validation to SFTP/FTP Changes: - added `.jar` file validation to SFTP/FTP - cleanup of old code --- src/handlers/handle_ftp.py | 13 ++++++++ src/handlers/handle_sftp.py | 23 +++++++++++--- src/plugin/plugin_downloader.py | 13 +++++--- src/plugin/plugin_updatechecker.py | 50 ++++++++++++++++++++++++------ 4 files changed, 80 insertions(+), 19 deletions(-) diff --git a/src/handlers/handle_ftp.py b/src/handlers/handle_ftp.py index 176c754..774b07d 100644 --- a/src/handlers/handle_ftp.py +++ b/src/handlers/handle_ftp.py @@ -1,6 +1,8 @@ import os import sys import ftplib +import stat +import re from utils.consoleoutput import oColors from handlers.handle_config import configurationValues @@ -93,3 +95,14 @@ def ftp_downloadFile(ftp, downloadPath, fileToDownload): ftp.retrbinary('RETR '+fileToDownload, filedata.write) filedata.close() ftp.quit() + + +def ftp_validateFileAttributes(ftp, pluginPath): + pluginFTPAttribute = ftp.lstat(pluginPath) + if stat.S_ISDIR(pluginFTPAttribute.st_mode): + return False + if stat.S_ISDIR(pluginFTPAttribute.st_mode): + if re.search(r'.jar$', pluginFTPAttribute.filename): + return True + else: + return False diff --git a/src/handlers/handle_sftp.py b/src/handlers/handle_sftp.py index b2174aa..7b15380 100644 --- a/src/handlers/handle_sftp.py +++ b/src/handlers/handle_sftp.py @@ -2,6 +2,8 @@ import sys import os import pysftp import paramiko +import stat +import re from utils.consoleoutput import oColors from handlers.handle_config import configurationValues @@ -27,14 +29,16 @@ def createSFTPConnection(): def sftp_showPlugins(sftp): - sftp.cd('plugins') + configValues = configurationValues() + sftp.cd(configValues.sftp_folderPath) for attr in sftp.listdir_attr(): print(attr.filename, attr) def sftp_upload_file(sftp, itemPath): + configValues = configurationValues() try: - sftp.chdir('plugins') + sftp.chdir(configValues.sftp_folderPath) sftp.put(itemPath) except FileNotFoundError: @@ -54,8 +58,9 @@ def sftp_upload_server_jar(sftp, itemPath): def sftp_listAll(sftp): + configValues = configurationValues() try: - sftp.chdir('plugins') + sftp.chdir(configValues.sftp_folderPath) installedPlugins = sftp.listdir() except FileNotFoundError: print(oColors.brightRed + "[SFTP]: The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) @@ -86,4 +91,14 @@ def sftp_downloadFile(sftp, downloadPath, fileToDownload): sftp.get(fileToDownload) sftp.close() os.chdir(currentDirectory) - \ No newline at end of file + + +def sftp_validateFileAttributes(sftp, pluginPath): + pluginSFTPAttribute = sftp.lstat(pluginPath) + if stat.S_ISDIR(pluginSFTPAttribute.st_mode): + return False + if stat.S_ISDIR(pluginSFTPAttribute.st_mode): + if re.search(r'.jar$', pluginSFTPAttribute.filename): + return True + else: + return False diff --git a/src/plugin/plugin_downloader.py b/src/plugin/plugin_downloader.py index 0ae4bd9..b41cd0c 100644 --- a/src/plugin/plugin_downloader.py +++ b/src/plugin/plugin_downloader.py @@ -79,12 +79,18 @@ def searchPackage(ressourceName): ressourceId = packageName[ressourceSelected]["id"] if not configValues.localPluginFolder: try: - getSpecificPackage(ressourceId, configValues.sftp_folderPath) + if configValues.sftp_seperateDownloadPath is True: + getSpecificPackage(ressourceId, configValues.sftp_pathToSeperateDownloadPath) + else: + getSpecificPackage(ressourceId, configValues.sftp_folderPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) else: try: - getSpecificPackage(ressourceId, configValues.pathToPluginFolder) + if configValues.seperateDownloadPath is True: + getSpecificPackage(ressourceId, configValues.pathToPluginFolder) + else: + getSpecificPackage(ressourceId, configValues.pathToSeperateDownloadPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) @@ -134,9 +140,6 @@ def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'): versionId = getVersionID(ressourceId, inputPackageVersion) packageVersion = getVersionName(ressourceId, versionId) packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" - #if not configValues.localPluginFolder: - #downloadPackagePath = f"{downloadPath}/{packageDownloadName}" - #else: downloadPackagePath = Path(f"{downloadPath}/{packageDownloadName}") if inputPackageVersion is None or inputPackageVersion == 'latest': downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) diff --git a/src/plugin/plugin_updatechecker.py b/src/plugin/plugin_updatechecker.py index c6215c5..12874bd 100644 --- a/src/plugin/plugin_updatechecker.py +++ b/src/plugin/plugin_updatechecker.py @@ -1,6 +1,8 @@ import os import re import io +import stat +import pysftp from zipfile import ZipFile from urllib.error import HTTPError from pathlib import Path @@ -9,8 +11,8 @@ from rich.progress import track from utils.consoleoutput import oColors from utils.web_request import doAPIRequest from handlers.handle_config import configurationValues -from handlers.handle_sftp import createSFTPConnection, sftp_listAll, sftp_downloadFile -from handlers.handle_ftp import createFTPConnection, ftp_listAll, ftp_downloadFile +from handlers.handle_sftp import createSFTPConnection, sftp_listAll, sftp_downloadFile, sftp_validateFileAttributes +from handlers.handle_ftp import createFTPConnection, ftp_listAll, ftp_downloadFile, ftp_validateFileAttributes from plugin.plugin_downloader import getSpecificPackage from utils.utilities import createTempPluginFolder, deleteTempPluginFolder @@ -133,11 +135,25 @@ def checkInstalledPackage(inputSelectedObject="all"): print("└─────┴────────────────────────────────┴──────────────┴──────────────┴───────────────────┘") try: for plugin in track(pluginList, description="Checking for updates" ,transient=True, complete_style="bright_yellow"): - if not os.path.isfile(Path(f"{pluginFolderPath}/{plugin}")): - continue + if not configValues.localPluginFolder: + if configValues.sftp_seperateDownloadPath is True: + pluginFile = f"{configValues.sftp_pathToSeperateDownloadPath}/{plugin}" + else: + pluginFile = f"{configValues.sftp_folderPath}/{plugin}" - if not re.search(r'.jar$', plugin): - continue + if configValues.sftp_useSftp: + pluginAttributes = sftp_validateFileAttributes(connection, pluginFile) + if pluginAttributes == False: + continue + else: + pluginAttributes = ftp_validateFileAttributes(connection, pluginFile) + if pluginAttributes == False: + continue + else: + if not os.path.isfile(Path(f"{pluginFolderPath}/{plugin}")): + continue + if not re.search(r'.jar$', plugin): + continue try: fileName = getFileName(plugin) @@ -223,11 +239,25 @@ def updateInstalledPackage(inputSelectedObject='all'): print("└─────┴────────────────────────────────┴────────────┴──────────┘") try: for plugin in track(pluginList, description="Updating" ,transient=True, complete_style="bright_magenta"): - if not os.path.isfile(Path(f"{pluginFolderPath}/{plugin}")): - continue + if not configValues.localPluginFolder: + if configValues.sftp_seperateDownloadPath is True: + pluginFile = f"{configValues.sftp_pathToSeperateDownloadPath}/{plugin}" + else: + pluginFile = f"{configValues.sftp_folderPath}/{plugin}" - if not re.search(r'.jar$', plugin): - continue + if configValues.sftp_useSftp: + pluginAttributes = sftp_validateFileAttributes(connection, pluginFile) + if pluginAttributes == False: + continue + else: + pluginAttributes = ftp_validateFileAttributes(connection, pluginFile) + if pluginAttributes == False: + continue + else: + if not os.path.isfile(Path(f"{pluginFolderPath}/{plugin}")): + continue + if not re.search(r'.jar$', plugin): + continue try: fileName = getFileName(plugin)