ClashX.Meta/ClashX/Models/ClashConfig.swift

95 lines
2.2 KiB
Swift
Raw Normal View History

2018-07-30 15:55:10 +08:00
//
// ClashConfig.swift
// ClashX
//
// Created by CYC on 2018/7/30.
2018-08-08 13:47:38 +08:00
// Copyright © 2018 yichengchen. All rights reserved.
2018-07-30 15:55:10 +08:00
//
import Foundation
import CocoaLumberjack
2018-07-30 15:55:10 +08:00
2019-10-20 13:40:50 +08:00
enum ClashProxyMode: String, Codable {
2020-06-05 20:04:40 +08:00
case rule
case global
case direct
}
2019-07-28 12:39:49 +08:00
extension ClashProxyMode {
var name: String {
switch self {
case .rule: return NSLocalizedString("Rule", comment: "")
case .global: return NSLocalizedString("Global", comment: "")
case .direct: return NSLocalizedString("Direct", comment: "")
}
}
}
2019-10-20 13:40:50 +08:00
enum ClashLogLevel: String, Codable {
case info
case warning = "warn"
2019-10-20 13:40:50 +08:00
case error
case debug
case silent
case unknow = "unknown"
func toDDLogLevel() -> DDLogLevel {
switch self {
case .info:
return .info
case .warning:
return .warning
case .error:
return .error
case .debug:
return .debug
case .silent:
return .off
case .unknow:
return .error
}
}
}
2019-10-20 13:40:50 +08:00
class ClashConfig: Codable {
2020-06-16 14:30:32 +08:00
private var port: Int
private var socksPort: Int
2019-10-20 13:40:50 +08:00
var allowLan: Bool
2020-06-16 14:30:32 +08:00
var mixedPort: Int
2019-10-20 13:40:50 +08:00
var mode: ClashProxyMode
var logLevel: ClashLogLevel
2020-06-16 14:30:32 +08:00
var usedHttpPort: Int {
if mixedPort > 0 {
return mixedPort
}
return port
}
var usedSocksPort: Int {
if mixedPort > 0 {
return mixedPort
}
return socksPort
}
2019-10-20 13:40:50 +08:00
private enum CodingKeys: String, CodingKey {
2020-06-16 14:30:32 +08:00
case port, socksPort = "socks-port", mixedPort = "mixed-port", allowLan = "allow-lan", mode, logLevel = "log-level"
2018-07-30 15:55:10 +08:00
}
2019-10-20 13:40:50 +08:00
static func fromData(_ data: Data) -> ClashConfig? {
2018-07-30 15:55:10 +08:00
let decoder = JSONDecoder()
2020-06-05 20:04:40 +08:00
do {
return try decoder.decode(ClashConfig.self, from: data)
} catch let err {
Logger.log((err as NSError).description, level: .error)
return nil
}
2018-07-30 15:55:10 +08:00
}
2019-10-20 13:40:50 +08:00
func copy() -> ClashConfig? {
2019-10-20 13:40:50 +08:00
guard let data = try? JSONEncoder().encode(self) else { return nil }
let copy = try? JSONDecoder().decode(ClashConfig.self, from: data)
return copy
}
2018-07-30 15:55:10 +08:00
}