ClashX.Meta/ClashX/AppDelegate.swift

385 lines
14 KiB
Swift
Raw Normal View History

2018-06-13 10:44:30 +08:00
//
// AppDelegate.swift
// ClashX
//
2018-08-08 13:47:38 +08:00
// Created by CYC on 2018/6/10.
// Copyright © 2018 yichengchen. All rights reserved.
2018-06-13 10:44:30 +08:00
//
import Cocoa
import LetsMove
2018-06-23 21:43:33 +08:00
import Alamofire
2018-08-04 14:33:47 +08:00
import RxCocoa
import RxSwift
2018-06-13 10:44:30 +08:00
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem: NSStatusItem!
@IBOutlet weak var statusMenu: NSMenu!
@IBOutlet weak var proxySettingMenuItem: NSMenuItem!
@IBOutlet weak var autoStartMenuItem: NSMenuItem!
2018-06-13 10:44:30 +08:00
@IBOutlet weak var proxyModeGlobalMenuItem: NSMenuItem!
@IBOutlet weak var proxyModeDirectMenuItem: NSMenuItem!
@IBOutlet weak var proxyModeRuleMenuItem: NSMenuItem!
@IBOutlet weak var allowFromLanMenuItem: NSMenuItem!
2018-08-04 21:49:32 +08:00
@IBOutlet weak var proxyModeMenuItem: NSMenuItem!
@IBOutlet weak var showNetSpeedIndicatorMenuItem: NSMenuItem!
2018-08-04 21:49:32 +08:00
@IBOutlet weak var separatorLineTop: NSMenuItem!
@IBOutlet weak var sepatatorLineEndProxySelect: NSMenuItem!
2018-08-04 21:49:32 +08:00
2018-08-12 11:29:51 +08:00
@IBOutlet weak var logLevelMenuItem: NSMenuItem!
var disposeBag = DisposeBag()
2018-06-13 10:44:30 +08:00
let ssQueue = DispatchQueue(label: "com.w2fzu.ssqueue", attributes: .concurrent)
var statusItemView:StatusItemView!
2018-06-23 20:17:05 +08:00
var isRunning = false
2018-06-13 10:44:30 +08:00
func applicationDidFinishLaunching(_ aNotification: Notification) {
signal(SIGPIPE, SIG_IGN)
failLaunchProtect()
2018-06-13 10:44:30 +08:00
_ = ProxyConfigManager.install()
PFMoveToApplicationsFolderIfNecessary()
statusItemView = StatusItemView.create(statusItem: nil,statusMenu: statusMenu)
statusItemView.onPopUpMenuAction = {
[weak self] in
guard let `self` = self else {return}
2018-08-11 13:23:06 +08:00
self.syncConfig()
}
setupData()
2018-08-19 14:07:56 +08:00
startProxy()
updateLoggingLevel()
2018-06-13 10:44:30 +08:00
}
2018-06-23 20:17:05 +08:00
2018-06-13 10:44:30 +08:00
func applicationWillTerminate(_ aNotification: Notification) {
if ConfigManager.shared.proxyPortAutoSet {
2018-06-13 10:44:30 +08:00
_ = ProxyConfigManager.setUpSystemProxy(port: nil,socksPort: nil)
}
}
func setupData() {
// check and refresh api url
_ = ConfigManager.apiUrl
// start watch config file change
ConfigFileFactory.shared.watchConfigFile()
NotificationCenter.default.rx.notification(kShouldUpDateConfig).bind {
[unowned self] (note) in
self.actionUpdateConfig(self)
}.disposed(by: disposeBag)
ConfigManager.shared
.showNetSpeedIndicatorObservable
.bind {[unowned self] (show) in
self.showNetSpeedIndicatorMenuItem.state = (show ?? true) ? .on : .off
2018-08-07 23:01:03 +08:00
self.statusItem = NSStatusBar.system.statusItem(withLength: (show ?? true) ? 65 : 25)
self.statusItem.view = self.statusItemView
self.statusItemView.showSpeedContainer(show: (show ?? true))
self.statusItemView.statusItem = self.statusItem
}.disposed(by: disposeBag)
ConfigManager.shared
.proxyPortAutoSetObservable
.distinctUntilChanged()
.bind{ [unowned self]
en in
let enable = en ?? false
self.proxySettingMenuItem.state = enable ? .on : .off
}.disposed(by: disposeBag)
let configObservable = ConfigManager.shared
.currentConfigVariable
.asObservable()
Observable.zip(configObservable,configObservable.skip(1))
.filter{(_, new) in return new != nil}
.bind {[unowned self] (old,config) in
self.proxyModeDirectMenuItem.state = .off
self.proxyModeGlobalMenuItem.state = .off
self.proxyModeRuleMenuItem.state = .off
switch config!.mode {
case .direct:self.proxyModeDirectMenuItem.state = .on
case .global:self.proxyModeGlobalMenuItem.state = .on
case .rule:self.proxyModeRuleMenuItem.state = .on
}
self.allowFromLanMenuItem.state = config!.allowLan ? .on : .off
self.proxyModeMenuItem.title = "Proxy Mode (\(config!.mode.rawValue))"
self.updateProxyList()
2018-08-20 12:25:16 +08:00
if (old?.port != config?.port && ConfigManager.shared.proxyPortAutoSet) {
_ = ProxyConfigManager.setUpSystemProxy(port: config!.port,socksPort: config!.socketPort)
}
2018-08-11 13:23:06 +08:00
self.selectProxyGroupWithMemory()
}.disposed(by: disposeBag)
LaunchAtLogin.shared
.isEnableVirable
.asObservable()
.subscribe(onNext: { (enable) in
self.autoStartMenuItem.state = enable ? .on : .off
}).disposed(by: disposeBag)
2018-06-13 10:44:30 +08:00
}
func failLaunchProtect(){
2018-08-06 15:52:38 +08:00
let x = UserDefaults.standard
var launch_fail_times:Int = 0
if let xx = x.object(forKey: "launch_fail_times") as? Int {launch_fail_times = xx }
launch_fail_times += 1
x.set(launch_fail_times, forKey: "launch_fail_times")
2018-08-19 11:31:43 +08:00
if launch_fail_times > 2{
2018-08-06 15:52:38 +08:00
//
let path = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash/")
let documentDirectory = URL(fileURLWithPath: path)
let originPath = documentDirectory.appendingPathComponent("config.ini")
let destinationPath = documentDirectory.appendingPathComponent("config.ini.bak")
try? FileManager.default.removeItem(at:destinationPath)
2018-08-06 15:52:38 +08:00
try? FileManager.default.moveItem(at: originPath, to: destinationPath)
try? FileManager.default.removeItem(at: documentDirectory.appendingPathComponent("Country.mmdb"))
NSUserNotificationCenter.default.post(title: "Fail on launch protect", info: "You origin Config has been rename to config.ini.bak")
}
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double(Int64(1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {
x.set(0, forKey: "launch_fail_times")
});
}
func selectProxyGroupWithMemory(){
for item in ConfigManager.selectedProxyMap {
ApiRequest.updateProxyGroup(group: item.key, selectProxy: item.value) { (success) in
if (!success) {
ConfigManager.selectedProxyMap[item.key] = nil
}
}
}
}
func selectOutBoundModeWithMenory() {
ApiRequest.updateOutBoundMode(mode: ConfigManager.selectOutBoundMode){
_ in
self.syncConfig()
}
}
func selectAllowLanWithMenory() {
ApiRequest.updateAllowLan(allow: ConfigManager.allowConnectFromLan){
self.syncConfig()
}
}
func updateProxyList() {
2018-08-04 21:49:32 +08:00
ProxyMenuItemFactory.menuItems { [unowned self] (menus) in
let startIndex = self.statusMenu.items.index(of: self.separatorLineTop)!+1
let endIndex = self.statusMenu.items.index(of: self.sepatatorLineEndProxySelect)!
2018-08-04 21:49:32 +08:00
var items = self.statusMenu.items
self.sepatatorLineEndProxySelect.isHidden = menus.count == 0
items.removeSubrange(Range(uncheckedBounds: (lower: startIndex, upper: endIndex)))
2018-08-04 21:49:32 +08:00
for each in menus {
items.insert(each, at: startIndex)
2018-08-04 21:49:32 +08:00
}
self.statusMenu.removeAllItems()
for each in items.reversed() {
self.statusMenu.insertItem(each, at: 0)
}
}
}
2018-08-12 11:29:51 +08:00
func updateLoggingLevel() {
for item in self.logLevelMenuItem.submenu?.items ?? [] {
item.state = item.title.lowercased() == ConfigManager.selectLoggingApiLevel.rawValue ? .on : .off
}
}
2018-06-13 10:44:30 +08:00
func startProxy() {
if self.isRunning {return}
self.isRunning = true
print("Trying start proxy")
if let cstring = run() {
// self.isRunning = false
let error = String(cString: cstring)
if (error != "success") {
NSUserNotificationCenter.default.postConfigErrorNotice(msg:error)
} else {
self.resetStreamApi()
self.selectOutBoundModeWithMenory()
self.selectAllowLanWithMenory()
}
2018-06-13 10:44:30 +08:00
}
2018-08-04 14:33:47 +08:00
}
func syncConfig(completeHandler:(()->())? = nil){
2018-07-30 15:55:10 +08:00
ApiRequest.requestConfig{ (config) in
2018-08-04 14:33:47 +08:00
guard config.port > 0 else {return}
ConfigManager.shared.currentConfig = config
completeHandler?()
2018-06-23 21:43:33 +08:00
}
}
2018-08-07 15:09:25 +08:00
func resetStreamApi() {
2018-08-04 14:33:47 +08:00
ApiRequest.shared.requestTrafficInfo(){ [weak self] up,down in
2018-07-30 15:55:10 +08:00
guard let `self` = self else {return}
((self.statusItem.view) as! StatusItemView).updateSpeedLabel(up: up, down: down)
2018-06-23 21:43:33 +08:00
}
2018-08-07 15:09:25 +08:00
ApiRequest.shared.requestLog { (type, msg) in
Logger.log(msg: msg,level: ClashLogLevel(rawValue: type) ?? .unknow)
2018-08-07 15:09:25 +08:00
}
2018-06-13 10:44:30 +08:00
}
2018-08-04 14:33:47 +08:00
//Actions:
2018-06-23 21:43:33 +08:00
2018-06-13 10:44:30 +08:00
@IBAction func actionQuit(_ sender: Any) {
NSApplication.shared.terminate(self)
}
2018-08-04 14:33:47 +08:00
2018-06-13 10:44:30 +08:00
@IBAction func actionSetSystemProxy(_ sender: Any) {
ConfigManager.shared.proxyPortAutoSet = !ConfigManager.shared.proxyPortAutoSet
if ConfigManager.shared.proxyPortAutoSet {
let port = ConfigManager.shared.currentConfig?.port ?? 0
let socketPort = ConfigManager.shared.currentConfig?.socketPort ?? 0
_ = ProxyConfigManager.setUpSystemProxy(port: port,socksPort:socketPort)
2018-06-13 10:44:30 +08:00
} else {
_ = ProxyConfigManager.setUpSystemProxy(port: nil,socksPort: nil)
}
}
@IBAction func actionCopyExportCommand(_ sender: Any) {
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
let port = ConfigManager.shared.currentConfig?.port ?? 0
pasteboard.setString("export https_proxy=http://127.0.0.1:\(port);export http_proxy=http://127.0.0.1:\(port)", forType: .string)
2018-06-13 10:44:30 +08:00
}
@IBAction func actionAllowFromLan(_ sender: NSMenuItem) {
ApiRequest.updateAllowLan(allow: !ConfigManager.allowConnectFromLan) {
[unowned self] in
self.syncConfig()
ConfigManager.allowConnectFromLan = !ConfigManager.allowConnectFromLan
}
}
2018-06-13 10:44:30 +08:00
@IBAction func actionStartAtLogin(_ sender: NSMenuItem) {
LaunchAtLogin.shared.isEnabled = !LaunchAtLogin.shared.isEnabled
}
var genConfigWindow:NSWindowController?=nil
@IBAction func actionGenConfig(_ sender: Any) {
let ctrl = PreferencesWindowController(windowNibName: NSNib.Name(rawValue: "PreferencesWindowController"))
2018-06-23 14:27:04 +08:00
genConfigWindow?.close()
genConfigWindow=ctrl
2018-06-23 14:27:04 +08:00
ctrl.window?.title = ctrl.contentViewController?.title ?? ""
ctrl.showWindow(nil)
NSApp.activate(ignoringOtherApps: true)
ctrl.window?.makeKeyAndOrderFront(self)
}
2018-06-13 10:44:30 +08:00
@IBAction func openConfigFolder(_ sender: Any) {
2018-08-19 11:14:41 +08:00
let path = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash")
2018-06-13 10:44:30 +08:00
NSWorkspace.shared.openFile(path)
}
2018-06-13 10:44:30 +08:00
@IBAction func actionUpdateConfig(_ sender: Any) {
ApiRequest.requestConfigUpdate() { [unowned self] error in
if (error == nil) {
2018-08-11 13:23:06 +08:00
self.syncConfig()
self.resetStreamApi()
self.selectProxyGroupWithMemory()
self.selectOutBoundModeWithMenory()
NSUserNotificationCenter
.default
.post(title: "Reload Config Succeed", info: "succees")
} else {
NSUserNotificationCenter
.default
.post(title: "Reload Config Fail", info: error ?? "")
}
2018-06-13 10:44:30 +08:00
}
}
2018-08-12 11:29:51 +08:00
@IBAction func actionSetLogLevel(_ sender: NSMenuItem) {
let level = ClashLogLevel(rawValue: sender.title.lowercased()) ?? .unknow
ConfigManager.selectLoggingApiLevel = level
updateLoggingLevel()
resetStreamApi()
}
@IBAction func actionImportBunchJsonFile(_ sender: NSMenuItem) {
ConfigFileFactory.importConfigFile()
}
@IBAction func actionSwitchProxyMode(_ sender: NSMenuItem) {
let mode:ClashProxyMode
switch sender {
case proxyModeGlobalMenuItem:
mode = .global
case proxyModeDirectMenuItem:
mode = .direct
case proxyModeRuleMenuItem:
mode = .rule
default:
return
}
let config = ConfigManager.shared.currentConfig?.copy()
config?.mode = mode
ApiRequest.updateOutBoundMode(mode: mode) { (success) in
ConfigManager.shared.currentConfig = config
ConfigManager.selectOutBoundMode = mode
}
}
@IBAction func actionImportConfigFromSSURL(_ sender: NSMenuItem) {
let pasteBoard = NSPasteboard.general.string(forType: NSPasteboard.PasteboardType.string)
if let proxyModel = ProxyServerModel(urlStr: pasteBoard ?? "") {
ConfigFileFactory.addProxyToConfig(proxy: proxyModel)
} else {
NSUserNotificationCenter.default.postImportConfigFromUrlFailNotice(urlStr: pasteBoard ?? "empty")
}
}
@IBAction func actionScanQRCode(_ sender: NSMenuItem) {
if let urls = QRCodeUtil.ScanQRCodeOnScreen() {
for url in urls {
if let proxyModel = ProxyServerModel(urlStr: url) {
ConfigFileFactory.addProxyToConfig(proxy: proxyModel)
} else {
NSUserNotificationCenter
.default
.postImportConfigFromUrlFailNotice(urlStr: url)
}
}
}else {
NSUserNotificationCenter.default.postQRCodeNotFoundNotice()
}
}
@IBAction func actionShowNetSpeedIndicator(_ sender: NSMenuItem) {
ConfigManager.shared.showNetSpeedIndicator = !ConfigManager.shared.showNetSpeedIndicator
}
@IBAction func actionShowLog(_ sender: Any) {
NSWorkspace.shared.openFile(Logger.shared.logFilePath())
}
2018-06-13 10:44:30 +08:00
}