mirror of
https://github.com/KeksPirates/SoftwareManager.git
synced 2026-08-03 17:39:42 +02:00
feat: enhance update handling by verifying installer hash and refactoring asset retrieval
This commit is contained in:
+35
-17
@@ -121,14 +121,28 @@ SVG_PLAY = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><polygon
|
|||||||
SVG_PAUSE = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect x="5" y="3" width="4" height="18" rx="1" fill="{color}"/><rect x="15" y="3" width="4" height="18" rx="1" fill="{color}"/></svg>'
|
SVG_PAUSE = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect x="5" y="3" width="4" height="18" rx="1" fill="{color}"/><rect x="15" y="3" width="4" height="18" rx="1" fill="{color}"/></svg>'
|
||||||
SVG_FOLDER = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M2 6c0-1.1.9-2 2-2h5l2 2h7c1.1 0 2 .9 2 2v10c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6z" fill="{color}"/></svg>'
|
SVG_FOLDER = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M2 6c0-1.1.9-2 2-2h5l2 2h7c1.1 0 2 .9 2 2v10c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6z" fill="{color}"/></svg>'
|
||||||
|
|
||||||
|
def _verify_hash(file_path, expected_hash):
|
||||||
|
import hashlib
|
||||||
|
sha256 = hashlib.sha256()
|
||||||
|
|
||||||
def _download_update(latest_version):
|
with open(file_path, "rb") as f:
|
||||||
|
for chunk in iter(lambda: f.read(8192), b""):
|
||||||
|
sha256.update(chunk)
|
||||||
|
|
||||||
|
return f"sha256:{sha256.hexdigest()}" == expected_hash
|
||||||
|
|
||||||
|
def _download_update(assets):
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
|
||||||
new_filename = f"SoftwareManager-dev-{latest_version.replace('-dev', '')}-windows-setup.exe"
|
|
||||||
url = f"https://github.com/KeksPirates/SoftwareManager/releases/latest/download/SoftwareManager-dev-{latest_version.replace('-dev', '')}-windows-setup.exe"
|
for asset in assets:
|
||||||
installer_path = os.path.join(tempfile.gettempdir(), new_filename)
|
if "-windows-setup.exe" in asset["name"]:
|
||||||
|
filename = asset["name"]
|
||||||
|
setup_hash = asset["hash"]
|
||||||
|
url = asset ["url"]
|
||||||
|
|
||||||
|
installer_path = os.path.join(tempfile.gettempdir(), filename)
|
||||||
|
|
||||||
progress = QtWidgets.QProgressDialog("Downloading installer...", None, 0, 0)
|
progress = QtWidgets.QProgressDialog("Downloading installer...", None, 0, 0)
|
||||||
progress.setWindowTitle("Updating")
|
progress.setWindowTitle("Updating")
|
||||||
@@ -157,6 +171,12 @@ def _download_update(latest_version):
|
|||||||
progress.close()
|
progress.close()
|
||||||
raise FileNotFoundError("Executable not found")
|
raise FileNotFoundError("Executable not found")
|
||||||
|
|
||||||
|
if _verify_hash(installer_path, setup_hash):
|
||||||
|
consoleLog(f"Sucessfully validated installer hash ({setup_hash})")
|
||||||
|
else:
|
||||||
|
consoleLog("Error: Invalid Filehash, file may be corrupted")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
progress.setLabelText("Installing update...")
|
progress.setLabelText("Installing update...")
|
||||||
progress.setValue(100)
|
progress.setValue(100)
|
||||||
QtWidgets.QApplication.processEvents()
|
QtWidgets.QApplication.processEvents()
|
||||||
@@ -200,21 +220,19 @@ class MainWindow(QtWidgets.QMainWindow, QWidget):
|
|||||||
|
|
||||||
# Check for updates on Windows
|
# Check for updates on Windows
|
||||||
if state.ignore_updates is False and platform.system() == "Windows":
|
if state.ignore_updates is False and platform.system() == "Windows":
|
||||||
result = get_updates()
|
assets = get_updates()
|
||||||
if result != (None, None):
|
if assets != None:
|
||||||
assets, latest_version = result
|
|
||||||
|
|
||||||
if assets:
|
msg = QMessageBox()
|
||||||
msg = QMessageBox()
|
msg.setIcon(QMessageBox.Icon.Information)
|
||||||
msg.setIcon(QMessageBox.Icon.Information)
|
msg.setWindowTitle("Update Available")
|
||||||
msg.setWindowTitle("Update Available")
|
msg.setText("A new version is available.")
|
||||||
msg.setText("A new version is available.")
|
msg.setInformativeText("Press Ok to download the update.")
|
||||||
msg.setInformativeText("Press Ok to download the update.")
|
msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Ignore)
|
||||||
msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Ignore)
|
|
||||||
|
|
||||||
response = msg.exec_()
|
response = msg.exec_()
|
||||||
if response == QMessageBox.StandardButton.Ok:
|
if response == QMessageBox.StandardButton.Ok:
|
||||||
_download_update(latest_version)
|
_download_update(assets)
|
||||||
|
|
||||||
self.setWindowTitle("Software Manager")
|
self.setWindowTitle("Software Manager")
|
||||||
self.setGeometry(100, 100, 800, 600)
|
self.setGeometry(100, 100, 800, 600)
|
||||||
|
|||||||
@@ -15,13 +15,20 @@ def get_updates():
|
|||||||
latest_version = release["name"]
|
latest_version = release["name"]
|
||||||
assets = release["assets"]
|
assets = release["assets"]
|
||||||
|
|
||||||
|
release_assets = []
|
||||||
|
|
||||||
if latest_version != state.version:
|
if latest_version != state.version:
|
||||||
consoleLog(f"New release available: {latest_version}")
|
consoleLog(f"New release available: {latest_version}")
|
||||||
if assets:
|
if assets:
|
||||||
consoleLog("Assets:")
|
consoleLog("Assets:")
|
||||||
for asset in assets:
|
for asset in assets:
|
||||||
consoleLog(f" - {asset['name']}: {asset['browser_download_url']} - {asset['digest']}")
|
consoleLog(f"{asset['name']}")
|
||||||
return assets, latest_version
|
release_assets.append(dict(
|
||||||
|
name=asset['name'],
|
||||||
|
url=asset['browser_download_url'],
|
||||||
|
hash=asset['digest']
|
||||||
|
))
|
||||||
|
return release_assets
|
||||||
else:
|
else:
|
||||||
return None, None
|
return None, None
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user