ClashX.Meta/ClashX/Managers/ProxyConfigManager.swift

86 lines
3.1 KiB
Swift
Raw Normal View History

2018-06-13 10:44:30 +08:00
import Foundation
class ProxyConfigManager {
static let kProxyConfigFolder = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash")
2018-08-10 20:58:20 +08:00
static let kVersion = "0.1.1"
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"
print(scriptPath)
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) {
print(res.stringValue ?? "")
if (res.stringValue?.contains("success")) ?? false {
2018-06-13 10:44:30 +08:00
return true
}
}
return false
}
2018-08-11 13:23:06 +08:00
static func install() -> Bool {
checkConfigDir()
2018-06-13 10:44:30 +08:00
checkMMDB()
let proxyHelperPath = Bundle.main.path(forResource: "ProxyConfig", ofType: nil)
let targetPath = "\(kProxyConfigFolder)/ProxyConfig"
2018-08-10 20:58:20 +08:00
if !vaildHelper() {
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"
let appleScriptStr = "do shell script \"bash \(scriptPath) \(kProxyConfigFolder) \" with administrator privileges"
2018-06-13 10:44:30 +08:00
let appleScript = NSAppleScript(source: appleScriptStr)
var dict: NSDictionary?
if let _ = appleScript?.executeAndReturnError(&dict) {
2018-06-13 10:44:30 +08:00
return true
} else {
return false
}
}
return true
}
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
let destMMDBPath = "\(kProxyConfigFolder)/Country.mmdb"
2018-06-13 10:44:30 +08:00
if !fileManage.fileExists(atPath: destMMDBPath) {
let mmdbPath = Bundle.main.path(forResource: "Country", ofType: "mmdb")
try! fileManage.copyItem(at: URL(fileURLWithPath: mmdbPath!), to: URL(fileURLWithPath: destMMDBPath))
}
}
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()
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
}
}