2018-06-13 10:44:30 +08:00
|
|
|
|
|
|
|
import Foundation
|
2018-08-19 21:07:50 +08:00
|
|
|
import AppKit
|
|
|
|
|
2018-06-13 10:44:30 +08:00
|
|
|
class ProxyConfigManager {
|
2018-06-14 12:56:07 +08:00
|
|
|
static let kProxyConfigFolder = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash")
|
2018-12-09 00:06:43 +08:00
|
|
|
static let kVersion = "0.1.2"
|
2018-06-13 10:44:30 +08:00
|
|
|
|
|
|
|
|
2018-08-11 13:23:06 +08:00
|
|
|
static func vaildHelper() -> Bool {
|
2018-06-13 10:44:30 +08:00
|
|
|
let scriptPath = "\(Bundle.main.resourcePath!)/check_proxy_helper.sh"
|
2018-08-19 20:52:21 +08:00
|
|
|
let appleScriptStr = "do shell script \"bash \\\"\(scriptPath)\\\" \(kProxyConfigFolder) \(kVersion) \" "
|
2018-06-13 10:44:30 +08:00
|
|
|
let appleScript = NSAppleScript(source: appleScriptStr)
|
|
|
|
var dict: NSDictionary?
|
|
|
|
if let res = appleScript?.executeAndReturnError(&dict) {
|
2018-06-14 12:56:07 +08:00
|
|
|
if (res.stringValue?.contains("success")) ?? false {
|
2018-06-13 10:44:30 +08:00
|
|
|
return true
|
|
|
|
}
|
2018-08-19 20:52:21 +08:00
|
|
|
} else {
|
|
|
|
Logger.log(msg: "\(String(describing: dict))",level: .error)
|
2018-06-13 10:44:30 +08:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-11 13:23:06 +08:00
|
|
|
static func install() -> Bool {
|
2018-06-14 12:56:07 +08:00
|
|
|
checkConfigDir()
|
2018-06-13 10:44:30 +08:00
|
|
|
checkMMDB()
|
2018-06-14 12:56:07 +08:00
|
|
|
|
|
|
|
let proxyHelperPath = Bundle.main.path(forResource: "ProxyConfig", ofType: nil)
|
|
|
|
let targetPath = "\(kProxyConfigFolder)/ProxyConfig"
|
|
|
|
|
2018-08-10 20:58:20 +08:00
|
|
|
|
2018-06-14 12:56:07 +08:00
|
|
|
if !vaildHelper() {
|
2018-08-19 21:07:50 +08:00
|
|
|
if (!showInstallHelperAlert()) {
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
|
2018-08-10 20:58:20 +08:00
|
|
|
if (FileManager.default.fileExists(atPath: targetPath)) {
|
|
|
|
try? FileManager.default.removeItem(atPath: targetPath)
|
|
|
|
}
|
|
|
|
try? FileManager.default.copyItem(at: URL(fileURLWithPath: proxyHelperPath!), to: URL(fileURLWithPath: targetPath))
|
|
|
|
|
2018-06-13 10:44:30 +08:00
|
|
|
let scriptPath = "\(Bundle.main.resourcePath!)/install_proxy_helper.sh"
|
2018-06-14 12:56:07 +08:00
|
|
|
let appleScriptStr = "do shell script \"bash \(scriptPath) \(kProxyConfigFolder) \" with administrator privileges"
|
2018-06-13 10:44:30 +08:00
|
|
|
let appleScript = NSAppleScript(source: appleScriptStr)
|
2018-08-19 21:07:50 +08:00
|
|
|
|
2018-06-13 10:44:30 +08:00
|
|
|
var dict: NSDictionary?
|
2018-06-14 12:56:07 +08:00
|
|
|
if let _ = appleScript?.executeAndReturnError(&dict) {
|
2018-06-13 10:44:30 +08:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:56:07 +08:00
|
|
|
static func checkConfigDir() {
|
|
|
|
var isDir : ObjCBool = true
|
|
|
|
if !FileManager.default.fileExists(atPath: kProxyConfigFolder, isDirectory:&isDir) {
|
|
|
|
try? FileManager.default.createDirectory(atPath: kProxyConfigFolder, withIntermediateDirectories: false, attributes: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 10:44:30 +08:00
|
|
|
static func checkMMDB() {
|
|
|
|
let fileManage = FileManager.default
|
2018-06-14 12:56:07 +08:00
|
|
|
let destMMDBPath = "\(kProxyConfigFolder)/Country.mmdb"
|
2018-06-13 10:44:30 +08:00
|
|
|
if !fileManage.fileExists(atPath: destMMDBPath) {
|
2018-10-27 21:00:40 +08:00
|
|
|
if let mmdbPath = Bundle.main.path(forResource: "Country", ofType: "mmdb") {
|
|
|
|
try? fileManage.copyItem(at: URL(fileURLWithPath: mmdbPath), to: URL(fileURLWithPath: destMMDBPath))
|
|
|
|
}
|
2018-06-13 10:44:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 13:23:06 +08:00
|
|
|
static func setUpSystemProxy(port: Int?,socksPort: Int?) -> Bool {
|
2018-06-13 10:44:30 +08:00
|
|
|
let task = Process()
|
2018-06-14 12:56:07 +08:00
|
|
|
task.launchPath = "\(kProxyConfigFolder)/ProxyConfig"
|
2018-06-13 10:44:30 +08:00
|
|
|
if let port = port,let socksPort = socksPort {
|
|
|
|
task.arguments = [String(port),String(socksPort), "enable"]
|
|
|
|
} else {
|
|
|
|
task.arguments = ["0", "0", "disable"]
|
|
|
|
}
|
|
|
|
|
|
|
|
task.launch()
|
|
|
|
|
|
|
|
task.waitUntilExit()
|
|
|
|
|
|
|
|
if task.terminationStatus != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2018-08-19 21:07:50 +08:00
|
|
|
|
|
|
|
static func showInstallHelperAlert() -> Bool{
|
|
|
|
let alert = NSAlert()
|
|
|
|
alert.messageText = """
|
|
|
|
ClashX needs to install a small tool to ~/.config/clash with administrator privileges to set system proxy quickly.
|
|
|
|
|
|
|
|
Otherwise you need to type in the administrator password every time you change system proxy through ClashX.
|
2018-12-09 21:45:38 +08:00
|
|
|
""".localized()
|
2018-08-19 21:07:50 +08:00
|
|
|
alert.alertStyle = .warning
|
2018-12-09 21:45:38 +08:00
|
|
|
alert.addButton(withTitle: "Install".localized())
|
|
|
|
alert.addButton(withTitle: "Quit".localized())
|
2018-08-19 21:07:50 +08:00
|
|
|
return alert.runModal() == .alertFirstButtonReturn
|
|
|
|
}
|
|
|
|
|
2018-06-13 10:44:30 +08:00
|
|
|
}
|