Skip to content

Instantly share code, notes, and snippets.

@esterTion esterTion/pac_add.c

Last active Oct 14, 2019
Embed
What would you like to do?
Set a pac url to com.apple.CommCenter (ip1), i.e. cellular data
//
// pac_add.m
// pac_add
//
// https://gist.github.com/esterTion/8bdeb78b35c172a4485f6d9e29476336/edit
//
// Created by esterTion on 2019/4/19.
// xcrun -sdk iphoneos clang -isysroot $THEOS/sdks/iPhoneOS11.2.sdk -arch arm64 -I$THEOS/include -framework Foundation -framework SystemConfiguration -L$THEOS/lib -lactivator main.m -o pac_add
//
#import <Foundation/Foundation.h>
#import <stdio.h>
#import "GetBSDProcessList.h"
#import <libactivator/libactivator.h>
#import <SystemConfiguration/SystemConfiguration.h>
static void killWifid() {
kinfo_proc *results = NULL;
size_t procCount=0;
GetBSDProcessList(&results, &procCount);
kinfo_proc* current_process = results;
for (int i=0; i<procCount && i<max_processes; i++)
{
if (strcmp(current_process->kp_proc.p_comm, "wifid") == 0) {
kill(current_process->kp_proc.p_pid, 9);
break;
}
current_process += 1;
}
free(results);
results = NULL;
}
// https://stackoverflow.com/a/40305705
#import <ifaddrs.h>
#import <arpa/inet.h>
static BOOL CheckWiFi() {
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
BOOL hasWifi = NO;
int err = getifaddrs(&interfaces);
if(err == 0) {
temp_addr = interfaces;
while(temp_addr) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
struct sockaddr_in *addr = (struct sockaddr_in *)temp_addr->ifa_addr;
if(memcmp(temp_addr->ifa_name, "en", 2) == 0) {
hasWifi = YES;
break;
}
}
temp_addr = temp_addr->ifa_next;
}
}
freeifaddrs(interfaces);
return hasWifi;
}
int main (int argc, const char * argv[])
{
@autoreleasepool
{
if (argc < 2) exit(1);
NSString *ip1Id = NULL;
NSString *filePath = @"/var/preferences/SystemConfiguration/preferences.plist";
bool restartWifi = false;
bool restartHotspot = false;
for (int i=2; i<argc; i++) {
if (strcmp(argv[i], "-wifi") == 0) restartWifi = true;
else if (strcmp(argv[i], "-hotspot") == 0) restartHotspot = true;
}
while (1) {
NSMutableDictionary* preferences = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
if (!ip1Id) {
for (NSString *serviceId in preferences[@"NetworkServices"]) {
if ([preferences[@"NetworkServices"][serviceId][@"UserDefinedName"] isEqualToString:@"com.apple.CommCenter (ip1)"]) {
ip1Id = serviceId;
break;
}
}
printf("%s\n", [ip1Id cStringUsingEncoding:NSUTF8StringEncoding]);
}
if (!ip1Id) exit(2);
//printf("check\n");
if (!preferences[@"NetworkServices"][ip1Id][@"Proxies"]) {
if (CheckWiFi()) {
//printf("current in wifi\n");
} else {
SCDynamicStoreRef sc = SCDynamicStoreCreate(NULL, CFSTR("com.apple.wirelessmodemsettings.MISManager"), NULL, NULL);
NSDictionary* info = (__bridge_transfer NSDictionary*)SCDynamicStoreCopyValue(sc, CFSTR("com.apple.MobileInternetSharing"));
CFRelease(sc);
bool hotspotOn = [info[@"ExternalInterfaces"] count] > 0;
//printf("hotspot state: %d\n", hotspotOn);
[info release];
preferences[@"NetworkServices"][ip1Id][@"Proxies"] = @{
@"ProxyAutoConfigEnable": @1,
@"ProxyAutoConfigURLString": [[NSString alloc] initWithCString:argv[1] encoding:NSUTF8StringEncoding]
};
bool success = [preferences writeToFile:filePath atomically:true];
killWifid(); // first kill wifid to let system reload cellular proxy
LAEvent *event;
sleep(1); // sleep 1 sec, and then toggle wifi twice, to reset wifi state
if (restartHotspot && hotspotOn) {
printf("toggle hotspot\n");
event = [LAEvent eventWithName:@"switch-flip.com.a3tweaks.switch.hotspot" mode:[[LAActivator sharedInstance] currentEventMode]];
[[LAActivator sharedInstance] sendEvent:event toListenerWithName:@"switch-flip.com.a3tweaks.switch.hotspot"];
}
sleep(1);
if (restartWifi) {
printf("toggle wifi\n");
event = [LAEvent eventWithName:@"switch-flip.com.a3tweaks.switch.wifi" mode:[[LAActivator sharedInstance] currentEventMode]];
[[LAActivator sharedInstance] sendEvent:event toListenerWithName:@"switch-flip.com.a3tweaks.switch.wifi"];
}
sleep(3);
if (restartWifi) {
printf("toggle wifi\n");
event = [LAEvent eventWithName:@"switch-flip.com.a3tweaks.switch.wifi" mode:[[LAActivator sharedInstance] currentEventMode]];
[[LAActivator sharedInstance] sendEvent:event toListenerWithName:@"switch-flip.com.a3tweaks.switch.wifi"];
}
if (restartHotspot && hotspotOn) {
printf("toggle hotspot\n");
sleep(3);
event = [LAEvent eventWithName:@"switch-flip.com.a3tweaks.switch.hotspot" mode:[[LAActivator sharedInstance] currentEventMode]];
[[LAActivator sharedInstance] sendEvent:event toListenerWithName:@"switch-flip.com.a3tweaks.switch.hotspot"];
}
//printf("write: %d\n", success);
}
}
[preferences release];
sleep(5);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.