ClashMetaConfig.

This commit is contained in:
mrFq1 2022-07-27 11:28:21 +08:00
parent 814ebd013c
commit 02a6cbfcf0
2 changed files with 93 additions and 0 deletions

View File

@ -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 = "<group>"; };
0162E74D2864B818007218A6 /* com.metacubex.ClashX.ProxyConfigHelper-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "com.metacubex.ClashX.ProxyConfigHelper-Bridging-Header.h"; sourceTree = "<group>"; };
0162E74E2864B819007218A6 /* MetaTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetaTask.swift; sourceTree = "<group>"; };
018F88F8286DD0CB004DD0F7 /* DualTitleMenuItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DualTitleMenuItem.swift; sourceTree = "<group>"; };
@ -324,6 +326,7 @@
491C250121BD561200AB5D44 /* Managers */,
491C250021BD55B900AB5D44 /* Utils */,
492C4868210EE6B9004554A0 /* ApiRequest.swift */,
015F1E90288E42A50052B20A /* ClashMetaConfig.swift */,
);
path = General;
sourceTree = "<group>";
@ -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 */,

View File

@ -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
}
}