From 2aa3e5ca1eda091303b2e11739489505891ad804 Mon Sep 17 00:00:00 2001 From: mrFq1 <1xxbx0il0@mozmail.com> Date: Sun, 31 Jul 2022 10:12:38 +0800 Subject: [PATCH] Validate core file with md5. --- ClashX/AppDelegate.swift | 49 +++++++++++++++++++++++++++++++++++----- install_dependency.sh | 4 ++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/ClashX/AppDelegate.swift b/ClashX/AppDelegate.swift index d76b136..d3e7e9a 100644 --- a/ClashX/AppDelegate.swift +++ b/ClashX/AppDelegate.swift @@ -17,6 +17,8 @@ import PromiseKit private let statusItemLengthWithSpeed: CGFloat = 72 +private let MetaCoreMd5 = "WOSHIZIDONGSHENGCHENGDEA" + @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { var statusItem: NSStatusItem! @@ -458,17 +460,29 @@ class AppDelegate: NSObject, NSApplicationDelegate { } if let path = Paths.defaultCorePath(), - testMetaCore(path) != nil { + testMetaCore(path) != nil, + validateDefaultCore() { return path } else { - assertionFailure("Meta Core file losted.") - return "" + return "ERROR" } }() - RemoteConfigManager.shared.verifyConfigTask.setLaunchPath(corePath) - PrivilegedHelperManager.shared.helper()?.initMetaCore(withPath: corePath) - Logger.log("initClashCore finish") + if corePath == "ERROR" { + let alert = NSAlert() + alert.messageText = "Failure to verify the internal Meta Core.\nDo NOT replace core file in the resources folder." + alert.alertStyle = .warning + alert.addButton(withTitle: NSLocalizedString("Quit", comment: "")) + alert.runModal() + + DispatchQueue.main.async { + NSApplication.shared.terminate(nil) + } + } else { + RemoteConfigManager.shared.verifyConfigTask.setLaunchPath(corePath) + PrivilegedHelperManager.shared.helper()?.initMetaCore(withPath: corePath) + Logger.log("initClashCore finish") + } } func testMetaCore(_ path: String) -> (version: String, date: Date?)? { @@ -516,6 +530,29 @@ class AppDelegate: NSObject, NSApplicationDelegate { return (version: version, date: date) } + func validateDefaultCore() -> Bool { + guard let path = Paths.defaultCorePath() else { return false } + #if DEBUG + return true + #endif + let proc = Process() + proc.executableURL = .init(fileURLWithPath: "/sbin/md5") + proc.arguments = ["-q", path] + let pipe = Pipe() + proc.standardOutput = pipe + + try? proc.run() + proc.waitUntilExit() + let data = pipe.fileHandleForReading.readDataToEndOfFile() + guard proc.terminationStatus == 0, + let out = String(data: data, encoding: .utf8) else { + return false + } + + let md5 = out.replacingOccurrences(of: "\n", with: "") + return md5 == MetaCoreMd5 + } + func chmodX(_ path: String) -> Bool { let proc = Process() proc.executableURL = .init(fileURLWithPath: "/bin/chmod") diff --git a/install_dependency.sh b/install_dependency.sh index e8522d6..db5cbab 100755 --- a/install_dependency.sh +++ b/install_dependency.sh @@ -16,6 +16,10 @@ chmod +x com.metacubex.ClashX.ProxyConfigHelper.meta cp com.metacubex.ClashX.ProxyConfigHelper.meta ../ClashX/Resources/ cd .. +echo "Update meta core md5 to code" +sed -i '' "s/WOSHIZIDONGSHENGCHENGDEA/$(md5 -q ClashX/Resources/com.metacubex.ClashX.ProxyConfigHelper.meta)/g" ClashX/AppDelegate.swift + + echo "Pod install" pod install echo "delete old files"