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-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-06-13 10:44:30 +08:00
|
|
|
|
|
|
|
let ssQueue = DispatchQueue(label: "com.w2fzu.ssqueue", attributes: .concurrent)
|
|
|
|
|
2018-06-23 14:37:00 +08:00
|
|
|
func applicationWillFinishLaunching(_ notification: Notification) {
|
|
|
|
PFMoveToApplicationsFolderIfNecessary()
|
|
|
|
}
|
|
|
|
|
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-06-13 10:44:30 +08:00
|
|
|
_ = ProxyConfigManager.install()
|
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()
|
|
|
|
startProxy()
|
|
|
|
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
let ports = getPortsC()
|
|
|
|
ConfigManager.httpProxyPort = Int(String(cString: ports.r0))!
|
|
|
|
ConfigManager.socksProxyPort = Int(String(cString: ports.r1))!
|
|
|
|
|
|
|
|
if ConfigManager.proxyPortAutoSet {
|
|
|
|
_ = ProxyConfigManager.setUpSystemProxy(port: ConfigManager.httpProxyPort,socksPort: ConfigManager.socksProxyPort)
|
|
|
|
}
|
2018-06-23 21:43:33 +08:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0) {
|
|
|
|
self.startRPCInterface()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startRPCInterface(){
|
|
|
|
|
|
|
|
class info:Codable {
|
|
|
|
let up:Int
|
|
|
|
let down:Int
|
|
|
|
}
|
|
|
|
|
|
|
|
request("http://127.0.0.1:8080/traffic").stream { [weak self] (data) in
|
|
|
|
guard let strongSelf = self else {return}
|
|
|
|
if let jsonData = try? JSONSerialization.jsonObject(with: data) as? [String:Int] {
|
|
|
|
((strongSelf.statusItem.view) as! StatusItemView).updateSpeedLabel(up: jsonData!["up"]!, down: jsonData!["down"]!)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2018-06-13 10:44:30 +08:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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) {
|
|
|
|
ssQueue.async {
|
|
|
|
updateConfigC()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:56:07 +08:00
|
|
|
|