1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 03:50:08 +02:00

🏗 Download and validate checksum

This commit is contained in:
2023-02-02 00:47:53 +01:00
parent 8df126a7b0
commit 57c90c216f

View File

@ -9,26 +9,61 @@
import Cocoa import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate { class AppDelegate: NSObject, NSApplicationDelegate {
var updaterDirectory: String = ""
func applicationDidFinishLaunching(_ aNotification: Notification) { func applicationDidFinishLaunching(_ aNotification: Notification) {
print("PHP MONITOR SELF-UPDATER by Nico Verbruggen") print("PHP MONITOR SELF-UPDATER by Nico Verbruggen")
// Figure out where the updater would like to find the self.updaterDirectory = "~/.config/phpmon/updater"
let path = "~/.config/phpmon/updater/phpmon.zip"
.replacingOccurrences(of: "~", with: NSHomeDirectory()) .replacingOccurrences(of: "~", with: NSHomeDirectory())
print("Updater directory set to: \(self.updaterDirectory)")
// Download the latest file
let zipPath = self.download(
// zipUrl: "https://github.com/nicoverbruggen/phpmon/releases/download/v5.7.2/phpmon.zip",
// sha256: "654dd1df64ae32b1e3b9ebed7f6d89d04ed374b0b4d6732704e6df190169214f"
zipUrl: "https://github.com/nicoverbruggen/phpmon/releases/download/v5.7.2/phpmon-dev.zip",
sha256: "1cb147bd1b1fbd52971d90dff577465b644aee7c878f15ede57f46e8f217067a"
)
// Terminating all instances of PHP Monitor first // Terminating all instances of PHP Monitor first
terminatePhpMon() terminatePhpMon()
// Checking if the updated file exists // We made it!
print("Checking path: \(path)") install(zipPath: zipPath)
// If the file does not exist, exit gracefully // Restart PHP Monitor, this will also close the updater
if !FileManager.default.fileExists(atPath: path) { restartPhpMon(dev: zipPath.contains("dev"))
}
func applicationWillTerminate(_ aNotification: Notification) {
exit(1)
}
func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return false
}
private func download(zipUrl: String, sha256: String) -> String {
// Remove all zips
system_quiet("rm -rf \(updaterDirectory)/*.zip")
// Download the file (and follow redirects + no output on failure)
system_quiet("cd \(updaterDirectory) && curl \(zipUrl) -fLO")
// Identify the downloaded file
let filename = system("cd \(updaterDirectory) && ls | grep .zip")
.trimmingCharacters(in: .whitespacesAndNewlines)
if filename.isEmpty {
print("The update has not been downloaded. Sadly, that means that PHP Monitor cannot not updated!") print("The update has not been downloaded. Sadly, that means that PHP Monitor cannot not updated!")
showAlert(title: "The archive containing the zip appears to be missing.", showAlert(title: "The update was not downloaded.",
description: "PHP Monitor will not be updated, but we will restart the app for you.") description: "PHP Monitor will not be updated, but we will restart the app for you. You may not be connected to the internet or the server may be encountering issues. Please try again later!")
if FileManager.default.fileExists(atPath: "/Applications/PHP Monitor.app") { if FileManager.default.fileExists(atPath: "/Applications/PHP Monitor.app") {
restartPhpMon(dev: false) restartPhpMon(dev: false)
@ -41,29 +76,52 @@ class AppDelegate: NSObject, NSApplicationDelegate {
} }
} }
// We made it! // Calculate the checksum for the downloaded file
install(zipPath: path) let checksum = system("openssl dgst -sha256 \(updaterDirectory)/\(filename) | awk '{print $NF}'")
.trimmingCharacters(in: .whitespacesAndNewlines)
// Restart PHP Monitor, this will also close the updater print("""
restartPhpMon(dev: false) Comparing checksums...
} Expected SHA256: \(sha256)
Actual SHA256: \(checksum)
""")
func applicationWillTerminate(_ aNotification: Notification) { // Make sure the checksum matches before we do anything with the file
exit(1) if checksum != sha256 {
} print("The checksums failed to match. Cancelling!")
showAlert(
title: "The downloaded update failed checksum validation",
description: "Please try again! If this issue persists, there may be an issue with the server and I do not recommend upgrading."
)
exit(0)
}
func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool { return "\(updaterDirectory)/\(filename)"
return true
} }
private func install(zipPath: String) { private func install(zipPath: String) {
_ = system("rm -rf ~/.config/phpmon/updater/output") system_quiet("rm -rf \(updaterDirectory)/output")
_ = system("mkdir -p ~/.config/phpmon/updater/output") system_quiet("mkdir -p \(updaterDirectory)/output")
_ = system("unzip \(zipPath) -d ~/.config/phpmon/updater/output")
_ = system("rm -rf \"/Applications/PHP Monitor.app\"") var isDirectory: ObjCBool = true
let command = "mv \"~/.config/phpmon/updater/output/PHP Monitor.app\" \"/Applications/PHP Monitor.app\"" if !FileManager.default.fileExists(atPath: "\(updaterDirectory)/output", isDirectory: &isDirectory) {
.replacingOccurrences(of: "~", with: NSHomeDirectory()) showAlert(
_ = system(command) title: "The updater directory is missing",
description: "The automatic updater will quit. Make sure that ` ~/.config/phpmon/updater` is writeable."
)
exit(0)
}
system_quiet("unzip \(zipPath) -d \(updaterDirectory)/output")
let expectedAppName = zipPath.contains("dev")
? "PHP Monitor DEV.app"
: "PHP Monitor.app"
print("Removing \(expectedAppName) before replacing...")
system_quiet("rm -rf \"/Applications/\(expectedAppName)\"")
system_quiet("mv \"\(updaterDirectory)/output/\(expectedAppName)\" \"/Applications/\(expectedAppName)\"")
} }
private func terminatePhpMon() { private func terminatePhpMon() {