ClashX.Meta/ClashX/Models/ClashConfig.swift

58 lines
1.4 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
enum ClashProxyMode: String,Codable {
case rule = "Rule"
case global = "Global"
case direct = "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: "")
}
}
}
enum ClashLogLevel:String,Codable {
case info = "info"
case warning = "warning"
case error = "error"
case debug = "debug"
case silent = "silent"
case unknow = "unknow"
}
2018-07-30 15:55:10 +08:00
class ClashConfig:Codable {
var port:Int
var socketPort:Int
var allowLan:Bool
var mode:ClashProxyMode
var logLevel:ClashLogLevel
2018-07-30 15:55:10 +08:00
private enum CodingKeys : String, CodingKey {
case port, socketPort = "socks-port", allowLan = "allow-lan", mode, logLevel = "log-level"
2018-07-30 15:55:10 +08:00
}
static func fromData(_ data:Data)->ClashConfig?{
2018-07-30 15:55:10 +08:00
let decoder = JSONDecoder()
let model = try? decoder.decode(ClashConfig.self, from: data)
return model
2018-07-30 15:55:10 +08:00
}
func copy() -> ClashConfig? {
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
}