Monitor PAC file. Disable then enable the system pac configure after it has been modified.

This commit is contained in:
Charlie Qiu
2016-10-19 00:24:45 +08:00
parent d0ad272198
commit e5b342c134
3 changed files with 52 additions and 9 deletions

View File

@ -162,6 +162,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
updateLaunchAtLoginMenu() updateLaunchAtLoginMenu()
ProxyConfHelper.install() ProxyConfHelper.install()
ProxyConfHelper.startMonitorPAC()
applyConfig() applyConfig()
SyncSSLocal() SyncSSLocal()
} }

View File

@ -20,8 +20,6 @@
+ (void)disableProxy; + (void)disableProxy;
//+ (void)startPACServer:(NSString*) PACFilePath; + (void)startMonitorPAC;
//
//+ (void)stopPACServer;
@end @end

View File

@ -14,6 +14,7 @@
@implementation ProxyConfHelper @implementation ProxyConfHelper
GCDWebServer *webServer =nil; GCDWebServer *webServer =nil;
FSEventStreamRef fsEventStream;
+ (BOOL)isVersionOk { + (BOOL)isVersionOk {
NSTask *task; NSTask *task;
@ -114,10 +115,14 @@ GCDWebServer *webServer =nil;
} }
} }
+ (NSString*)getPACFilePath {
return [NSString stringWithFormat:@"%@/%@", NSHomeDirectory(), @".ShadowsocksX-NG/gfwlist.js"];
}
+ (void)enablePACProxy { + (void)enablePACProxy {
//start server here and then using the string next line //start server here and then using the string next line
//next two lines can open gcdwebserver and work around pac file //next two lines can open gcdwebserver and work around pac file
NSString* PACFilePath = [NSString stringWithFormat:@"%@/%@", NSHomeDirectory(), @".ShadowsocksX-NG/gfwlist.js"]; NSString* PACFilePath = [self getPACFilePath];
[self startPACServer: PACFilePath]; [self startPACServer: PACFilePath];
NSURL* url = [NSURL URLWithString: [self getHttpPACUrl]]; NSURL* url = [NSURL URLWithString: [self getHttpPACUrl]];
@ -172,22 +177,20 @@ GCDWebServer *webServer =nil;
} }
+ (void)startPACServer:(NSString*) PACFilePath { + (void)startPACServer:(NSString*) PACFilePath {
//使PAC [self stopPACServer];
NSString * routerPath = @"/proxy.pac"; NSString * routerPath = @"/proxy.pac";
[self stopPACServer]; NSData* originalPACData = [NSData dataWithContentsOfFile:PACFilePath];
webServer = [[GCDWebServer alloc] init]; webServer = [[GCDWebServer alloc] init];
[webServer addHandlerForMethod:@"GET" [webServer addHandlerForMethod:@"GET"
path:routerPath path:routerPath
requestClass:[GCDWebServerRequest class] requestClass:[GCDWebServerRequest class]
processBlock:^GCDWebServerResponse *(GCDWebServerRequest *request) processBlock:^GCDWebServerResponse *(GCDWebServerRequest *request)
{ {
NSLog(@"get proxy.pac");
NSData* originalPACData = [NSData dataWithContentsOfFile:PACFilePath];
GCDWebServerDataResponse* resp = [GCDWebServerDataResponse responseWithData:originalPACData GCDWebServerDataResponse* resp = [GCDWebServerDataResponse responseWithData:originalPACData
contentType:@"application/x-ns-proxy-autoconfig"]; contentType:@"application/x-ns-proxy-autoconfig"];
resp.cacheControlMaxAge = 0;
return resp; return resp;
} }
]; ];
@ -206,4 +209,45 @@ GCDWebServer *webServer =nil;
} }
} }
void onPACChange(
ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:@"ShadowsocksOn"]) {
if ([[defaults stringForKey:@"ShadowsocksRunningMode"] isEqualToString:@"auto"]) {
[ProxyConfHelper disableProxy];
[ProxyConfHelper enablePACProxy];
}
}
}
+ (void)startMonitorPAC {
NSString* PACFilePath = [self getPACFilePath];
if (fsEventStream) {
return;
}
CFStringRef mypath = (__bridge CFStringRef)(PACFilePath);
CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **)&mypath, 1, NULL);
void *callbackInfo = NULL; // could put stream-specific data here.
CFAbsoluteTime latency = 3.0; /* Latency in seconds */
/* Create the stream, passing in a callback */
fsEventStream = FSEventStreamCreate(NULL,
&onPACChange,
callbackInfo,
pathsToWatch,
kFSEventStreamEventIdSinceNow, /* Or a previous event ID */
latency,
kFSEventStreamCreateFlagNone /* Flags explained in reference */
);
FSEventStreamScheduleWithRunLoop(fsEventStream, [[NSRunLoop mainRunLoop] getCFRunLoop], (__bridge CFStringRef)NSDefaultRunLoopMode);
FSEventStreamStart(fsEventStream);
}
@end @end