@@ -17,6 +17,7 @@ @interface AppDelegate () {
17
17
ConfigWindowController *configWindowController;
18
18
19
19
dispatch_queue_t taskQueue;
20
+ dispatch_source_t dispatchPacSource;
20
21
FSEventStreamRef fsEventStream;
21
22
}
22
23
@@ -80,7 +81,16 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
80
81
profiles = [[NSMutableArray alloc ] init ];
81
82
[self readDefaults ];
82
83
[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
+
84
94
appDelegate = self;
85
95
}
86
96
@@ -111,6 +121,8 @@ - (NSData*) pacData {
111
121
}
112
122
113
123
- (void )applicationWillTerminate : (NSNotification *)aNotification {
124
+ // stop monitor pac
125
+ dispatch_source_cancel (dispatchPacSource);
114
126
// unload v2ray
115
127
runCommandLine (@" /bin/launchctl" , @[@" unload" , plistPath]);
116
128
NSLog (@" V2RayX quiting, V2Ray core unloaded." );
@@ -524,47 +536,13 @@ -(void)configurationDidChange {
524
536
[self updateServerMenuList ];
525
537
}
526
538
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
-
549
539
- (IBAction )copyExportCmd : (id )sender {
550
540
[[NSPasteboard generalPasteboard ] clearContents ];
551
541
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];
552
542
[[NSPasteboard generalPasteboard ] setString: command forType: NSStringPboardType ];
553
543
}
554
544
555
545
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
-
568
546
@synthesize logDirPath;
569
547
570
548
@synthesize proxyState;
0 commit comments