Last active
April 13, 2023 02:59
-
-
Save nicoster/1b6dd6057adb7d19ebaaefceca933685 to your computer and use it in GitHub Desktop.
invoke a script when wifi connection changes. it will hold the notification for 5 seconds and coalesce repeating notifications.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| #include <CoreFoundation/CoreFoundation.h> | |
| void onNotifyCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { | |
| NSString* notifyName = (NSString*)name; | |
| if ([notifyName isEqualToString:@"com.apple.system.config.network_change"]) { | |
| // Get the path to the shell script from the command line | |
| NSArray *arguments = [[NSProcessInfo processInfo] arguments]; | |
| if (arguments.count > 1) { | |
| NSString *shellScriptPath = [arguments objectAtIndex:1]; | |
| // Cancel any previous timer and start a new one | |
| static NSTimer *timer = nil; | |
| // printf("timer:%p\n", timer); | |
| [timer invalidate]; | |
| timer = [NSTimer scheduledTimerWithTimeInterval:5.0 repeats:NO block:^(NSTimer *) { | |
| timer = nil; | |
| // Run the shell command | |
| system([shellScriptPath UTF8String]); | |
| NSLog(@"Shell script executed."); | |
| }]; | |
| // printf("timer after:%p\n", timer); | |
| } else { | |
| NSLog(@"No shell script path specified."); | |
| } | |
| } else { | |
| NSLog(@"Intercepted %@", notifyName); | |
| } | |
| } | |
| int main(int argc, const char * argv[]) { | |
| @autoreleasepool { | |
| // Add observer for network change notifications | |
| CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), | |
| NULL, | |
| onNotifyCallback, | |
| CFSTR("com.apple.system.config.network_change"), | |
| NULL, | |
| CFNotificationSuspensionBehaviorDeliverImmediately); | |
| // Run the main loop to keep the program running | |
| NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; | |
| [runLoop run]; | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| TMPFILE=/tmp/en0-ip | |
| IPCMD=$(ifconfig en0 | grep inet\ ) | |
| # Compare with previous IP address | |
| diff <(echo "$IPCMD") $TMPFILE 2> /dev/null | |
| # Check the exit status of the diff command | |
| if [ $? -eq 0 ]; then | |
| echo "IP address has not changed." | |
| else | |
| # Save current IP address to a file | |
| echo "$IPCMD" > $TMPFILE | |
| pkill wireproxy | |
| cd ~/Documents/wireguard || exit | |
| ~/bin/wireproxy -d -c wireproxy.conf && ps aux | grep -v grep | grep wireproxy | |
| fi | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" | |
| "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
| <plist version="1.0"> | |
| <dict> | |
| <key>Label</key> | |
| <string>wireproxy</string> | |
| <key>KeepAlive</key> | |
| <true/> | |
| <key>StandardErrorPath</key> | |
| <string>/Users/nickxiao/Library/Logs/wireproxy/wireproxy-stderr.log</string> | |
| <key>StandardOutPath</key> | |
| <string>/Users/nickxiao/Library/Logs/wireproxy/wireproxy-stdout.log</string> | |
| <key>EnvironmentVariables</key> | |
| <dict> | |
| <key>PATH</key> | |
| <string>/Users/nickxiao/.pyenv/shims:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/nickxiao/.local/bin:/usr/local/sbin:/usr/local/openresty/nginx/sbin:/usr/local/opt/llvm/bin:/Users/nickr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string> | |
| </dict> | |
| <key>ProgramArguments</key> | |
| <array> | |
| <string>/Users/nickxiao/bin/onwificonnect</string> | |
| <string>/Users/nickxiao/bin/startwireproxy</string> | |
| </array> | |
| </dict> | |
| </plist> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
% clang -framework Foundation -o onwificonnect -g onwificonnect.mm
% ./onwificonnect ~/bin/startwireproxy