Validate core file with md5.

This commit is contained in:
mrFq1 2022-07-31 10:12:38 +08:00
parent bee5d20338
commit 2aa3e5ca1e
2 changed files with 47 additions and 6 deletions

View File

@ -17,6 +17,8 @@ import PromiseKit
private let statusItemLengthWithSpeed: CGFloat = 72 private let statusItemLengthWithSpeed: CGFloat = 72
private let MetaCoreMd5 = "WOSHIZIDONGSHENGCHENGDEA"
@NSApplicationMain @NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate { class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem: NSStatusItem! var statusItem: NSStatusItem!
@ -458,17 +460,29 @@ class AppDelegate: NSObject, NSApplicationDelegate {
} }
if let path = Paths.defaultCorePath(), if let path = Paths.defaultCorePath(),
testMetaCore(path) != nil { testMetaCore(path) != nil,
validateDefaultCore() {
return path return path
} else { } else {
assertionFailure("Meta Core file losted.") return "ERROR"
return ""
} }
}() }()
RemoteConfigManager.shared.verifyConfigTask.setLaunchPath(corePath) if corePath == "ERROR" {
PrivilegedHelperManager.shared.helper()?.initMetaCore(withPath: corePath) let alert = NSAlert()
Logger.log("initClashCore finish") 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?)? { func testMetaCore(_ path: String) -> (version: String, date: Date?)? {
@ -516,6 +530,29 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return (version: version, date: date) 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 { func chmodX(_ path: String) -> Bool {
let proc = Process() let proc = Process()
proc.executableURL = .init(fileURLWithPath: "/bin/chmod") proc.executableURL = .init(fileURLWithPath: "/bin/chmod")

View File

@ -16,6 +16,10 @@ chmod +x com.metacubex.ClashX.ProxyConfigHelper.meta
cp com.metacubex.ClashX.ProxyConfigHelper.meta ../ClashX/Resources/ cp com.metacubex.ClashX.ProxyConfigHelper.meta ../ClashX/Resources/
cd .. 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" echo "Pod install"
pod install pod install
echo "delete old files" echo "delete old files"