2018-06-13 10:44:30 +08:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
class ProxyConfigManager {
|
2018-06-14 12:56:07 +08:00
|
|
|
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)
|
2018-06-14 12:56:07 +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) {
|
|
|
|
print(res.stringValue ?? "")
|
2018-06-14 12:56:07 +08:00
|
|
|
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 {
|
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-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)
|
|
|
|
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) {
|
|
|
|
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()
|
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
|
|
|
|
}
|
|
|
|
}
|