diff --git a/app/AppDelegate.m b/app/AppDelegate.m index 987b3853..8cc66287 100644 --- a/app/AppDelegate.m +++ b/app/AppDelegate.m @@ -8,6 +8,7 @@ #include #include #include +#import #import "AppDelegate.h" #import "PasteboardDevice.h" #import "LocationDevice.h" @@ -22,6 +23,7 @@ @interface AppDelegate () @property BOOL exiting; +@property SCNetworkReachabilityRef reachability; @end @@ -124,32 +126,7 @@ static void ios_handle_die(const char *msg) { do_mount(&procfs, "proc", "/proc", 0); do_mount(&devptsfs, "devpts", "/dev/pts", 0); - // configure dns - struct __res_state res; - if (EXIT_SUCCESS != res_ninit(&res)) { - exit(2); - } - NSMutableString *resolvConf = [NSMutableString new]; - for (int i = 0; res.dnsrch[i] != NULL; i++) { - [resolvConf appendFormat:@"search %s\n", res.dnsrch[i]]; - } - union res_sockaddr_union servers[NI_MAXSERV]; - int serversFound = res_getservers(&res, servers, NI_MAXSERV); - char address[NI_MAXHOST]; - for (int i = 0; i < serversFound; i ++) { - union res_sockaddr_union s = servers[i]; - if (s.sin.sin_len == 0) - continue; - getnameinfo((struct sockaddr *) &s.sin, s.sin.sin_len, - address, sizeof(address), - NULL, 0, NI_NUMERICHOST); - [resolvConf appendFormat:@"nameserver %s\n", address]; - } - struct fd *fd = generic_open("/etc/resolv.conf", O_WRONLY_ | O_CREAT_ | O_TRUNC_, 0666); - if (!IS_ERR(fd)) { - fd->ops->write(fd, resolvConf.UTF8String, [resolvConf lengthOfBytesUsingEncoding:NSUTF8StringEncoding]); - fd_close(fd); - } + [self configureDns]; exit_hook = ios_handle_exit; die_handler = ios_handle_die; @@ -176,6 +153,34 @@ static void ios_handle_die(const char *msg) { return 0; } +- (void)configureDns { + struct __res_state res; + if (EXIT_SUCCESS != res_ninit(&res)) { + exit(2); + } + NSMutableString *resolvConf = [NSMutableString new]; + for (int i = 0; res.dnsrch[i] != NULL; i++) { + [resolvConf appendFormat:@"search %s\n", res.dnsrch[i]]; + } + union res_sockaddr_union servers[NI_MAXSERV]; + int serversFound = res_getservers(&res, servers, NI_MAXSERV); + char address[NI_MAXHOST]; + for (int i = 0; i < serversFound; i ++) { + union res_sockaddr_union s = servers[i]; + if (s.sin.sin_len == 0) + continue; + getnameinfo((struct sockaddr *) &s.sin, s.sin.sin_len, + address, sizeof(address), + NULL, 0, NI_NUMERICHOST); + [resolvConf appendFormat:@"nameserver %s\n", address]; + } + struct fd *fd = generic_open("/etc/resolv.conf", O_WRONLY_ | O_CREAT_ | O_TRUNC_, 0666); + if (!IS_ERR(fd)) { + fd->ops->write(fd, resolvConf.UTF8String, [resolvConf lengthOfBytesUsingEncoding:NSUTF8StringEncoding]); + fd_close(fd); + } +} + static int bootError; + (int)bootError { @@ -187,12 +192,28 @@ static int bootError; return YES; } +void NetworkReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info) { + AppDelegate *self = (__bridge AppDelegate *) info; + [self configureDns]; +} + - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // get the network permissions popup to appear on chinese devices [[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:@"http://captive.apple.com"]] resume]; [UserPreferences.shared addObserver:self forKeyPath:@"shouldDisableDimming" options:NSKeyValueObservingOptionInitial context:nil]; + struct sockaddr_in6 address = { + .sin6_len = sizeof(address), + .sin6_family = AF_INET6, + }; + self.reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (struct sockaddr *) &address); + SCNetworkReachabilityContext context = { + .info = (__bridge void *) self, + }; + SCNetworkReachabilitySetCallback(self.reachability, NetworkReachabilityCallback, &context); + SCNetworkReachabilityScheduleWithRunLoop(self.reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes); + if (self.window != nil) { // For iOS <13, where the app delegate owns the window instead of the scene TerminalViewController *vc = (TerminalViewController *) self.window.rootViewController; @@ -205,6 +226,20 @@ static int bootError; UIApplication.sharedApplication.idleTimerDisabled = UserPreferences.shared.shouldDisableDimming; } +- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet *)sceneSessions API_AVAILABLE(ios(13.0)) { + for (UISceneSession *sceneSession in sceneSessions) { + NSString *terminalUUID = sceneSession.stateRestorationActivity.userInfo[@"TerminalUUID"]; + [[Terminal terminalWithUUID:[[NSUUID alloc] initWithUUIDString:terminalUUID]] destroy]; + } +} + +- (void)dealloc { + if (self.reachability != NULL) { + SCNetworkReachabilityUnscheduleFromRunLoop(self.reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes); + CFRelease(self.reachability); + } +} + - (void)exitApp { self.exiting = YES; id app = [UIApplication sharedApplication]; @@ -216,13 +251,6 @@ static int bootError; exit(0); } -- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet *)sceneSessions API_AVAILABLE(ios(13.0)) { - for (UISceneSession *sceneSession in sceneSessions) { - NSString *terminalUUID = sceneSession.stateRestorationActivity.userInfo[@"TerminalUUID"]; - [[Terminal terminalWithUUID:[[NSUUID alloc] initWithUUIDString:terminalUUID]] destroy]; - } -} - @end NSString *const ProcessExitedNotification = @"ProcessExitedNotification"; diff --git a/iSH.xcodeproj/project.pbxproj b/iSH.xcodeproj/project.pbxproj index efe225f2..96bd0270 100644 --- a/iSH.xcodeproj/project.pbxproj +++ b/iSH.xcodeproj/project.pbxproj @@ -38,6 +38,7 @@ BB88F49A2154760800A341FD /* FileProviderEnumerator.m in Sources */ = {isa = PBXBuildFile; fileRef = BB88F4992154760800A341FD /* FileProviderEnumerator.m */; }; BB88F49F2154760800A341FD /* iSHFileProvider.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = BB88F4902154760800A341FD /* iSHFileProvider.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; BB88F4A62154770200A341FD /* libish.a in Frameworks */ = {isa = PBXBuildFile; fileRef = BB13F7DC200AD81D003D1C4D /* libish.a */; }; + BBA8E2C1236FF5EA00515F76 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BBA8E2C0236FF5EA00515F76 /* SystemConfiguration.framework */; }; BBBCE7E321D2F02200CA00B3 /* About.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BBBCE7E521D2F02200CA00B3 /* About.storyboard */; }; BBBFE94921C5CFF100509DD5 /* NSError+ISHErrno.m in Sources */ = {isa = PBXBuildFile; fileRef = BB13F4DD21C5770000343E17 /* NSError+ISHErrno.m */; }; BBCC9D962365430800424C83 /* SceneDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = BBCC9D952365430800424C83 /* SceneDelegate.m */; }; @@ -289,6 +290,7 @@ BB88F49B2154760800A341FD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; BB88F49C2154760800A341FD /* iSHFileProvider.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = iSHFileProvider.entitlements; sourceTree = ""; }; BB88F4A4215476BA00A341FD /* iSH.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = iSH.entitlements; sourceTree = ""; }; + BBA8E2C0236FF5EA00515F76 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; }; BBBCE7E421D2F02200CA00B3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/About.storyboard; sourceTree = ""; }; BBCC9D942365430800424C83 /* SceneDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SceneDelegate.h; sourceTree = ""; }; BBCC9D952365430800424C83 /* SceneDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SceneDelegate.m; sourceTree = ""; }; @@ -312,6 +314,7 @@ buildActionMask = 2147483647; files = ( BB235537235D49B300139E00 /* CoreLocation.framework in Frameworks */, + BBA8E2C1236FF5EA00515F76 /* SystemConfiguration.framework in Frameworks */, BB6DB261216435340047A611 /* libiconv.tbd in Frameworks */, BBFB55662158644C00DFE6DE /* libresolv.tbd in Frameworks */, BB13F7EA200ADCED003D1C4D /* libish.a in Frameworks */, @@ -453,6 +456,7 @@ BB792B7D1F96E32B00FFB7A4 /* Frameworks */ = { isa = PBXGroup; children = ( + BBA8E2C0236FF5EA00515F76 /* SystemConfiguration.framework */, BB235536235D49B300139E00 /* CoreLocation.framework */, BB6DB260216435330047A611 /* libiconv.tbd */, BBFB55652158644C00DFE6DE /* libresolv.tbd */,