Skip to content

Commit c79d5bf

Browse files
committed
Add check for DNS blocking just to make logs more informative
1 parent 559560b commit c79d5bf

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

Sources/BranchSDK/BNCServerInterface.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ - (void)genericHTTPRequest:(NSURLRequest *)request retryNumber:(NSInteger)retryN
122122

123123
} else {
124124
if (status != 200) {
125-
[[BranchLogger shared] logWarning: [NSString stringWithFormat:@"Giving up on request with HTTP status code %ld", (long)status] error:underlyingError];
125+
if ([NSError branchDNSBlockingError:underlyingError]) {
126+
[[BranchLogger shared] logWarning:[NSString stringWithFormat:@"Possible DNS Ad Blocker. Giving up on request with HTTP status code %ld", (long)status] error:underlyingError];
127+
} else {
128+
[[BranchLogger shared] logWarning: [NSString stringWithFormat:@"Giving up on request with HTTP status code %ld", (long)status] error:underlyingError];
129+
}
126130
}
127131

128132
// Don't call on the main queue since it might be blocked.
@@ -301,7 +305,7 @@ - (BNCServerResponse *)processServerResponse:(NSURLResponse *)response data:(NSD
301305
serverResponse.requestId = requestId;
302306

303307
if ([[BranchLogger shared] shouldLog:BranchLogLevelDebug]) {
304-
[[BranchLogger shared] logWarning:@"Request failed with NSError" error:error];
308+
[[BranchLogger shared] logDebug:@"Request failed with NSError" error:error];
305309
}
306310
}
307311

Sources/BranchSDK/BNCURLFilter.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "BNCURLFilter.h"
1212
#import "Branch.h"
1313
#import "BranchLogger.h"
14+
#import "NSError+Branch.h"
1415

1516
@interface BNCURLFilter ()
1617

@@ -130,7 +131,11 @@ - (BOOL)foundUpdatedURLList:(id<BNCNetworkOperationProtocol>)operation {
130131
[[BranchLogger shared] logDebug:@"No update for URL ignore list found." error:nil];
131132
return NO;
132133
} else if (statusCode != 200 || error != nil || jsonString == nil) {
133-
[[BranchLogger shared] logWarning:[NSString stringWithFormat:@"Failed to update URL ignore list. error: %@ status: %ld", operation.error, (long)operation.response.statusCode] error:operation.error];
134+
if ([NSError branchDNSBlockingError:error]) {
135+
[[BranchLogger shared] logWarning:@"Possible DNS Ad Blocker" error:error];
136+
} else {
137+
[[BranchLogger shared] logWarning:@"Failed to update URL ignore list" error:operation.error];
138+
}
134139
return NO;
135140
} else {
136141
return YES;

Sources/BranchSDK/BranchQRCode.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,12 @@ - (void)callQRCodeAPI:(nullable NSDictionary *)params
143143
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
144144

145145
if (error) {
146-
[[BranchLogger shared] logError:@"QR Code request failed" error:error];
147-
completion(nil, error);
146+
if ([NSError branchDNSBlockingError:error]) {
147+
[[BranchLogger shared] logWarning:@"Possible DNS Ad Blocker" error:error];
148+
} else {
149+
[[BranchLogger shared] logError:@"QR Code request failed" error:error];
150+
completion(nil, error);
151+
}
148152
return;
149153
}
150154

Sources/BranchSDK/NSError+Branch.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,36 @@ + (NSError *) branchErrorWithCode:(BNCErrorCode)errorCode localizedMessage:(NSSt
8383
return [NSError branchErrorWithCode:errorCode error:nil localizedMessage:message];
8484
}
8585

86+
+ (BOOL)branchDNSBlockingError:(NSError *)error {
87+
if (error) {
88+
NSError *underlyingError = error.userInfo[@"NSUnderlyingError"];
89+
if (underlyingError) {
90+
91+
/**
92+
Check if an NSError was likely caused by a DNS sinkhole, such as Pi-hole.
93+
The OS level logs will show that the IP address that failed is all 0's, however App level logs will not contain that information.
94+
95+
`Domain=kCFErrorDomainCFNetwork Code=-1000` - Connection failed due to a malformed URL. A bit misleading since Ad blockers DNS resolve the URL as 0.0.0.0.
96+
https://developer.apple.com/documentation/cfnetwork/cfnetworkerrors/kcfurlerrorbadurl?language=objc
97+
98+
`_kCFStreamErrorDomainKey=1` Error domain is a POSIX error.
99+
https://opensource.apple.com/source/CF/CF-550.13/CFStream.h.auto.html
100+
101+
`_kCFStreamErrorCodeKey=22` POSIX error is invalid argument. In this case the IP address is 0.0.0.0, which is invalid.
102+
https://opensource.apple.com/source/xnu/xnu-792/bsd/sys/errno.h.auto.html
103+
*/
104+
BOOL isCFErrorDomainCFNetwork = [((NSString *)kCFErrorDomainCFNetwork) isEqualToString:underlyingError.domain];
105+
BOOL isCodeMalFormedURL = [@(-1000) isEqual:@(underlyingError.code)];
106+
107+
BOOL isErrorDomainPosix = [@(1) isEqual:error.userInfo[@"_kCFStreamErrorDomainKey"]];
108+
BOOL isPosixInvalidArgument = [@(22) isEqual:error.userInfo[@"_kCFStreamErrorCodeKey"]];
109+
110+
if (isCFErrorDomainCFNetwork && isCodeMalFormedURL && isErrorDomainPosix && isPosixInvalidArgument) {
111+
return YES;
112+
}
113+
}
114+
}
115+
return NO;
116+
}
117+
86118
@end

Sources/BranchSDK/Private/NSError+Branch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ typedef NS_ENUM(NSInteger, BNCErrorCode) {
4343
+ (NSError *) branchErrorWithCode:(BNCErrorCode)errorCode error:(NSError *_Nullable)error;
4444
+ (NSError *) branchErrorWithCode:(BNCErrorCode)errorCode localizedMessage:(NSString *_Nullable)message;
4545

46+
// Checks if an NSError looks like a DNS blocking error
47+
+ (BOOL)branchDNSBlockingError:(NSError *)error;
48+
4649
@end
4750

4851
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)