ClashX.Meta/ClashX/General/Managers/SystemProxyManager.swift

112 lines
3.6 KiB
Swift
Raw Normal View History

//
// SystemProxyManager.swift
// ClashX
//
// Created by yichengchen on 2019/8/17.
// Copyright © 2019 west2online. All rights reserved.
//
import AppKit
2019-10-20 13:40:50 +08:00
import ServiceManagement
class SystemProxyManager: NSObject {
static let shared = SystemProxyManager()
2019-10-20 13:40:50 +08:00
private var savedProxyInfo: [String: Any] {
get {
return UserDefaults.standard.dictionary(forKey: "kSavedProxyInfo") ?? [:]
}
set {
UserDefaults.standard.set(newValue, forKey: "kSavedProxyInfo")
}
}
2019-10-20 13:40:50 +08:00
private var disableRestoreProxy: Bool {
get {
return UserDefaults.standard.bool(forKey: "kDisableRestoreProxy")
}
set {
UserDefaults.standard.set(newValue, forKey: "kDisableRestoreProxy")
}
}
2019-10-20 13:40:50 +08:00
2020-04-21 23:57:06 +08:00
private var helper: ProxyConfigRemoteProcessProtocol? {
PrivilegedHelperManager.shared.helper()
}
2019-10-20 13:40:50 +08:00
func saveProxy() {
2019-10-20 13:40:50 +08:00
guard !disableRestoreProxy else { return }
Logger.log("saveProxy", level: .debug)
2020-04-21 23:57:06 +08:00
helper?.getCurrentProxySetting({ [weak self] info in
Logger.log("saveProxy done", level: .debug)
2019-10-20 13:40:50 +08:00
if let info = info as? [String: Any] {
self?.savedProxyInfo = info
}
})
}
2019-10-20 13:40:50 +08:00
func enableProxy() {
let port = ConfigManager.shared.currentConfig?.port ?? 0
let socketPort = ConfigManager.shared.currentConfig?.socketPort ?? 0
SystemProxyManager.shared.enableProxy(port: port, socksPort: socketPort)
}
2019-10-20 13:40:50 +08:00
func enableProxy(port: Int, socksPort: Int) {
2020-02-22 11:25:35 +08:00
guard port > 0 && socksPort > 0 else {
Logger.log("enableProxy fail: \(port) \(socksPort)", level: .error)
return
}
Logger.log("enableProxy", level: .debug)
helper?.enableProxy(withPort: Int32(port), socksPort: Int32(socksPort), error: { error in
2019-10-20 13:40:50 +08:00
if let error = error {
Logger.log("enableProxy \(error)", level: .error)
}
})
}
2019-10-20 13:40:50 +08:00
func disableProxy() {
let port = ConfigManager.shared.currentConfig?.port ?? 0
let socketPort = ConfigManager.shared.currentConfig?.socketPort ?? 0
SystemProxyManager.shared.disableProxy(port: port, socksPort: socketPort)
}
func disableProxy(port: Int, socksPort: Int, forceDisable: Bool = false, complete: (() -> Void)? = nil) {
Logger.log("disableProxy", level: .debug)
2019-10-20 13:40:50 +08:00
if disableRestoreProxy || forceDisable {
helper?.disableProxy { error in
2019-10-20 13:40:50 +08:00
if let error = error {
Logger.log("disableProxy \(error)", level: .error)
}
complete?()
}
return
}
2019-10-20 13:40:50 +08:00
helper?.restoreProxy(withCurrentPort: Int32(port), socksPort: Int32(socksPort), info: savedProxyInfo, error: { error in
2019-10-20 13:40:50 +08:00
if let error = error {
Logger.log("restoreProxy \(error)", level: .error)
}
complete?()
2019-10-20 13:40:50 +08:00
})
}
2019-10-20 13:40:50 +08:00
// MARK: - Expriment Menu Items
func addDisableRestoreProxyMenuItem(_ menu: inout NSMenu) {
let item = NSMenuItem(title: NSLocalizedString("Disable Restore Proxy Setting", comment: ""), action: #selector(optionMenuItemTap(sender:)), keyEquivalent: "")
item.target = self
menu.addItem(item)
updateMenuItemStatus(item)
}
func updateMenuItemStatus(_ item: NSMenuItem) {
item.state = disableRestoreProxy ? .on : .off
}
@objc func optionMenuItemTap(sender: NSMenuItem) {
disableRestoreProxy = !disableRestoreProxy
updateMenuItemStatus(sender)
}
}