Added SFTP Support & Requirements check

This commit is contained in:
Neocky 2021-03-10 02:05:56 +01:00
parent db404a7a61
commit 6915b213dc
7 changed files with 223 additions and 41 deletions

View File

@ -1,2 +1,10 @@
urllib3 >= 1.21.1 urllib3 >= 1.21.1
requests >=2.25.1 requests >=2.25.1
paramiko >= 2.7.2
bcrypt >= 3.2.0
cryptography >= 3.4.6
pynacl >= 1.4.0
cffi >= 1.14.5
six >= 1.15.0
pycparser >= 2.20
pysftp >= 0.2.9

View File

@ -18,7 +18,21 @@ def checkConfig():
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.sections() config.sections()
config.read("config.ini") config.read("config.ini")
localPluginFolder = config['General']['LocalPluginFolder']
pathToPluginFolder = config['General']['PathToPluginFolder'] pathToPluginFolder = config['General']['PathToPluginFolder']
sftp_server = config['Remote Server']['Server']
sftp_user = config['Remote Server']['Username']
sftp_password = config['Remote Server']['Password']
sftp_port = config['Remote Server']['Port']
sftp_folderPath = config['Remote Server']['PluginFolder']
sftp_port = int(sftp_port)
if localPluginFolder == 'True':
localPluginFolder = True
else:
localPluginFolder = False
return configValues return configValues
@ -32,6 +46,8 @@ def createConfig():
config['Remote Server']['Server'] = '0.0.0.0' config['Remote Server']['Server'] = '0.0.0.0'
config['Remote Server']['Username'] = 'user' config['Remote Server']['Username'] = 'user'
config['Remote Server']['Password'] = 'longpassword' config['Remote Server']['Password'] = 'longpassword'
config['Remote Server']['Port'] = '22'
config['Remote Server']['PluginFolder'] = '.\\plugins'
with open('./config.ini', 'w') as configfile: with open('./config.ini', 'w') as configfile:
config.write(configfile) config.write(configfile)

View File

@ -4,7 +4,8 @@ from consoleoutput import consoleTitle, clearConsole, printMainMenu, oColors
from plugin_downloader import searchPackage, getSpecificPackage from plugin_downloader import searchPackage, getSpecificPackage
from plugin_updatechecker import updateInstalledPackage, checkInstalledPackage from plugin_updatechecker import updateInstalledPackage, checkInstalledPackage
from handle_config import checkConfig from handle_config import checkConfig
from utilities import getHelp from utilities import getHelp, check_requirements
from handle_sftp import createSFTPConnection, sftp_showPlugins
def createInputLists(): def createInputLists():
global COMMANDLIST global COMMANDLIST
@ -26,22 +27,29 @@ def handleInput(inputCommand, inputSelectedObject, inputParams):
while True: while True:
if inputCommand == 'get': if inputCommand == 'get':
if inputSelectedObject.isdigit(): if inputSelectedObject.isdigit():
getSpecificPackage(inputSelectedObject, checkConfig().pathToPluginFolder, inputParams) if not checkConfig().localPluginFolder:
break getSpecificPackage(inputSelectedObject, checkConfig().sftp_folderPath, inputParams)
break
else:
getSpecificPackage(inputSelectedObject, checkConfig().pathToPluginFolder, inputParams)
else: else:
searchPackage(inputSelectedObject) searchPackage(inputSelectedObject)
break break
if inputCommand == 'update': if inputCommand == 'update':
updateInstalledPackage(checkConfig().pathToPluginFolder, inputSelectedObject) updateInstalledPackage(inputSelectedObject)
break break
if inputCommand == 'check': if inputCommand == 'check':
checkInstalledPackage(checkConfig().pathToPluginFolder, inputSelectedObject) checkInstalledPackage(inputSelectedObject)
break break
if inputCommand == 'exit': if inputCommand == 'exit':
sys.exit() sys.exit()
if inputCommand == 'help': if inputCommand == 'help':
getHelp() getHelp()
break break
if inputCommand == 'sftp':
sftp = createSFTPConnection()
sftp_showPlugins(sftp)
break
else: else:
print(oColors.brightRed + "Command not found. Please try again." + oColors.standardWhite) print(oColors.brightRed + "Command not found. Please try again." + oColors.standardWhite)
getInput() getInput()
@ -64,11 +72,14 @@ def getInput():
def inputMainMenu(): def inputMainMenu():
consoleTitle()
clearConsole() clearConsole()
checkConfig() checkConfig()
check_requirements()
createInputLists() createInputLists()
printMainMenu() printMainMenu()
getInput() getInput()
outputTest()
def outputTest(): def outputTest():
@ -87,6 +98,4 @@ def outputTest():
input("Press key to end program...") input("Press key to end program...")
consoleTitle()
inputMainMenu() inputMainMenu()
outputTest()

41
src/handle_sftp.py Normal file
View File

@ -0,0 +1,41 @@
import sys
import pysftp
from handle_config import checkConfig
from consoleoutput import oColors
def createSFTPConnection():
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None # TODO fix this
sftp = pysftp.Connection(checkConfig().sftp_server, username=checkConfig().sftp_user, password=checkConfig().sftp_password, port=checkConfig().sftp_port, cnopts=cnopts)
return sftp
def sftp_showPlugins(sftp):
sftp.cd('plugins')
for attr in sftp.listdir_attr():
print(attr.filename, attr)
def sftp_cdPluginDir(sftp):
sftp.cd('plugins')
def sftp_upload_file(sftp, itemPath):
#sftp = createSFTPConnection()
try:
sftp.chdir('plugins')
sftp.put(itemPath)
except FileNotFoundError:
print(oColors.brightRed + "The *plugins* folder couldn*t be found on the remote host!" + oColors.standardWhite)
print(oColors.brightRed + "Aborting installation." + oColors.standardWhite)
sftp.close()
def sftp_listAll(sftp):
try:
sftp.chdir('plugins')
installedPlugins = sftp.listdir()
except FileNotFoundError:
print(oColors.brightRed + "The *plugins* folder couldn*t be found on the remote host!" + oColors.standardWhite)
return installedPlugins

View File

@ -4,6 +4,8 @@ import re
from web_request import doAPIRequest from web_request import doAPIRequest
from consoleoutput import oColors from consoleoutput import oColors
from handle_config import checkConfig from handle_config import checkConfig
from utilities import createTempPluginFolder, deleteTempPluginFolder
from handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection
def calculateFileSize(downloadFileSize): def calculateFileSize(downloadFileSize):
@ -78,7 +80,10 @@ def searchPackage(ressourceName):
#fileInfo = packageName[ressourceSelected]["file"] #fileInfo = packageName[ressourceSelected]["file"]
#packageUrl = fileInfo["url"] #packageUrl = fileInfo["url"]
ressourceId = packageName[ressourceSelected]["id"] ressourceId = packageName[ressourceSelected]["id"]
getSpecificPackage(ressourceId, checkConfig().pathToPluginFolder) if not checkConfig().localPluginFolder:
getSpecificPackage(ressourceId, checkConfig().sftp_folderPath)
else:
getSpecificPackage(ressourceId, checkConfig().pathToPluginFolder)
def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'): def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'):
@ -96,9 +101,16 @@ def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'):
filesizeData = calculateFileSize(filesize) filesizeData = calculateFileSize(filesize)
print(f"Downloadsize: {filesizeData} KB") print(f"Downloadsize: {filesizeData} KB")
print(f"File downloaded here: {downloadPath}") print(f"File downloaded here: {downloadPath}")
if not checkConfig().localPluginFolder:
print(downloadPath)
sftpSession = createSFTPConnection()
#sftp_cdPluginDir(sftpSession)
sftp_upload_file(sftpSession, downloadPath)
def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'): def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'):
if checkConfig().localPluginFolder == False:
downloadPath = createTempPluginFolder()
url = f"https://api.spiget.org/v2/resources/{ressourceId}" url = f"https://api.spiget.org/v2/resources/{ressourceId}"
packageDetails = doAPIRequest(url) packageDetails = doAPIRequest(url)
packageName = packageDetails["name"] packageName = packageDetails["name"]
@ -108,17 +120,31 @@ def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'):
#packageVersion = getlatestVersion(ressourceId) #packageVersion = getlatestVersion(ressourceId)
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"
downloadPackagePath = f"{downloadPath}\\{packageDownloadName}" downloadPackagePath = f"{downloadPath}\\{packageDownloadName}"
if checkConfig().localPluginFolder:
if inputPackageVersion is None or inputPackageVersion == 'latest':
try:
downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
else:
try:
downloadSpecificVersion(ressourceId, downloadPackagePath, versionId)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
if inputPackageVersion is None or inputPackageVersion == 'latest': if not checkConfig().localPluginFolder:
try: if inputPackageVersion is None or inputPackageVersion == 'latest':
downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) try:
except HTTPError as err: downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath)
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) deleteTempPluginFolder(downloadPath)
else: except HTTPError as err:
try: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
downloadSpecificVersion(ressourceId, downloadPackagePath, versionId) else:
except HTTPError as err: try:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) downloadSpecificVersion(ressourceId, downloadPackagePath, versionId)
deleteTempPluginFolder(downloadPath)
except HTTPError as err:
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
# get latest update > https://api.spiget.org/v2/resources/28140/updates/latest # get latest update > https://api.spiget.org/v2/resources/28140/updates/latest
# this also > https://api.spiget.org/v2/resources/28140/versions/latest # this also > https://api.spiget.org/v2/resources/28140/versions/latest

View File

@ -5,6 +5,7 @@ from consoleoutput import oColors
from plugin_downloader import getSpecificPackage #handleInput from plugin_downloader import getSpecificPackage #handleInput
from web_request import doAPIRequest from web_request import doAPIRequest
from handle_config import checkConfig from handle_config import checkConfig
from handle_sftp import createSFTPConnection, sftp_listAll
def createPluginList(): def createPluginList():
@ -19,10 +20,13 @@ def addToPluginList(pluginId, versionId, plugin_is_outdated):
def getFileName(pluginName): def getFileName(pluginName):
pluginNameFull = pluginName pluginNameFull = pluginName
pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull) pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull) #errors out when for example .yaml file in plugins
pluginVersionFull = pluginVersion.group() try:
pluginNameOnly = pluginNameFull.replace(pluginVersionFull, '') pluginVersionFull = pluginVersion.group() # TODO fix this
pluginNameOnly = re.sub(r'(\-$)', '', pluginNameOnly) except AttributeError:
pluginVersionFull = pluginVersion
pluginNameOnlyy = pluginNameFull.replace(pluginVersionFull, '')
pluginNameOnly = re.sub(r'(\-$)', '', pluginNameOnlyy)
return pluginNameOnly return pluginNameOnly
@ -46,9 +50,13 @@ def compareVersions(pluginId, pluginVersion):
return plugin_is_outdated return plugin_is_outdated
def checkInstalledPackage(pluginFolderPath, inputSelectedObject="all"): def checkInstalledPackage(inputSelectedObject="all"):
createPluginList() createPluginList()
pluginList = os.listdir(pluginFolderPath) if not checkConfig().localPluginFolder:
sftp = createSFTPConnection()
pluginList = sftp_listAll(sftp)
else:
pluginList = os.listdir(checkConfig().pathToPluginFolder)
i = 0 i = 0
oldPackages = 0 oldPackages = 0
print("Index / Name / Installed Version / Update available") print("Index / Name / Installed Version / Update available")
@ -84,10 +92,13 @@ def checkInstalledPackage(pluginFolderPath, inputSelectedObject="all"):
print(f"Old packages: [{oldPackages}/{i}]") print(f"Old packages: [{oldPackages}/{i}]")
def updateInstalledPackage(pluginFolderPath, inputSelectedObject='all'): def updateInstalledPackage(inputSelectedObject='all'):
createPluginList() createPluginList()
pluginList = os.listdir(pluginFolderPath) if not checkConfig().localPluginFolder:
print(pluginList) sftp = createSFTPConnection()
pluginList = sftp_listAll(sftp)
else:
pluginList = os.listdir(checkConfig().pathToPluginFolder)
i = 0 i = 0
for plugin in pluginList: for plugin in pluginList:
print(plugin) print(plugin)
@ -102,29 +113,43 @@ def updateInstalledPackage(pluginFolderPath, inputSelectedObject='all'):
if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE):
print(f"Updating: {fileName}") print(f"Updating: {fileName}")
pluginPath = checkConfig().pathToPluginFolder if not checkConfig().localPluginFolder:
pluginPath = f"{pluginPath}\\{plugin}" pluginPath = checkConfig().sftp_folderPath
os.remove(pluginPath) pluginPath = f"{pluginPath}\\{plugin}"
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder) sftp = createSFTPConnection()
sftp.remove(pluginPath)
getSpecificPackage(pluginId, checkConfig().sftp_folderPath)
else:
pluginPath = checkConfig().pathToPluginFolder
pluginPath = f"{pluginPath}\\{plugin}"
os.remove(pluginPath)
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
break break
if inputSelectedObject == 'all': if inputSelectedObject == 'all':
if INSTALLEDPLUGINLIST[i][2] == True: if INSTALLEDPLUGINLIST[i][2] == True:
print("Deleting old plugin...") if not checkConfig().localPluginFolder:
pluginPath = checkConfig().pathToPluginFolder pluginPath = checkConfig().sftp_folderPath
pluginPath = f"{pluginPath}\\{plugin}" pluginPath = f"{pluginPath}\\{plugin}"
os.remove(pluginPath) print("Deleting old plugin...")
print("Downloading new plugin...") sftp = createSFTPConnection()
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder) sftp.remove(pluginPath)
print("Downloading new plugin...")
getSpecificPackage(pluginId, checkConfig().sftp_folderPath)
else:
pluginPath = checkConfig().pathToPluginFolder
pluginPath = f"{pluginPath}\\{plugin}"
print("Deleting old plugin...")
os.remove(pluginPath)
print("Downloading new plugin...")
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
i = i + 1 i = i + 1
#print(INSTALLEDPLUGINLIST[1][0])
#getLatestPackageVersion(pluginID, r"C:\\Users\USER\Desktop\\plugins\\")
def getInstalledPlugin(localFileName, localFileVersion): def getInstalledPlugin(localFileName, localFileVersion):
url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name" url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name"
packageName = doAPIRequest(url) packageName = doAPIRequest(url)
#packageName = response.json()
i = 1 i = 1
plugin_match_found = False plugin_match_found = False
pluginID = None pluginID = None

View File

@ -1,5 +1,11 @@
# misc functions # misc functions
import os
import sys
import requests
import shutil
from consoleoutput import oColors from consoleoutput import oColors
from handle_config import checkConfig
def getHelp(): def getHelp():
print(oColors.brightYellow+ "Need help?" + oColors.standardWhite) print(oColors.brightYellow+ "Need help?" + oColors.standardWhite)
@ -7,3 +13,54 @@ def getHelp():
print("https://github.com/Neocky/pluGET") print("https://github.com/Neocky/pluGET")
print("Or go to the official discord.") print("Or go to the official discord.")
print("The link for discord can also be found on Github!") print("The link for discord can also be found on Github!")
def check_local_plugin_folder():
if checkConfig().localPluginFolder:
if not os.path.isdir(checkConfig().pathToPluginFolder):
print(oColors.brightRed + "Plugin folder coulnd*t be found. Creating one..." + oColors.standardWhite)
try:
os.mkdir(checkConfig().pathToPluginFolder)
except OSError:
print(oColors.brightRed + "Creation of directory %s failed" % checkConfig().pathToPluginFolder)
print(oColors.brightRed + "Please check the config file!" + oColors.standardWhite)
sys.exit()
else:
print("Created directory %s" % checkConfig().pathToPluginFolder)
def apiTest():
apiStatusUrl = 'https://api.spiget.org/v2/status'
try:
r = requests.get(apiStatusUrl)
except requests.exceptions.HTTPError:
print(oColors.brightRed + "Couldn*t make a connection to the API. Check you connection to the internet!" + oColors.standardWhite)
sys.exit()
if r.status_code != 200:
print(oColors.brightRed + "Problems with the API detected. Plese try it again later!" + oColors.standardWhite)
sys.exit()
def check_requirements():
apiTest()
check_local_plugin_folder()
# sftp test
def createTempPluginFolder():
tempPluginFolder = ".\\plugins"
if not os.path.isdir(tempPluginFolder):
#print(oColors.brightRed + "Plugin folder coulnd*t be found. Creating one..." + oColors.standardWhite)
try:
os.mkdir(tempPluginFolder)
except OSError:
print(oColors.brightRed + "Creation of directory %s failed" % checkConfig().pathToPluginFolder)
print(oColors.brightRed + "Please check the config file!" + oColors.standardWhite)
sys.exit()
return tempPluginFolder
def deleteTempPluginFolder(tempPluginFolder):
try:
shutil.rmtree(tempPluginFolder)
except OSError as e:
print ("Error: %s - %s." % (e.filename, e.strerror))