mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-08 10:44:50 +00:00
Remove lossy default-action writes that can replace china-proxy with gfwlist. Preserve legacy preference reads and derive the default action from the selected preset. Update tests to use explicit presets and verify identity, legacy compatibility, and no-op reselection.
47 lines
1.6 KiB
Swift
47 lines
1.6 KiB
Swift
import Foundation
|
|
|
|
/// 当前用户的代理控制偏好;采用 UserDefaults 的即时内存更新与异步写盘语义。
|
|
struct ProxyControlPreferences {
|
|
let defaults: UserDefaults
|
|
|
|
init(defaults: UserDefaults = .standard) { self.defaults = defaults }
|
|
|
|
var preferredMode: ProxyModeKind {
|
|
get {
|
|
defaults.string(forKey: "proxyControl.preferredMode").flatMap(ProxyModeKind.init(rawValue:))
|
|
?? .rule
|
|
}
|
|
nonmutating set { defaults.set(newValue.rawValue, forKey: "proxyControl.preferredMode") }
|
|
}
|
|
|
|
var rulePreset: RulePreset {
|
|
get {
|
|
if let saved = defaults.string(forKey: "proxyControl.rulePreset").flatMap(
|
|
RulePreset.init(rawValue:))
|
|
{
|
|
return saved
|
|
}
|
|
let legacy =
|
|
defaults.string(forKey: "proxyControl.ruleDefaultAction")
|
|
.flatMap(RuleDefaultAction.init(rawValue:)) ?? .directWhenUnmatched
|
|
return RulePreset(legacy: legacy)
|
|
}
|
|
nonmutating set { defaults.set(newValue.rawValue, forKey: "proxyControl.rulePreset") }
|
|
}
|
|
|
|
var agentEnabled: Bool {
|
|
get { defaults.object(forKey: "proxyControl.agentEnabled") as? Bool ?? false }
|
|
nonmutating set { defaults.set(newValue, forKey: "proxyControl.agentEnabled") }
|
|
}
|
|
|
|
var systemProxyEnabled: Bool {
|
|
get { defaults.object(forKey: "proxyControl.systemProxyEnabled") as? Bool ?? false }
|
|
nonmutating set { defaults.set(newValue, forKey: "proxyControl.systemProxyEnabled") }
|
|
}
|
|
|
|
var proxyExceptions: String {
|
|
get { defaults.object(forKey: "proxyControl.proxyExceptions") as? String ?? "" }
|
|
nonmutating set { defaults.set(newValue, forKey: "proxyControl.proxyExceptions") }
|
|
}
|
|
}
|