From 59f914febeb8a28477327a84e98169a56aef3ebb Mon Sep 17 00:00:00 2001 From: Charlie Qiu Date: Mon, 16 Jan 2017 14:47:40 +0800 Subject: [PATCH] Refactor launch agent utils. --- ShadowsocksX-NG.xcodeproj/project.pbxproj | 12 ---- ShadowsocksX-NG/AppDelegate.swift | 5 +- ShadowsocksX-NG/Info.plist | 2 +- ShadowsocksX-NG/LaunchAgentUtils.swift | 72 +++++++---------------- ShadowsocksX-NG/reload_conf_kcptun.sh | 10 ---- ShadowsocksX-NG/reload_conf_privoxy.sh | 12 ---- ShadowsocksX-NG/reload_conf_ss_local.sh | 12 ---- 7 files changed, 27 insertions(+), 98 deletions(-) delete mode 100755 ShadowsocksX-NG/reload_conf_kcptun.sh delete mode 100755 ShadowsocksX-NG/reload_conf_privoxy.sh delete mode 100755 ShadowsocksX-NG/reload_conf_ss_local.sh diff --git a/ShadowsocksX-NG.xcodeproj/project.pbxproj b/ShadowsocksX-NG.xcodeproj/project.pbxproj index 9980e9d..dd00469 100755 --- a/ShadowsocksX-NG.xcodeproj/project.pbxproj +++ b/ShadowsocksX-NG.xcodeproj/project.pbxproj @@ -42,7 +42,6 @@ 9B9CBCA81E26312E00FC61AA /* install_kcptun.sh in Resources */ = {isa = PBXBuildFile; fileRef = 9B9CBCA71E26310E00FC61AA /* install_kcptun.sh */; }; 9B9CBCAA1E2633CB00FC61AA /* kcptun_client in Resources */ = {isa = PBXBuildFile; fileRef = 9B9CBCA91E2633CB00FC61AA /* kcptun_client */; }; 9B9CBCAF1E263B1600FC61AA /* libpcre.1.dylib in Resources */ = {isa = PBXBuildFile; fileRef = 9B9CBCAD1E263A6600FC61AA /* libpcre.1.dylib */; }; - 9B9CBCB31E26452500FC61AA /* reload_conf_kcptun.sh in Resources */ = {isa = PBXBuildFile; fileRef = 9B9CBCB21E26452500FC61AA /* reload_conf_kcptun.sh */; }; 9B9CBCB41E26453C00FC61AA /* start_kcptun.sh in Resources */ = {isa = PBXBuildFile; fileRef = 9B9CBCB01E2644DC00FC61AA /* start_kcptun.sh */; }; 9B9CBCB51E26453C00FC61AA /* stop_kcptun.sh in Resources */ = {isa = PBXBuildFile; fileRef = 9B9CBCB11E26450D00FC61AA /* stop_kcptun.sh */; }; 9BA04B231D23D5A5005AAD7F /* ProxyConfTool.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BA04B221D23D5A5005AAD7F /* ProxyConfTool.m */; }; @@ -53,7 +52,6 @@ 9BEEF0691D04D4D500FC52B3 /* install_ss_local.sh in Resources */ = {isa = PBXBuildFile; fileRef = 9BEEF0651D04CB8500FC52B3 /* install_ss_local.sh */; }; 9BEEF06A1D04D4D500FC52B3 /* start_ss_local.sh in Resources */ = {isa = PBXBuildFile; fileRef = 9BEEF0661D04CE8D00FC52B3 /* start_ss_local.sh */; }; 9BEEF06B1D04D4D500FC52B3 /* stop_ss_local.sh in Resources */ = {isa = PBXBuildFile; fileRef = 9BEEF0671D04CE9A00FC52B3 /* stop_ss_local.sh */; }; - 9BEEF06C1D04D4D500FC52B3 /* reload_conf_ss_local.sh in Resources */ = {isa = PBXBuildFile; fileRef = 9BEEF0681D04CFE500FC52B3 /* reload_conf_ss_local.sh */; }; 9BEEF06E1D04DCE400FC52B3 /* ServerProfile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BEEF06D1D04DCE400FC52B3 /* ServerProfile.swift */; }; 9BEEF0701D04DDB100FC52B3 /* ServerProfileManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BEEF06F1D04DDB100FC52B3 /* ServerProfileManager.swift */; }; 9BEEF0751D04EF3E00FC52B3 /* PreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BEEF0731D04EF3E00FC52B3 /* PreferencesWindowController.swift */; }; @@ -61,7 +59,6 @@ 9BEEF07B1D05631500FC52B3 /* AdvPreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BEEF0791D05631500FC52B3 /* AdvPreferencesWindowController.swift */; }; C6D429931DA75988002A5711 /* install_privoxy.sh in Resources */ = {isa = PBXBuildFile; fileRef = C6D4298E1DA75988002A5711 /* install_privoxy.sh */; }; C6D429941DA75988002A5711 /* privoxy in Resources */ = {isa = PBXBuildFile; fileRef = C6D4298F1DA75988002A5711 /* privoxy */; }; - C6D429951DA75988002A5711 /* reload_conf_privoxy.sh in Resources */ = {isa = PBXBuildFile; fileRef = C6D429901DA75988002A5711 /* reload_conf_privoxy.sh */; }; C6D429961DA75988002A5711 /* start_privoxy.sh in Resources */ = {isa = PBXBuildFile; fileRef = C6D429911DA75988002A5711 /* start_privoxy.sh */; }; C6D429971DA75988002A5711 /* stop_privoxy.sh in Resources */ = {isa = PBXBuildFile; fileRef = C6D429921DA75988002A5711 /* stop_privoxy.sh */; }; C6D429991DA76FBC002A5711 /* privoxy.config.example in Resources */ = {isa = PBXBuildFile; fileRef = C6D429981DA76FBC002A5711 /* privoxy.config.example */; }; @@ -182,7 +179,6 @@ 9B9CBCAD1E263A6600FC61AA /* libpcre.1.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; path = libpcre.1.dylib; sourceTree = ""; }; 9B9CBCB01E2644DC00FC61AA /* start_kcptun.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = start_kcptun.sh; sourceTree = ""; }; 9B9CBCB11E26450D00FC61AA /* stop_kcptun.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = stop_kcptun.sh; sourceTree = ""; }; - 9B9CBCB21E26452500FC61AA /* reload_conf_kcptun.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = reload_conf_kcptun.sh; sourceTree = ""; }; 9BA04B211D23D5A5005AAD7F /* ProxyConfTool.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ProxyConfTool.h; sourceTree = ""; }; 9BA04B221D23D5A5005AAD7F /* ProxyConfTool.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ProxyConfTool.m; sourceTree = ""; }; 9BA04B241D24044D005AAD7F /* ProxyPreferencesController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProxyPreferencesController.swift; sourceTree = ""; }; @@ -193,7 +189,6 @@ 9BEEF0651D04CB8500FC52B3 /* install_ss_local.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = install_ss_local.sh; sourceTree = ""; }; 9BEEF0661D04CE8D00FC52B3 /* start_ss_local.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = start_ss_local.sh; sourceTree = ""; }; 9BEEF0671D04CE9A00FC52B3 /* stop_ss_local.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = stop_ss_local.sh; sourceTree = ""; }; - 9BEEF0681D04CFE500FC52B3 /* reload_conf_ss_local.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = reload_conf_ss_local.sh; sourceTree = ""; }; 9BEEF06D1D04DCE400FC52B3 /* ServerProfile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServerProfile.swift; sourceTree = ""; }; 9BEEF06F1D04DDB100FC52B3 /* ServerProfileManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServerProfileManager.swift; sourceTree = ""; }; 9BEEF0731D04EF3E00FC52B3 /* PreferencesWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesWindowController.swift; sourceTree = ""; }; @@ -202,7 +197,6 @@ B4E6A97CA843F3943524B686 /* Pods-proxy_conf_helper.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-proxy_conf_helper.debug.xcconfig"; path = "Pods/Target Support Files/Pods-proxy_conf_helper/Pods-proxy_conf_helper.debug.xcconfig"; sourceTree = ""; }; C6D4298E1DA75988002A5711 /* install_privoxy.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = install_privoxy.sh; sourceTree = ""; }; C6D4298F1DA75988002A5711 /* privoxy */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = privoxy; sourceTree = ""; }; - C6D429901DA75988002A5711 /* reload_conf_privoxy.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = reload_conf_privoxy.sh; sourceTree = ""; }; C6D429911DA75988002A5711 /* start_privoxy.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = start_privoxy.sh; sourceTree = ""; }; C6D429921DA75988002A5711 /* stop_privoxy.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = stop_privoxy.sh; sourceTree = ""; }; C6D429981DA76FBC002A5711 /* privoxy.config.example */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = privoxy.config.example; sourceTree = ""; }; @@ -271,7 +265,6 @@ 08FCA0FE1E24BE1A0070984F /* example-gui-config.json */, C6D4298E1DA75988002A5711 /* install_privoxy.sh */, C6D4298F1DA75988002A5711 /* privoxy */, - C6D429901DA75988002A5711 /* reload_conf_privoxy.sh */, C6D429911DA75988002A5711 /* start_privoxy.sh */, C6D429921DA75988002A5711 /* stop_privoxy.sh */, 9BE8FBBD1D0B1FB900CAFD01 /* libcrypto.1.0.0.dylib */, @@ -289,11 +282,9 @@ 9B9CBCA71E26310E00FC61AA /* install_kcptun.sh */, 9BEEF0661D04CE8D00FC52B3 /* start_ss_local.sh */, 9BEEF0671D04CE9A00FC52B3 /* stop_ss_local.sh */, - 9BEEF0681D04CFE500FC52B3 /* reload_conf_ss_local.sh */, C6D429981DA76FBC002A5711 /* privoxy.config.example */, 9B9CBCB01E2644DC00FC61AA /* start_kcptun.sh */, 9B9CBCB11E26450D00FC61AA /* stop_kcptun.sh */, - 9B9CBCB21E26452500FC61AA /* reload_conf_kcptun.sh */, ); name = "Support Files"; sourceTree = ""; @@ -564,16 +555,13 @@ C6D429991DA76FBC002A5711 /* privoxy.config.example in Resources */, 9BEEF06B1D04D4D500FC52B3 /* stop_ss_local.sh in Resources */, 9B3FFF341D08CEF70019A709 /* SWBQRCodeWindowController.xib in Resources */, - 9BEEF06C1D04D4D500FC52B3 /* reload_conf_ss_local.sh in Resources */, 9B3FFF231D088E8D0019A709 /* abp.js in Resources */, 9B07EFAD1D048E880052D9DF /* menu_icon.png in Resources */, - C6D429951DA75988002A5711 /* reload_conf_privoxy.sh in Resources */, 9B0BFFEB1D0460A70040E62B /* Assets.xcassets in Resources */, 9B2491B71D0ACC3E003BBECC /* AdvPreferencesWindowController.xib in Resources */, 08FCA0FF1E24BE1A0070984F /* example-gui-config.json in Resources */, 9B3FFF271D0898EB0019A709 /* gfwlist.txt in Resources */, C6D429931DA75988002A5711 /* install_privoxy.sh in Resources */, - 9B9CBCB31E26452500FC61AA /* reload_conf_kcptun.sh in Resources */, 9BC70EDC1D2E3E3100EDA4CA /* Localizable.strings in Resources */, 9B0BFFEE1D0460A70040E62B /* MainMenu.xib in Resources */, 9B3FFF4C1D09D8F70019A709 /* install_helper.sh in Resources */, diff --git a/ShadowsocksX-NG/AppDelegate.swift b/ShadowsocksX-NG/AppDelegate.swift index 6e9c19a..91bb249 100755 --- a/ShadowsocksX-NG/AppDelegate.swift +++ b/ShadowsocksX-NG/AppDelegate.swift @@ -201,8 +201,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele ProxyConfHelper.install() ProxyConfHelper.startMonitorPAC() - applyConfig() SyncSSLocal() + applyConfig() // Register global hotkey registerHotkey() @@ -211,6 +211,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele func applicationWillTerminate(_ aNotification: Notification) { // Insert code here to tear down your application StopSSLocal() + StopKcptun() StopPrivoxy() ProxyConfHelper.disableProxy() if let ref = hotKeyRef { UnregisterEventHotKey(ref) } @@ -223,6 +224,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele if isOn { StartSSLocal() + StartKcptun() StartPrivoxy() if mode == "auto" { ProxyConfHelper.enablePACProxy() @@ -233,6 +235,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele } } else { StopSSLocal() + StopKcptun() StopPrivoxy() ProxyConfHelper.disableProxy() } diff --git a/ShadowsocksX-NG/Info.plist b/ShadowsocksX-NG/Info.plist index 79f2386..faeda9d 100644 --- a/ShadowsocksX-NG/Info.plist +++ b/ShadowsocksX-NG/Info.plist @@ -36,7 +36,7 @@ CFBundleVersion - 1 + 2 LSApplicationCategoryType public.app-category.utilities LSMinimumSystemVersion diff --git a/ShadowsocksX-NG/LaunchAgentUtils.swift b/ShadowsocksX-NG/LaunchAgentUtils.swift index c39dff3..2dca183 100644 --- a/ShadowsocksX-NG/LaunchAgentUtils.swift +++ b/ShadowsocksX-NG/LaunchAgentUtils.swift @@ -75,18 +75,6 @@ func generateSSLocalLauchAgentPlist() -> Bool { } } -func ReloadConfSSLocal() { - let bundle = Bundle.main - let installerPath = bundle.path(forResource: "reload_conf_ss_local.sh", ofType: nil) - let task = Process.launchedProcess(launchPath: installerPath!, arguments: [""]) - task.waitUntilExit() - if task.terminationStatus == 0 { - NSLog("Start ss-local succeeded.") - } else { - NSLog("Start ss-local failed.") - } -} - func StartSSLocal() { let bundle = Bundle.main let installerPath = bundle.path(forResource: "start_ss_local.sh", ofType: nil) @@ -168,8 +156,8 @@ func SyncSSLocal() { let on = UserDefaults.standard.bool(forKey: "ShadowsocksOn") if on { + StopSSLocal() StartSSLocal() - ReloadConfSSLocal() } } else { removeSSLocalConfFile() @@ -217,19 +205,6 @@ func generatePrivoxyLauchAgentPlist() -> Bool { } } - -func ReloadConfPrivoxy() { - let bundle = Bundle.main - let installerPath = bundle.path(forResource: "reload_conf_privoxy.sh", ofType: nil) - let task = Process.launchedProcess(launchPath: installerPath!, arguments: [""]) - task.waitUntilExit() - if task.terminationStatus == 0 { - NSLog("reload privoxy succeeded.") - } else { - NSLog("reload privoxy failed.") - } -} - func StartPrivoxy() { let bundle = Bundle.main let installerPath = bundle.path(forResource: "start_privoxy.sh", ofType: nil) @@ -316,8 +291,8 @@ func SyncPrivoxy() { let on = UserDefaults.standard.bool(forKey: "LocalHTTPOn") if on { -// StartPrivoxy() - ReloadConfPrivoxy() + StopPrivoxy() + StartPrivoxy() } else { removePrivoxyConfFile() @@ -401,6 +376,14 @@ func writeKcptunConfFile(_ conf:[String:AnyObject]) -> Bool { return false } +func isEnabledKcptun() -> Bool { + let mgr = ServerProfileManager.instance + if let profile = mgr.getActiveProfile() { + return profile.enabledKcptun + } + return false +} + func removeKcptunConfFile() { do { let filepath = NSHomeDirectory() + APP_SUPPORT_DIR + "kcptun-config.json" @@ -410,28 +393,17 @@ func removeKcptunConfFile() { } } - -func ReloadConfKcptun() { - let bundle = Bundle.main - let installerPath = bundle.path(forResource: "reload_conf_kcptun.sh", ofType: nil) - let task = Process.launchedProcess(launchPath: installerPath!, arguments: [""]) - task.waitUntilExit() - if task.terminationStatus == 0 { - NSLog("Start kcptun succeeded.") - } else { - NSLog("Start kcptun failed.") - } -} - func StartKcptun() { - let bundle = Bundle.main - let installerPath = bundle.path(forResource: "start_kcptun.sh", ofType: nil) - let task = Process.launchedProcess(launchPath: installerPath!, arguments: [""]) - task.waitUntilExit() - if task.terminationStatus == 0 { - NSLog("Start kcptun succeeded.") - } else { - NSLog("Start kcptun failed.") + if isEnabledKcptun() { + let bundle = Bundle.main + let installerPath = bundle.path(forResource: "start_kcptun.sh", ofType: nil) + let task = Process.launchedProcess(launchPath: installerPath!, arguments: [""]) + task.waitUntilExit() + if task.terminationStatus == 0 { + NSLog("Start kcptun succeeded.") + } else { + NSLog("Start kcptun failed.") + } } } @@ -457,8 +429,8 @@ func SyncKcptun() { let on = UserDefaults.standard.bool(forKey: "ShadowsocksOn") if on { + StopKcptun() StartKcptun() - ReloadConfKcptun() return } } diff --git a/ShadowsocksX-NG/reload_conf_kcptun.sh b/ShadowsocksX-NG/reload_conf_kcptun.sh deleted file mode 100755 index 183d135..0000000 --- a/ShadowsocksX-NG/reload_conf_kcptun.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh - -# reload_conf_kcptun.sh -# ShadowsocksX-NG -# -# Created by 邱宇舟 on 2017/1/11. -# Copyright © 2017年 qiuyuzhou. All rights reserved. - -launchctl unload "$HOME/Library/LaunchAgents/com.qiuyuzhou.shadowsocksX-NG.kcptun.plist" -launchctl load "$HOME/Library/LaunchAgents/com.qiuyuzhou.shadowsocksX-NG.kcptun.plist" diff --git a/ShadowsocksX-NG/reload_conf_privoxy.sh b/ShadowsocksX-NG/reload_conf_privoxy.sh deleted file mode 100755 index f8e435c..0000000 --- a/ShadowsocksX-NG/reload_conf_privoxy.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh - -# reload_privoxy.sh -# ShadowsocksX-NG -# -# Created by 王晨 on 16/10/7. -# Copyright © 2016年 zhfish. All rights reserved. - -#launchctl kill SIGHUP "$HOME/Library/LaunchAgents/com.qiuyuzhou.shadowsocksX-NG.http.plist" - -launchctl unload "$HOME/Library/LaunchAgents/com.qiuyuzhou.shadowsocksX-NG.http.plist" -launchctl load "$HOME/Library/LaunchAgents/com.qiuyuzhou.shadowsocksX-NG.http.plist" diff --git a/ShadowsocksX-NG/reload_conf_ss_local.sh b/ShadowsocksX-NG/reload_conf_ss_local.sh deleted file mode 100755 index c96f790..0000000 --- a/ShadowsocksX-NG/reload_conf_ss_local.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh - -# reload_conf_ss_local.sh -# ShadowsocksX-NG -# -# Created by 邱宇舟 on 16/6/6. -# Copyright © 2016年 qiuyuzhou. All rights reserved. - -#launchctl kill SIGHUP "$HOME/Library/LaunchAgents/com.qiuyuzhou.shadowsocksX-NG.local.plist" - -launchctl unload "$HOME/Library/LaunchAgents/com.qiuyuzhou.shadowsocksX-NG.local.plist" -launchctl load "$HOME/Library/LaunchAgents/com.qiuyuzhou.shadowsocksX-NG.local.plist"