From 245770afcf0f0e0edfebb48251271dcf95237621 Mon Sep 17 00:00:00 2001 From: Charlie Qiu Date: Thu, 4 Aug 2016 00:41:00 +0800 Subject: [PATCH] Fix bug #24 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 避免清空不是由ShadowsocksX-NG设置代理配置。 --- ShadowsocksX-NG/ProxyConfHelper.m | 10 +++++- ShadowsocksX-NG/proxy_conf_helper_version.h | 2 +- proxy_conf_helper/main.m | 39 +++++++++++++++++---- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/ShadowsocksX-NG/ProxyConfHelper.m b/ShadowsocksX-NG/ProxyConfHelper.m index 282bbc7..d18303d 100644 --- a/ShadowsocksX-NG/ProxyConfHelper.m +++ b/ShadowsocksX-NG/ProxyConfHelper.m @@ -136,7 +136,15 @@ } + (void)disableProxy { - NSMutableArray* args = [@[@"--mode", @"off"]mutableCopy]; + // 带上所有参数是为了判断是否原有代理设置是否由ssx-ng设置的。如果是用户手工设置的其他配置,则不进行清空。 + NSString* urlString = [NSString stringWithFormat:@"%@/.ShadowsocksX-NG/gfwlist.js", NSHomeDirectory()]; + NSURL* url = [NSURL fileURLWithPath:urlString]; + NSUInteger port = [[NSUserDefaults standardUserDefaults]integerForKey:@"LocalSocks5.ListenPort"]; + + NSMutableArray* args = [@[@"--mode", @"off" + , @"--port", [NSString stringWithFormat:@"%lu", (unsigned long)port] + , @"--pac-url", [url absoluteString] + ]mutableCopy]; [self addArguments4ManualSpecifyNetworkServices:args]; [self callHelper:args]; } diff --git a/ShadowsocksX-NG/proxy_conf_helper_version.h b/ShadowsocksX-NG/proxy_conf_helper_version.h index 95c2bbc..da95803 100644 --- a/ShadowsocksX-NG/proxy_conf_helper_version.h +++ b/ShadowsocksX-NG/proxy_conf_helper_version.h @@ -9,6 +9,6 @@ #ifndef proxy_conf_helper_version_h #define proxy_conf_helper_version_h -#define kProxyConfHelperVersion @"1.2.0" +#define kProxyConfHelperVersion @"1.3.1" #endif /* proxy_conf_helper_version_h */ diff --git a/proxy_conf_helper/main.m b/proxy_conf_helper/main.m index 600cfe2..039846b 100644 --- a/proxy_conf_helper/main.m +++ b/proxy_conf_helper/main.m @@ -67,10 +67,6 @@ int main(int argc, const char * argv[]) if (!portString) { return 1; } - port = [portString integerValue]; - if (0 == port) { - return 1; - } } else if (![@"off" isEqualToString:mode]) { return 1; } @@ -79,6 +75,12 @@ int main(int argc, const char * argv[]) return 0; } + if (portString) { + port = [portString integerValue]; + if (0 == port) { + return 1; + } + } static AuthorizationRef authRef; static AuthorizationFlags authFlags; @@ -126,11 +128,16 @@ int main(int argc, const char * argv[]) if (modify) { + NSString* prefPath = [NSString stringWithFormat:@"/%@/%@/%@", kSCPrefNetworkServices + , key, kSCEntNetProxies]; + if ([mode isEqualToString:@"auto"]) { [proxies setObject:pacURL forKey:(NSString *)kCFNetworkProxiesProxyAutoConfigURLString]; [proxies setObject:[NSNumber numberWithInt:1] forKey:(NSString *)kCFNetworkProxiesProxyAutoConfigEnable]; + SCPreferencesPathSetValue(prefRef, (__bridge CFStringRef)prefPath + , (__bridge CFDictionaryRef)proxies); } else if ([mode isEqualToString:@"global"]) { @@ -142,9 +149,29 @@ int main(int argc, const char * argv[]) kCFNetworkProxiesSOCKSEnable]; [proxies setObject:@[@"127.0.0.1", @"localhost"] forKey:(NSString *)kCFNetworkProxiesExceptionsList]; + SCPreferencesPathSetValue(prefRef, (__bridge CFStringRef)prefPath + , (__bridge CFDictionaryRef)proxies); + } else if ([mode isEqualToString:@"off"]) { + if (pacURL != nil && portString != nil) { + // 取原来的配置,判断是否为shadowsocksX-NG设置的 + NSDictionary* oldProxies + = (__bridge NSDictionary*)SCPreferencesPathGetValue(prefRef + , (__bridge CFStringRef)prefPath); + + if (([oldProxies[(NSString *)kCFNetworkProxiesProxyAutoConfigURLString] isEqualToString:pacURL] + &&[oldProxies[(NSString *)kCFNetworkProxiesProxyAutoConfigEnable] isEqual:[NSNumber numberWithInt:1]]) + ||([oldProxies[(NSString*)kCFNetworkProxiesSOCKSProxy] isEqualToString:@"127.0.0.1"] + &&[oldProxies[(NSString*)kCFNetworkProxiesSOCKSPort] isEqualTo:[NSNumber numberWithInteger:port]] + &&[oldProxies[(NSString*)kCFNetworkProxiesSOCKSEnable] isEqual:[NSNumber numberWithInt:1]]) + ) { + SCPreferencesPathSetValue(prefRef, (__bridge CFStringRef)prefPath + , (__bridge CFDictionaryRef)proxies); + } + } else { + SCPreferencesPathSetValue(prefRef, (__bridge CFStringRef)prefPath + , (__bridge CFDictionaryRef)proxies); + } } - - SCPreferencesPathSetValue(prefRef, (__bridge CFStringRef)[NSString stringWithFormat:@"/%@/%@/%@", kSCPrefNetworkServices, key, kSCEntNetProxies], (__bridge CFDictionaryRef)proxies); } }