2018-06-13 10:44:30 +08:00
|
|
|
//
|
|
|
|
// AppDelegate.swift
|
|
|
|
// ClashX
|
|
|
|
//
|
|
|
|
// Created by 称一称 on 2018/6/10.
|
|
|
|
// Copyright © 2018年 west2online. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
2018-06-23 14:37:00 +08:00
|
|
|
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-14 12:56:07 +08:00
|
|
|
|
2018-06-13 10:44:30 +08:00
|
|
|
@NSApplicationMain
|
|
|
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
|
|
|
var statusItem: NSStatusItem!
|
2018-06-23 20:17:05 +08:00
|
|
|
static let StatusItemIconWidth: CGFloat = NSStatusItem.variableLength * 2
|
2018-06-13 10:44:30 +08:00
|
|
|
|
|
|
|
@IBOutlet weak var statusMenu: NSMenu!
|
|
|
|
@IBOutlet weak var proxySettingMenuItem: NSMenuItem!
|
2018-06-14 16:16:00 +08:00
|
|
|
@IBOutlet weak var autoStartMenuItem: NSMenuItem!
|
2018-08-04 14:33:47 +08:00
|
|
|
var disposeBag = DisposeBag()
|
2018-06-13 10:44:30 +08:00
|
|
|
|
|
|
|
let ssQueue = DispatchQueue(label: "com.w2fzu.ssqueue", attributes: .concurrent)
|
|
|
|
|
2018-06-23 20:17:05 +08:00
|
|
|
|
2018-06-13 10:44:30 +08:00
|
|
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
2018-06-14 12:56:07 +08:00
|
|
|
signal(SIGPIPE, SIG_IGN)
|
2018-08-03 16:49:40 +08:00
|
|
|
|
2018-06-13 10:44:30 +08:00
|
|
|
_ = ProxyConfigManager.install()
|
2018-07-30 13:35:18 +08:00
|
|
|
PFMoveToApplicationsFolderIfNecessary()
|
2018-08-03 16:49:40 +08:00
|
|
|
self.startProxy()
|
2018-07-30 13:35:18 +08:00
|
|
|
|
2018-06-23 20:17:05 +08:00
|
|
|
statusItem = NSStatusBar.system.statusItem(withLength: 57)
|
|
|
|
let view = StatusItemView.create(statusItem: statusItem,statusMenu: statusMenu)
|
|
|
|
statusItem.view = view
|
2018-06-13 10:44:30 +08:00
|
|
|
updateMenuItem()
|
|
|
|
|
|
|
|
}
|
2018-06-23 20:17:05 +08:00
|
|
|
|
2018-06-13 10:44:30 +08:00
|
|
|
|
|
|
|
func applicationWillTerminate(_ aNotification: Notification) {
|
|
|
|
if ConfigManager.proxyPortAutoSet {
|
|
|
|
_ = ProxyConfigManager.setUpSystemProxy(port: nil,socksPort: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateMenuItem(){
|
|
|
|
proxySettingMenuItem.state = ConfigManager.proxyPortAutoSet ? .on : .off
|
2018-06-14 16:16:00 +08:00
|
|
|
autoStartMenuItem.state = LaunchAtLogin.isEnabled ? .on:.off
|
2018-06-23 14:27:04 +08:00
|
|
|
let image = proxySettingMenuItem.state == .on ?
|
|
|
|
NSImage(named: NSImage.Name(rawValue: "menu_icon"))! :
|
|
|
|
NSImage(named: NSImage.Name(rawValue: "menu_icon_disabled"))!
|
2018-06-14 16:16:00 +08:00
|
|
|
|
2018-06-23 14:27:04 +08:00
|
|
|
statusItem.image = image
|
2018-06-13 10:44:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func startProxy() {
|
|
|
|
ssQueue.async {
|
|
|
|
run()
|
|
|
|
}
|
2018-08-04 14:33:47 +08:00
|
|
|
syncConfigAndStartTrafficMonitor()
|
|
|
|
}
|
|
|
|
|
|
|
|
func syncConfigAndStartTrafficMonitor(){
|
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}
|
2018-07-30 15:55:10 +08:00
|
|
|
ConfigManager.httpProxyPort = config.port
|
|
|
|
ConfigManager.socksProxyPort = config.socketPort
|
2018-08-04 14:33:47 +08:00
|
|
|
|
2018-07-30 15:55:10 +08:00
|
|
|
if ConfigManager.proxyPortAutoSet {
|
|
|
|
_ = ProxyConfigManager.setUpSystemProxy(port: ConfigManager.httpProxyPort,socksPort: ConfigManager.socksProxyPort)
|
|
|
|
}
|
|
|
|
self.startTrafficMonitor()
|
2018-06-23 21:43:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 14:33:47 +08:00
|
|
|
func startTrafficMonitor() {
|
|
|
|
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-06-13 10:44:30 +08:00
|
|
|
}
|
|
|
|
|
2018-08-04 14:33:47 +08:00
|
|
|
func registerNotification() {
|
|
|
|
NotificationCenter.default.rx.notification(kShouldUpDateConfig).bind {
|
|
|
|
[weak self] (note) in
|
|
|
|
guard let `self` = self else {return}
|
|
|
|
self.syncConfigAndStartTrafficMonitor()
|
|
|
|
}.disposed(by: disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
//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.proxyPortAutoSet = !ConfigManager.proxyPortAutoSet
|
|
|
|
updateMenuItem()
|
|
|
|
if ConfigManager.proxyPortAutoSet {
|
|
|
|
_ = ProxyConfigManager.setUpSystemProxy(port: ConfigManager.httpProxyPort,socksPort: ConfigManager.socksProxyPort)
|
|
|
|
} else {
|
|
|
|
_ = ProxyConfigManager.setUpSystemProxy(port: nil,socksPort: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func actionCopyExportCommand(_ sender: Any) {
|
|
|
|
let pasteboard = NSPasteboard.general
|
|
|
|
pasteboard.clearContents()
|
|
|
|
pasteboard.setString("export https_proxy=http://127.0.0.1:\(ConfigManager.httpProxyPort);export http_proxy=http://127.0.0.1:\(ConfigManager.httpProxyPort)", forType: .string)
|
|
|
|
}
|
|
|
|
|
2018-06-14 16:16:00 +08:00
|
|
|
@IBAction func actionStartAtLogin(_ sender: NSMenuItem) {
|
|
|
|
LaunchAtLogin.isEnabled = !LaunchAtLogin.isEnabled
|
|
|
|
updateMenuItem()
|
|
|
|
}
|
|
|
|
|
|
|
|
var genConfigWindow:NSWindowController?=nil
|
|
|
|
@IBAction func actionGenConfig(_ sender: Any) {
|
|
|
|
let ctrl = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
|
|
|
|
.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "sampleConfigGenerator")) as! NSWindowController
|
2018-06-23 14:27:04 +08:00
|
|
|
|
2018-06-14 16:16:00 +08:00
|
|
|
genConfigWindow?.close()
|
|
|
|
genConfigWindow=ctrl
|
2018-06-23 14:27:04 +08:00
|
|
|
ctrl.window?.title = ctrl.contentViewController?.title ?? ""
|
2018-06-14 16:16:00 +08:00
|
|
|
ctrl.showWindow(nil)
|
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
ctrl.window?.makeKeyAndOrderFront(self)
|
|
|
|
|
|
|
|
}
|
2018-06-13 10:44:30 +08:00
|
|
|
|
|
|
|
@IBAction func openConfigFolder(_ sender: Any) {
|
|
|
|
let path = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash/config.ini")
|
|
|
|
NSWorkspace.shared.openFile(path)
|
|
|
|
}
|
|
|
|
@IBAction func actionUpdateConfig(_ sender: Any) {
|
2018-07-30 15:55:10 +08:00
|
|
|
ApiRequest.requestConfigUpdate() { [weak self] success in
|
|
|
|
guard let strongSelf = self else {return}
|
2018-08-04 14:33:47 +08:00
|
|
|
strongSelf.syncConfigAndStartTrafficMonitor()
|
2018-06-13 10:44:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:56:07 +08:00
|
|
|
|