diff --git a/ClashX.xcodeproj/project.pbxproj b/ClashX.xcodeproj/project.pbxproj index 22bd04c..e6675e1 100644 --- a/ClashX.xcodeproj/project.pbxproj +++ b/ClashX.xcodeproj/project.pbxproj @@ -8,6 +8,9 @@ /* Begin PBXBuildFile section */ 0D318CDAC9464E01DAD1F92B /* Pods_ClashX.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0358BF8F0CBD7FF9A7EA4E30 /* Pods_ClashX.framework */; }; + 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 */; }; 495340B020DE5F7200B0D3FF /* StatusItemView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 495340AF20DE5F7200B0D3FF /* StatusItemView.xib */; }; 495340B320DE68C300B0D3FF /* StatusItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495340B220DE68C300B0D3FF /* StatusItemView.swift */; }; 495A44BF20D2660A00888A0A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495A44BE20D2660A00888A0A /* AppDelegate.swift */; }; @@ -28,7 +31,6 @@ 49CF3B4220CD7845001EBF94 /* menu_icon@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 49CF3B3E20CD7845001EBF94 /* menu_icon@2x.png */; }; 49CF3B5C20CE8068001EBF94 /* ProxyConfigManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49CF3B5B20CE8068001EBF94 /* ProxyConfigManager.swift */; }; 49CF3B5F20CE80D2001EBF94 /* install_proxy_helper.sh in Resources */ = {isa = PBXBuildFile; fileRef = 49CF3B5E20CE80D2001EBF94 /* install_proxy_helper.sh */; }; - 49CF3B6120CE8404001EBF94 /* ProxyConfig in Resources */ = {isa = PBXBuildFile; fileRef = 49CF3B6020CE8404001EBF94 /* ProxyConfig */; }; 49CF3B6320CED9CF001EBF94 /* check_proxy_helper.sh in Resources */ = {isa = PBXBuildFile; fileRef = 49CF3B6220CED934001EBF94 /* check_proxy_helper.sh */; }; 49CF3B6520CEE06C001EBF94 /* ConfigManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49CF3B6420CEE06C001EBF94 /* ConfigManager.swift */; }; 49E07A8C20D501A000A088A3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 49E07A8920D501A000A088A3 /* Main.storyboard */; }; @@ -66,6 +68,9 @@ /* Begin PBXFileReference section */ 0358BF8F0CBD7FF9A7EA4E30 /* Pods_ClashX.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ClashX.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 492C4864210EC9E4004554A0 /* ProxyConfig */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; name = ProxyConfig; path = "../../../../Library/Developer/Xcode/DerivedData/ClashX-dojkemepirlxgtgjgkqxpjrbgopn/Build/Products/Debug/ProxyConfig"; sourceTree = ""; }; + 492C4868210EE6B9004554A0 /* ApiRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiRequest.swift; sourceTree = ""; }; + 492C4870210EF62E004554A0 /* ClashConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashConfig.swift; sourceTree = ""; }; 495340AF20DE5F7200B0D3FF /* StatusItemView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = StatusItemView.xib; sourceTree = ""; }; 495340B220DE68C300B0D3FF /* StatusItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusItemView.swift; sourceTree = ""; }; 495A44BC20D2660A00888A0A /* ClashXLaunchHelper.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ClashXLaunchHelper.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -95,7 +100,6 @@ 49CF3B4B20CE5232001EBF94 /* ProxyConfig.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; path = ProxyConfig.xcodeproj; sourceTree = ""; }; 49CF3B5B20CE8068001EBF94 /* ProxyConfigManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxyConfigManager.swift; sourceTree = ""; }; 49CF3B5E20CE80D2001EBF94 /* install_proxy_helper.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = install_proxy_helper.sh; sourceTree = ""; }; - 49CF3B6020CE8404001EBF94 /* ProxyConfig */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; name = ProxyConfig; path = "../../../../../Library/Developer/Xcode/DerivedData/ClashX-bcbrsbofqgevdffhsbymjskmsxik/Build/Products/Debug/ProxyConfig"; sourceTree = ""; }; 49CF3B6220CED934001EBF94 /* check_proxy_helper.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = check_proxy_helper.sh; sourceTree = ""; }; 49CF3B6420CEE06C001EBF94 /* ConfigManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigManager.swift; sourceTree = ""; }; 49E07A8A20D501A000A088A3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Main.storyboard; sourceTree = ""; }; @@ -123,6 +127,22 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 492C4866210EE69B004554A0 /* General */ = { + isa = PBXGroup; + children = ( + 492C4868210EE6B9004554A0 /* ApiRequest.swift */, + ); + path = General; + sourceTree = ""; + }; + 492C486E210EF621004554A0 /* Models */ = { + isa = PBXGroup; + children = ( + 492C4870210EF62E004554A0 /* ClashConfig.swift */, + ); + path = Models; + sourceTree = ""; + }; 495A44BD20D2660A00888A0A /* ClashXLaunchHelper */ = { isa = PBXGroup; children = ( @@ -177,6 +197,8 @@ 49CF3B1F20CD7463001EBF94 /* ClashX */ = { isa = PBXGroup; children = ( + 492C486E210EF621004554A0 /* Models */, + 492C4866210EE69B004554A0 /* General */, 4997732220D251A60009B136 /* Basic */, 4989F98520D0AA300001E564 /* ViewControllers */, 49CF3B5D20CE80D2001EBF94 /* script */, @@ -200,10 +222,10 @@ children = ( 4989F98D20D0AE990001E564 /* sampleConfig.ini */, 4989F98320D02D200001E564 /* Country.mmdb */, - 49CF3B6020CE8404001EBF94 /* ProxyConfig */, 49CF3B3D20CD7845001EBF94 /* menu_icon_disabled.png */, 49CF3B3C20CD7845001EBF94 /* menu_icon_disabled@2x.png */, 49CF3B3B20CD7845001EBF94 /* menu_icon.png */, + 492C4864210EC9E4004554A0 /* ProxyConfig */, 49CF3B3E20CD7845001EBF94 /* menu_icon@2x.png */, ); path = "Support Files"; @@ -392,13 +414,13 @@ 495340B020DE5F7200B0D3FF /* StatusItemView.xib in Resources */, 49CF3B2820CD7465001EBF94 /* Main.storyboard in Resources */, 49CF3B4220CD7845001EBF94 /* menu_icon@2x.png in Resources */, - 49CF3B6120CE8404001EBF94 /* ProxyConfig in Resources */, 49CF3B4120CD7845001EBF94 /* menu_icon_disabled.png in Resources */, 49CF3B5F20CE80D2001EBF94 /* install_proxy_helper.sh in Resources */, 49CF3B4020CD7845001EBF94 /* menu_icon_disabled@2x.png in Resources */, 49CF3B3F20CD7845001EBF94 /* menu_icon.png in Resources */, 4989F98420D02D200001E564 /* Country.mmdb in Resources */, 497F0DF320DE2FE50077AD41 /* Icon.icns in Resources */, + 492C4865210EC9E4004554A0 /* ProxyConfig in Resources */, 4989F98E20D0AE990001E564 /* sampleConfig.ini in Resources */, 49CF3B6320CED9CF001EBF94 /* check_proxy_helper.sh in Resources */, ); @@ -485,6 +507,8 @@ 495340B320DE68C300B0D3FF /* StatusItemView.swift in Sources */, 495A44D320D267D000888A0A /* LaunchAtLogin.swift in Sources */, 49CF3B2120CD7463001EBF94 /* AppDelegate.swift in Sources */, + 492C4871210EF62E004554A0 /* ClashConfig.swift in Sources */, + 492C4869210EE6B9004554A0 /* ApiRequest.swift in Sources */, 49CF3B6520CEE06C001EBF94 /* ConfigManager.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -527,7 +551,10 @@ COMBINE_HIDPI_IMAGES = YES; DEVELOPMENT_TEAM = MEWHFZ92DY; INFOPLIST_FILE = ClashXLaunchHelper/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); PRODUCT_BUNDLE_IDENTIFIER = com.west2online.ClashX.LaunchHelper; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; @@ -543,7 +570,10 @@ COMBINE_HIDPI_IMAGES = YES; DEVELOPMENT_TEAM = MEWHFZ92DY; INFOPLIST_FILE = ClashXLaunchHelper/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); PRODUCT_BUNDLE_IDENTIFIER = com.west2online.ClashX.LaunchHelper; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; @@ -677,7 +707,10 @@ "$(PROJECT_DIR)/Carthage/Build/Mac", ); INFOPLIST_FILE = ClashX/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/ClashX", @@ -705,7 +738,10 @@ "$(PROJECT_DIR)/Carthage/Build/Mac", ); INFOPLIST_FILE = ClashX/Info.plist; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/ClashX", diff --git a/ClashX/AppDelegate.swift b/ClashX/AppDelegate.swift index 05bc174..3bc49d7 100644 --- a/ClashX/AppDelegate.swift +++ b/ClashX/AppDelegate.swift @@ -58,33 +58,22 @@ class AppDelegate: NSObject, NSApplicationDelegate { 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) - } - DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0) { - self.startRPCInterface() + ApiRequest.requestConfig{ (config) in + print(config.port) + ConfigManager.httpProxyPort = config.port + ConfigManager.socksProxyPort = config.socketPort + if ConfigManager.proxyPortAutoSet { + _ = ProxyConfigManager.setUpSystemProxy(port: ConfigManager.httpProxyPort,socksPort: ConfigManager.socksProxyPort) + } + self.startTrafficMonitor() } } - 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"]!) - - } + func startTrafficMonitor(){ + ApiRequest.requestTrafficInfo(){ [weak self] up,down in + guard let `self` = self else {return} + ((self.statusItem.view) as! StatusItemView).updateSpeedLabel(up: up, down: down) } } @@ -136,8 +125,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { NSWorkspace.shared.openFile(path) } @IBAction func actionUpdateConfig(_ sender: Any) { - ssQueue.async { - updateConfigC() + ApiRequest.requestConfigUpdate() { [weak self] success in + guard let strongSelf = self else {return} } } } diff --git a/ClashX/General/ApiRequest.swift b/ClashX/General/ApiRequest.swift new file mode 100644 index 0000000..e564d77 --- /dev/null +++ b/ClashX/General/ApiRequest.swift @@ -0,0 +1,39 @@ +// +// ApiRequest.swift +// ClashX +// +// Created by CYC on 2018/7/30. +// Copyright © 2018年 west2online. All rights reserved. +// + +import Cocoa +import Alamofire + + + +class ApiRequest{ + static func requestConfig(completeHandler:@escaping ((ClashConfig)->())){ + request(ConfigManager.apiUrl + "/configs", method: .get).responseData{ + res in + guard let data = res.result.value else {return} + let config = ClashConfig.fromData(data) + completeHandler(config) + } + } + + static func requestTrafficInfo(callback:@escaping ((Int,Int)->()) ){ + 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) + } + } + } + + static func requestConfigUpdate(callback:@escaping ((Bool)->())){ + request(ConfigManager.apiUrl + "/configs", method: .put).responseJSON{ + data in + // response is undocumented yet. + callback(true) + } + } +} diff --git a/ClashX/Managers/ConfigManager.swift b/ClashX/Managers/ConfigManager.swift index 9584424..e0d2731 100644 --- a/ClashX/Managers/ConfigManager.swift +++ b/ClashX/Managers/ConfigManager.swift @@ -41,4 +41,10 @@ class ConfigManager { instance._socksProxyPort = newValue } } + + static var apiUrl:String{ + get { + return "http://127.0.0.1:8080" + } + } } diff --git a/ClashX/Models/ClashConfig.swift b/ClashX/Models/ClashConfig.swift new file mode 100644 index 0000000..0e9385d --- /dev/null +++ b/ClashX/Models/ClashConfig.swift @@ -0,0 +1,26 @@ +// +// ClashConfig.swift +// ClashX +// +// Created by CYC on 2018/7/30. +// Copyright © 2018年 west2online. All rights reserved. +// +import Foundation + +class ClashConfig:Codable { + let port:Int + let socketPort:Int + let allowLan:Bool + let mode:String + let logLevel:String + + private enum CodingKeys : String, CodingKey { + case port, socketPort = "socket-port", allowLan = "allow-lan", mode, logLevel = "log-level" + } + + static func fromData(_ data:Data)->ClashConfig{ + let decoder = JSONDecoder() + let model = try? decoder.decode(ClashConfig.self, from: data) + return model! + } +} diff --git a/ClashX/ViewControllers/SampleConfigViewController.swift b/ClashX/ViewControllers/SampleConfigViewController.swift index 1ed7526..fe79d53 100644 --- a/ClashX/ViewControllers/SampleConfigViewController.swift +++ b/ClashX/ViewControllers/SampleConfigViewController.swift @@ -94,7 +94,7 @@ class SampleConfigViewController: NSViewController { generateConfig() self.view.window?.windowController?.close() DispatchQueue(label: "com.w2fzu.ssqueue", attributes: .concurrent).async { - updateConfigC() +// updateConfigC() } } else { shakeWindows()