From 79f5d8be5bd7b48f6233d20f51e9b663175826bc Mon Sep 17 00:00:00 2001 From: Qiu Yuzhou Date: Thu, 12 Sep 2019 01:44:44 +0800 Subject: [PATCH] Support user to add customize configure lines for privoxy. --- ShadowsocksX-NG.xcodeproj/project.pbxproj | 12 ++++++--- ShadowsocksX-NG/LaunchAgentUtils.swift | 26 +++++++++++++++---- ...config.example => privoxy.template.config} | 6 ++++- ShadowsocksX-NG/privoxy/user-privoxy.config | 3 +++ 4 files changed, 37 insertions(+), 10 deletions(-) rename ShadowsocksX-NG/privoxy/{privoxy.config.example => privoxy.template.config} (79%) create mode 100644 ShadowsocksX-NG/privoxy/user-privoxy.config diff --git a/ShadowsocksX-NG.xcodeproj/project.pbxproj b/ShadowsocksX-NG.xcodeproj/project.pbxproj index 7f92629..ae1030c 100644 --- a/ShadowsocksX-NG.xcodeproj/project.pbxproj +++ b/ShadowsocksX-NG.xcodeproj/project.pbxproj @@ -67,6 +67,7 @@ 9BAA663423295FAC00F5CC99 /* menu_icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 9BAA662423295FAB00F5CC99 /* menu_icon.png */; }; 9BAA663523295FAC00F5CC99 /* virtual-server-icon-3.png in Resources */ = {isa = PBXBuildFile; fileRef = 9BAA662523295FAB00F5CC99 /* virtual-server-icon-3.png */; }; 9BAA663623295FAC00F5CC99 /* menu_g_icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 9BAA662623295FAB00F5CC99 /* menu_g_icon.png */; }; + 9BAA66382329636200F5CC99 /* user-privoxy.config in Resources */ = {isa = PBXBuildFile; fileRef = 9BAA66372329636200F5CC99 /* user-privoxy.config */; }; 9BAFE2E21E83ED7F00F71CCE /* PreferencesWinController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9BAFE2E41E83ED7F00F71CCE /* PreferencesWinController.xib */; }; 9BB706A71D1B982300551F0E /* SWBApplication.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BB706A51D1B982300551F0E /* SWBApplication.m */; }; 9BBE7B751F508A0E00E8FFE5 /* fix_dir_owner.sh in Resources */ = {isa = PBXBuildFile; fileRef = 9BBE7B711F50790500E8FFE5 /* fix_dir_owner.sh */; }; @@ -86,7 +87,7 @@ C6D429941DA75988002A5711 /* privoxy in Resources */ = {isa = PBXBuildFile; fileRef = C6D4298F1DA75988002A5711 /* privoxy */; }; 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 */; }; + C6D429991DA76FBC002A5711 /* privoxy.template.config in Resources */ = {isa = PBXBuildFile; fileRef = C6D429981DA76FBC002A5711 /* privoxy.template.config */; }; C8E42A6C1D4F270A0074C7EA /* UserRulesController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8E42A6A1D4F270A0074C7EA /* UserRulesController.swift */; }; C8E42A6E1D4F2CAF0074C7EA /* UserRulesController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C8E42A701D4F2CAF0074C7EA /* UserRulesController.xib */; }; D8E3630B1E2072980027449B /* ServerProfileTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8E3630A1E2072980027449B /* ServerProfileTests.swift */; }; @@ -230,6 +231,7 @@ 9BAA662423295FAB00F5CC99 /* menu_icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = menu_icon.png; sourceTree = ""; }; 9BAA662523295FAB00F5CC99 /* virtual-server-icon-3.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "virtual-server-icon-3.png"; sourceTree = ""; }; 9BAA662623295FAB00F5CC99 /* menu_g_icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = menu_g_icon.png; sourceTree = ""; }; + 9BAA66372329636200F5CC99 /* user-privoxy.config */ = {isa = PBXFileReference; lastKnownFileType = text; path = "user-privoxy.config"; sourceTree = ""; }; 9BAFE2E31E83ED7F00F71CCE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/PreferencesWinController.xib; sourceTree = ""; }; 9BAFE2E91E83EDDF00F71CCE /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/PreferencesWinController.strings"; sourceTree = ""; }; 9BAFE2EB1E83F91D00F71CCE /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/MainMenu.strings"; sourceTree = ""; }; @@ -251,7 +253,7 @@ C6D4298F1DA75988002A5711 /* privoxy */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = privoxy; 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 = ""; }; + C6D429981DA76FBC002A5711 /* privoxy.template.config */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = privoxy.template.config; sourceTree = ""; }; C8E42A6A1D4F270A0074C7EA /* UserRulesController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserRulesController.swift; sourceTree = ""; }; C8E42A6F1D4F2CAF0074C7EA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/UserRulesController.xib; sourceTree = ""; }; C8E42A721D4F2CB10074C7EA /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/UserRulesController.strings"; sourceTree = ""; }; @@ -453,7 +455,8 @@ 9B5AA0AA209C42A900E8B659 /* privoxy */ = { isa = PBXGroup; children = ( - C6D429981DA76FBC002A5711 /* privoxy.config.example */, + C6D429981DA76FBC002A5711 /* privoxy.template.config */, + 9BAA66372329636200F5CC99 /* user-privoxy.config */, C6D4298F1DA75988002A5711 /* privoxy */, C6D4298E1DA75988002A5711 /* install_privoxy.sh */, C6D429911DA75988002A5711 /* start_privoxy.sh */, @@ -660,6 +663,7 @@ 9BCB1D6C20A15B0E005BABE7 /* libsodium.23.dylib in Resources */, 9BCB1D6D20A15B0E005BABE7 /* libcares.2.dylib in Resources */, 9BBE7B751F508A0E00E8FFE5 /* fix_dir_owner.sh in Resources */, + 9BAA66382329636200F5CC99 /* user-privoxy.config in Resources */, 9B3F7BFF1E82BF5B00C68B75 /* libev.4.dylib in Resources */, 9BAA662D23295FAC00F5CC99 /* icons8-Blind Filled-50.png in Resources */, 9BAA663623295FAC00F5CC99 /* menu_g_icon.png in Resources */, @@ -683,7 +687,7 @@ 9B3546731E802B1200B510B4 /* ToastWindowController.xib in Resources */, 9BAA662923295FAB00F5CC99 /* menu_p_icon.png in Resources */, C6D429941DA75988002A5711 /* privoxy in Resources */, - C6D429991DA76FBC002A5711 /* privoxy.config.example in Resources */, + C6D429991DA76FBC002A5711 /* privoxy.template.config in Resources */, 9BEEF06B1D04D4D500FC52B3 /* stop_ss_local.sh in Resources */, 9B3FFF341D08CEF70019A709 /* SWBQRCodeWindowController.xib in Resources */, 9B3FFF231D088E8D0019A709 /* abp.js in Resources */, diff --git a/ShadowsocksX-NG/LaunchAgentUtils.swift b/ShadowsocksX-NG/LaunchAgentUtils.swift index b71a556..0ea46bd 100644 --- a/ShadowsocksX-NG/LaunchAgentUtils.swift +++ b/ShadowsocksX-NG/LaunchAgentUtils.swift @@ -14,6 +14,7 @@ let V2RAY_PLUGIN_VERSION = "1.1.0" let PRIVOXY_VERSION = "3.0.26.static" let SIMPLE_OBFS_VERSION = "0.0.5_1" let APP_SUPPORT_DIR = "/Library/Application Support/ShadowsocksX-NG/" +let USER_CONFIG_DIR = "/.ShadowsocksX-NG/" let LAUNCH_AGENT_DIR = "/Library/LaunchAgents/" let LAUNCH_AGENT_CONF_SSLOCAL_NAME = "com.qiuyuzhou.shadowsocksX-NG.local.plist" let LAUNCH_AGENT_CONF_PRIVOXY_NAME = "com.qiuyuzhou.shadowsocksX-NG.http.plist" @@ -333,18 +334,33 @@ func InstallPrivoxy() { NSLog("Install privoxy failed.") } } + + let userConfigPath = homeDir + USER_CONFIG_DIR + "user-privoxy.config" + if !fileMgr.fileExists(atPath: userConfigPath) { + let srcPath = Bundle.main.path(forResource: "user-privoxy", ofType: "config")! + try! fileMgr.copyItem(atPath: srcPath, toPath: userConfigPath) + } } func writePrivoxyConfFile() -> Bool { do { let defaults = UserDefaults.standard let bundle = Bundle.main - let examplePath = bundle.path(forResource: "privoxy.config.example", ofType: nil) - var example = try String(contentsOfFile: examplePath!, encoding: .utf8) - example = example.replacingOccurrences(of: "{http}", with: defaults.string(forKey: "LocalHTTP.ListenAddress")! + ":" + String(defaults.integer(forKey: "LocalHTTP.ListenPort"))) - example = example.replacingOccurrences(of: "{socks5}", with: defaults.string(forKey: "LocalSocks5.ListenAddress")! + ":" + String(defaults.integer(forKey: "LocalSocks5.ListenPort"))) - let data = example.data(using: .utf8) + let templatePath = bundle.path(forResource: "privoxy.template.config", ofType: nil) + // Read template file + var template = try String(contentsOfFile: templatePath!, encoding: .utf8) + + template = template.replacingOccurrences(of: "{http}", with: defaults.string(forKey: "LocalHTTP.ListenAddress")! + ":" + String(defaults.integer(forKey: "LocalHTTP.ListenPort"))) + template = template.replacingOccurrences(of: "{socks5}", with: defaults.string(forKey: "LocalSocks5.ListenAddress")! + ":" + String(defaults.integer(forKey: "LocalSocks5.ListenPort"))) + + // Append the user config file to the end + let userConfigPath = NSHomeDirectory() + USER_CONFIG_DIR + "user-privoxy.config" + let userConfig = try String(contentsOfFile: userConfigPath, encoding: .utf8) + template.append(contentsOf: userConfig) + + // Write to file + let data = template.data(using: .utf8) let filepath = NSHomeDirectory() + APP_SUPPORT_DIR + "privoxy.config" let oldSum = getFileSHA1Sum(filepath) diff --git a/ShadowsocksX-NG/privoxy/privoxy.config.example b/ShadowsocksX-NG/privoxy/privoxy.template.config similarity index 79% rename from ShadowsocksX-NG/privoxy/privoxy.config.example rename to ShadowsocksX-NG/privoxy/privoxy.template.config index 41fa850..ec10bdd 100644 --- a/ShadowsocksX-NG/privoxy/privoxy.config.example +++ b/ShadowsocksX-NG/privoxy/privoxy.template.config @@ -12,7 +12,11 @@ split-large-forms 0 keep-alive-timeout 5 socket-timeout 60 -forward-socks5 / {socks5} . forward 192.168.*.*/ . forward 10.*.*.*/ . forward 127.*.*.*/ . +forward [FE80::/64] . +forward [::1] . +forward [FD00::/8] . +forward-socks5 / {socks5} . + diff --git a/ShadowsocksX-NG/privoxy/user-privoxy.config b/ShadowsocksX-NG/privoxy/user-privoxy.config new file mode 100644 index 0000000..6507104 --- /dev/null +++ b/ShadowsocksX-NG/privoxy/user-privoxy.config @@ -0,0 +1,3 @@ +# Put user privoxy config line in this file. +# Ref: https://www.privoxy.org/user-manual/index.html +