From d72eee6cf710601ef2f164a01aba6afdbcfb1fca Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Wed, 22 Mar 2017 09:58:13 -0700 Subject: If we get a 401 error from the ZeroTier service in the macOS UI, try re-copying the auth token --- macui/ZeroTier One/ServiceCom.h | 1 + macui/ZeroTier One/ServiceCom.m | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) (limited to 'macui') diff --git a/macui/ZeroTier One/ServiceCom.h b/macui/ZeroTier One/ServiceCom.h index 74ab2b35..c2b4692f 100644 --- a/macui/ZeroTier One/ServiceCom.h +++ b/macui/ZeroTier One/ServiceCom.h @@ -26,6 +26,7 @@ NSString *baseURL; NSURLSession *session; BOOL _isQuitting; + BOOL _resetKey; } + (ServiceCom*)sharedInstance; diff --git a/macui/ZeroTier One/ServiceCom.m b/macui/ZeroTier One/ServiceCom.m index dd03b3f7..06d676bb 100644 --- a/macui/ZeroTier One/ServiceCom.m +++ b/macui/ZeroTier One/ServiceCom.m @@ -46,6 +46,7 @@ baseURL = @"http://127.0.0.1:9993"; session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]]; _isQuitting = NO; + _resetKey = NO; } return self; @@ -54,6 +55,37 @@ - (NSString*)key:(NSError* __autoreleasing *)err { static NSString *k = nil; + static NSUInteger resetCount = 10; + + if (_resetKey && k != nil) { + k = nil; + ++resetCount; + NSLog(@"ResetCount: %lu", (unsigned long)resetCount); + if (resetCount > 10) { + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + NSAlert *alert = [NSAlert alertWithMessageText:@"Error obtaining Auth Token" + defaultButton:@"Quit" + alternateButton:@"Retry" + otherButton:nil + informativeTextWithFormat:@"Please ensure ZeroTier is installed correctly"]; + alert.alertStyle = NSCriticalAlertStyle; + + NSModalResponse res; + if (!_isQuitting) { + res = [alert runModal]; + } + else { + return; + } + + if(res == 1) { + _isQuitting = YES; + [NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0]; + } + }]; + return @""; + } + } if (k == nil) { NSError *error = nil; @@ -67,10 +99,12 @@ appSupportDir = [[appSupportDir URLByAppendingPathComponent:@"ZeroTier"] URLByAppendingPathComponent:@"One"]; NSURL *authtokenURL = [appSupportDir URLByAppendingPathComponent:@"authtoken.secret"]; - if ([[NSFileManager defaultManager] fileExistsAtPath:[authtokenURL path]]) { + if (!_resetKey && [[NSFileManager defaultManager] fileExistsAtPath:[authtokenURL path]]) { k = [NSString stringWithContentsOfURL:authtokenURL encoding:NSUTF8StringEncoding error:&error]; + + k = [k stringByReplacingOccurrencesOfString:@"\n" withString:@""]; if (error) { NSLog(@"Error: %@", error); @@ -80,6 +114,7 @@ } } else { + _resetKey = NO; NSURL *sysAppSupportDir = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory inDomain:NSSystemDomainMask appropriateForURL:nil create:false error:nil]; sysAppSupportDir = [[sysAppSupportDir URLByAppendingPathComponent:@"ZeroTier"] URLByAppendingPathComponent:@"One"]; @@ -249,6 +284,9 @@ completionHandler(networks); } + else if (status == 401) { + self->_resetKey = YES; + } }]; [task resume]; } -- cgit v1.2.3