@@ -17,6 +17,7 @@ @interface AppDelegate () {
1717 ConfigWindowController *configWindowController;
1818
1919 dispatch_queue_t taskQueue;
20+ dispatch_source_t dispatchPacSource;
2021 FSEventStreamRef fsEventStream;
2122}
2223
@@ -80,7 +81,16 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
8081 profiles = [[NSMutableArray alloc ] init ];
8182 [self readDefaults ];
8283 [self configurationDidChange ];
83- [self monitorPAC: pacDir];
84+
85+ // https://randexdev.com/2012/03/how-to-detect-directory-changes-using-gcd/
86+ int fildes = open ([pacPath cStringUsingEncoding: NSUTF8StringEncoding], O_RDONLY);
87+ dispatchPacSource = dispatch_source_create (DISPATCH_SOURCE_TYPE_VNODE, fildes, DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND, dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ));
88+ dispatch_source_set_event_handler (dispatchPacSource, ^{
89+ NSLog (@" pac changed" );
90+ [appDelegate updateSystemProxy ];
91+ });
92+ dispatch_resume (dispatchPacSource);
93+
8494 appDelegate = self;
8595}
8696
@@ -111,6 +121,8 @@ - (NSData*) pacData {
111121}
112122
113123- (void )applicationWillTerminate : (NSNotification *)aNotification {
124+ // stop monitor pac
125+ dispatch_source_cancel (dispatchPacSource);
114126 // unload v2ray
115127 runCommandLine (@" /bin/launchctl" , @[@" unload" , plistPath]);
116128 NSLog (@" V2RayX quiting, V2Ray core unloaded." );
@@ -524,47 +536,13 @@ -(void)configurationDidChange {
524536 [self updateServerMenuList ];
525537}
526538
527- - (void )monitorPAC : (NSString *)filePath {
528- if (fsEventStream) {
529- return ;
530- }
531- CFStringRef mypath = (__bridge CFStringRef)(filePath);
532- CFArrayRef pathsToWatch = CFArrayCreate (NULL , (const void **)&mypath, 1 , NULL );
533- void *callbackInfo = NULL ; // could put stream-specific data here.
534- CFAbsoluteTime latency = 3.0 ; /* Latency in seconds */
535-
536- /* Create the stream, passing in a callback */
537- fsEventStream = FSEventStreamCreate (NULL ,
538- &onPACChange,
539- callbackInfo,
540- pathsToWatch,
541- kFSEventStreamEventIdSinceNow , /* Or a previous event ID */
542- latency,
543- kFSEventStreamCreateFlagNone /* Flags explained in reference */
544- );
545- FSEventStreamScheduleWithRunLoop (fsEventStream, [[NSRunLoop mainRunLoop ] getCFRunLoop ], (__bridge CFStringRef)NSDefaultRunLoopMode );
546- FSEventStreamStart (fsEventStream);
547- }
548-
549539- (IBAction )copyExportCmd : (id )sender {
550540 [[NSPasteboard generalPasteboard ] clearContents ];
551541 NSString * command = [NSString stringWithFormat: @" export http_proxy=\" http://127.0.0.1:%ld \" ; export HTTP_PROXY=\" http://127.0.0.1:%ld \" ; export https_proxy=\" http://127.0.0.1:%ld \" ; export HTTPS_PROXY=\" http://127.0.0.1:%ld \" " , httpPort, httpPort, httpPort, httpPort];
552542 [[NSPasteboard generalPasteboard ] setString: command forType: NSStringPboardType ];
553543}
554544
555545
556- void onPACChange (
557- ConstFSEventStreamRef streamRef,
558- void *clientCallBackInfo,
559- size_t numEvents,
560- void *eventPaths,
561- const FSEventStreamEventFlags eventFlags[],
562- const FSEventStreamEventId eventIds[])
563- {
564- // NSLog(@"pac changed");
565- [appDelegate updateSystemProxy ];
566- }
567-
568546@synthesize logDirPath;
569547
570548@synthesize proxyState;
0 commit comments