From 02a6cbfcf009f3ffa663e78709abf2c32a94e01c Mon Sep 17 00:00:00 2001 From: mrFq1 <1xxbx0il0@mozmail.com> Date: Wed, 27 Jul 2022 11:28:21 +0800 Subject: [PATCH] ClashMetaConfig. --- ClashX.xcodeproj/project.pbxproj | 4 ++ ClashX/General/ClashMetaConfig.swift | 89 ++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 ClashX/General/ClashMetaConfig.swift diff --git a/ClashX.xcodeproj/project.pbxproj b/ClashX.xcodeproj/project.pbxproj index 0d276cc..b56da9b 100644 --- a/ClashX.xcodeproj/project.pbxproj +++ b/ClashX.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 015F1E91288E42A50052B20A /* ClashMetaConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 015F1E90288E42A50052B20A /* ClashMetaConfig.swift */; }; 015F1E92288E60D30052B20A /* MetaTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0162E74E2864B819007218A6 /* MetaTask.swift */; }; 0162E74F2864B819007218A6 /* MetaTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0162E74E2864B819007218A6 /* MetaTask.swift */; }; 018F88F9286DD0CB004DD0F7 /* DualTitleMenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 018F88F8286DD0CB004DD0F7 /* DualTitleMenuItem.swift */; }; @@ -131,6 +132,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 015F1E90288E42A50052B20A /* ClashMetaConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashMetaConfig.swift; sourceTree = ""; }; 0162E74D2864B818007218A6 /* com.metacubex.ClashX.ProxyConfigHelper-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "com.metacubex.ClashX.ProxyConfigHelper-Bridging-Header.h"; sourceTree = ""; }; 0162E74E2864B819007218A6 /* MetaTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetaTask.swift; sourceTree = ""; }; 018F88F8286DD0CB004DD0F7 /* DualTitleMenuItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DualTitleMenuItem.swift; sourceTree = ""; }; @@ -324,6 +326,7 @@ 491C250121BD561200AB5D44 /* Managers */, 491C250021BD55B900AB5D44 /* Utils */, 492C4868210EE6B9004554A0 /* ApiRequest.swift */, + 015F1E90288E42A50052B20A /* ClashMetaConfig.swift */, ); path = General; sourceTree = ""; @@ -706,6 +709,7 @@ 49722FEF211F338B00650A41 /* FileEvent.swift in Sources */, 49D176A72355FE680093DD7B /* NetworkChangeNotifier.swift in Sources */, 4913C82321157D0200F6B87C /* Notification.swift in Sources */, + 015F1E91288E42A50052B20A /* ClashMetaConfig.swift in Sources */, 8ACD21BD27A04ED500BC4632 /* ProxyModeChangeCommand.swift in Sources */, 49228457270AADE20027A4B6 /* RemoteConfigUpdateIntervalSettingView.swift in Sources */, F9203A26236342820020D57D /* AppDelegate+..swift in Sources */, diff --git a/ClashX/General/ClashMetaConfig.swift b/ClashX/General/ClashMetaConfig.swift new file mode 100644 index 0000000..a9caeb4 --- /dev/null +++ b/ClashX/General/ClashMetaConfig.swift @@ -0,0 +1,89 @@ +// +// ClashMetaConfig.swift +// ClashX Meta + +import Foundation +import Cocoa +import Yams + +class ClashMetaConfig: NSObject { + + struct Config: Codable { + var externalUI: String? = { + guard let htmlPath = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "dashboard") else { + return nil + } + return URL(fileURLWithPath: htmlPath).deletingLastPathComponent().path + }() + + var externalController = "127.0.0.1:9096" + var secret: String? + + var port: Int? + var socksPort: Int? + var mixedPort: Int? + + var logLevel = ConfigManager.selectLoggingApiLevel.rawValue + + var path: String { + get { + guard let s = try? YAMLEncoder().encode(self), + let path = RemoteConfigManager.createCacheConfig(string: s) else { + assertionFailure("Create init config file failed.") + return "" + } + return path + } + } + + enum CodingKeys: String, CodingKey { + case externalController = "external-controller", + externalUI = "external-ui", + mixedPort = "mixed-port", + port, + socksPort = "socks-port", + logLevel = "log-level", + secret + } + + mutating func loadDefaultConfigFile() { + let fm = FileManager.default + guard let data = fm.contents(atPath: kDefaultConfigFilePath), + let string = String(data: data, encoding: .utf8), + let yaml = try? Yams.load(yaml: string) as? [String: Any] else { + return + } + + let keys = Config.CodingKeys.self + if let ec = yaml[keys.externalController.rawValue] as? String { + externalController = ec + } + + if let s = yaml[keys.secret.rawValue] as? String { + secret = s + } + + if let port = yaml[keys.mixedPort.rawValue] as? Int { + mixedPort = port + } else { + if let p = yaml[keys.port.rawValue] as? Int { + port = p + } + if let sp = yaml[keys.socksPort.rawValue] as? Int { + socksPort = sp + } + } + + if port == nil && mixedPort == nil { + mixedPort = 7890 + } + } + } + + static func generateInitConfig() -> Config { + var config = Config() + config.loadDefaultConfigFile() + return config + } + +}