Fixed: config reload logic
This commit is contained in:
parent
3ca9c85b89
commit
409facd6af
@ -8,6 +8,7 @@
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
0D318CDAC9464E01DAD1F92B /* Pods_ClashX.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0358BF8F0CBD7FF9A7EA4E30 /* Pods_ClashX.framework */; };
|
||||
4913C82321157D0200F6B87C /* Notification.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4913C82221157D0200F6B87C /* Notification.swift */; };
|
||||
492C4865210EC9E4004554A0 /* ProxyConfig in Resources */ = {isa = PBXBuildFile; fileRef = 492C4864210EC9E4004554A0 /* ProxyConfig */; };
|
||||
492C4869210EE6B9004554A0 /* ApiRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 492C4868210EE6B9004554A0 /* ApiRequest.swift */; };
|
||||
492C4871210EF62E004554A0 /* ClashConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 492C4870210EF62E004554A0 /* ClashConfig.swift */; };
|
||||
@ -68,6 +69,7 @@
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
0358BF8F0CBD7FF9A7EA4E30 /* Pods_ClashX.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ClashX.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
4913C82221157D0200F6B87C /* Notification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notification.swift; sourceTree = "<group>"; };
|
||||
492C4864210EC9E4004554A0 /* ProxyConfig */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; name = ProxyConfig; path = "../../../../Library/Developer/Xcode/DerivedData/ClashX-dojkemepirlxgtgjgkqxpjrbgopn/Build/Products/Debug/ProxyConfig"; sourceTree = "<group>"; };
|
||||
492C4868210EE6B9004554A0 /* ApiRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiRequest.swift; sourceTree = "<group>"; };
|
||||
492C4870210EF62E004554A0 /* ClashConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashConfig.swift; sourceTree = "<group>"; };
|
||||
@ -127,6 +129,14 @@
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
4913C82021157CEB00F6B87C /* Macro */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
4913C82221157D0200F6B87C /* Notification.swift */,
|
||||
);
|
||||
path = Macro;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
492C4866210EE69B004554A0 /* General */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@ -197,6 +207,7 @@
|
||||
49CF3B1F20CD7463001EBF94 /* ClashX */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
4913C82021157CEB00F6B87C /* Macro */,
|
||||
492C486E210EF621004554A0 /* Models */,
|
||||
492C4866210EE69B004554A0 /* General */,
|
||||
4997732220D251A60009B136 /* Basic */,
|
||||
@ -509,6 +520,7 @@
|
||||
495340B320DE68C300B0D3FF /* StatusItemView.swift in Sources */,
|
||||
495A44D320D267D000888A0A /* LaunchAtLogin.swift in Sources */,
|
||||
49CF3B2120CD7463001EBF94 /* AppDelegate.swift in Sources */,
|
||||
4913C82321157D0200F6B87C /* Notification.swift in Sources */,
|
||||
492C4871210EF62E004554A0 /* ClashConfig.swift in Sources */,
|
||||
492C4869210EE6B9004554A0 /* ApiRequest.swift in Sources */,
|
||||
49CF3B6520CEE06C001EBF94 /* ConfigManager.swift in Sources */,
|
||||
|
@ -9,6 +9,8 @@
|
||||
import Cocoa
|
||||
import LetsMove
|
||||
import Alamofire
|
||||
import RxCocoa
|
||||
import RxSwift
|
||||
|
||||
@NSApplicationMain
|
||||
class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
@ -18,6 +20,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
@IBOutlet weak var statusMenu: NSMenu!
|
||||
@IBOutlet weak var proxySettingMenuItem: NSMenuItem!
|
||||
@IBOutlet weak var autoStartMenuItem: NSMenuItem!
|
||||
var disposeBag = DisposeBag()
|
||||
|
||||
let ssQueue = DispatchQueue(label: "com.w2fzu.ssqueue", attributes: .concurrent)
|
||||
|
||||
@ -57,11 +60,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
ssQueue.async {
|
||||
run()
|
||||
}
|
||||
syncConfigAndStartTrafficMonitor()
|
||||
}
|
||||
|
||||
func syncConfigAndStartTrafficMonitor(){
|
||||
ApiRequest.requestConfig{ (config) in
|
||||
print(config.port)
|
||||
guard config.port > 0 else {return}
|
||||
ConfigManager.httpProxyPort = config.port
|
||||
ConfigManager.socksProxyPort = config.socketPort
|
||||
|
||||
|
||||
if ConfigManager.proxyPortAutoSet {
|
||||
_ = ProxyConfigManager.setUpSystemProxy(port: ConfigManager.httpProxyPort,socksPort: ConfigManager.socksProxyPort)
|
||||
}
|
||||
@ -69,20 +76,27 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
func startTrafficMonitor(){
|
||||
ApiRequest.requestTrafficInfo(){ [weak self] up,down in
|
||||
func startTrafficMonitor() {
|
||||
ApiRequest.shared.requestTrafficInfo(){ [weak self] up,down in
|
||||
guard let `self` = self else {return}
|
||||
((self.statusItem.view) as! StatusItemView).updateSpeedLabel(up: up, down: down)
|
||||
}
|
||||
}
|
||||
|
||||
func registerNotification() {
|
||||
NotificationCenter.default.rx.notification(kShouldUpDateConfig).bind {
|
||||
[weak self] (note) in
|
||||
guard let `self` = self else {return}
|
||||
self.syncConfigAndStartTrafficMonitor()
|
||||
}.disposed(by: disposeBag)
|
||||
}
|
||||
|
||||
//Actions:
|
||||
|
||||
@IBAction func actionQuit(_ sender: Any) {
|
||||
NSApplication.shared.terminate(self)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@IBAction func actionSetSystemProxy(_ sender: Any) {
|
||||
ConfigManager.proxyPortAutoSet = !ConfigManager.proxyPortAutoSet
|
||||
updateMenuItem()
|
||||
@ -126,6 +140,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
@IBAction func actionUpdateConfig(_ sender: Any) {
|
||||
ApiRequest.requestConfigUpdate() { [weak self] success in
|
||||
guard let strongSelf = self else {return}
|
||||
strongSelf.syncConfigAndStartTrafficMonitor()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,10 @@ import Alamofire
|
||||
|
||||
|
||||
class ApiRequest{
|
||||
static let shared = ApiRequest()
|
||||
|
||||
var trafficReq:DataRequest? = nil
|
||||
|
||||
static func requestConfig(completeHandler:@escaping ((ClashConfig)->())){
|
||||
request(ConfigManager.apiUrl + "/configs", method: .get).responseData{
|
||||
res in
|
||||
@ -21,8 +25,11 @@ class ApiRequest{
|
||||
}
|
||||
}
|
||||
|
||||
static func requestTrafficInfo(callback:@escaping ((Int,Int)->()) ){
|
||||
request(ConfigManager.apiUrl + "/traffic").stream {(data) in
|
||||
func requestTrafficInfo(callback:@escaping ((Int,Int)->()) ){
|
||||
self.trafficReq?.cancel()
|
||||
|
||||
self.trafficReq =
|
||||
request(ConfigManager.apiUrl + "/traffic").stream {(data) in
|
||||
if let jsonData = try? JSONSerialization.jsonObject(with: data) as? [String:Int] {
|
||||
callback(jsonData!["up"] ?? 0, jsonData!["down"] ?? 0)
|
||||
}
|
||||
|
10
ClashX/Macro/Notification.swift
Normal file
10
ClashX/Macro/Notification.swift
Normal file
@ -0,0 +1,10 @@
|
||||
//
|
||||
// Notification.swift
|
||||
// ClashX
|
||||
//
|
||||
// Created by CYC on 2018/8/4.
|
||||
// Copyright © 2018年 west2online. All rights reserved.
|
||||
//
|
||||
import Foundation
|
||||
|
||||
let kShouldUpDateConfig = Notification.Name("kShouldUpDateConfig")
|
@ -12,8 +12,8 @@ import Cocoa
|
||||
class ConfigManager {
|
||||
|
||||
private static let instance = ConfigManager()
|
||||
var _httpProxyPort = 0
|
||||
var _socksProxyPort = 0
|
||||
private var _httpProxyPort = 0
|
||||
private var _socksProxyPort = 0
|
||||
|
||||
static var proxyPortAutoSet:Bool {
|
||||
get{
|
||||
@ -47,4 +47,6 @@ class ConfigManager {
|
||||
return "http://127.0.0.1:8080"
|
||||
}
|
||||
}
|
||||
|
||||
// func reloadClashConfig
|
||||
}
|
||||
|
@ -93,9 +93,7 @@ class SampleConfigViewController: NSViewController {
|
||||
if (vaildConfig()) {
|
||||
generateConfig()
|
||||
self.view.window?.windowController?.close()
|
||||
DispatchQueue(label: "com.w2fzu.ssqueue", attributes: .concurrent).async {
|
||||
// updateConfigC()
|
||||
}
|
||||
NotificationCenter.default.post(Notification(name:kShouldUpDateConfig))
|
||||
} else {
|
||||
shakeWindows()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user