Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-2470] Created branchVPNBlockingError method #1414

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/BranchSDK/BNCServerInterface.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ - (void)genericHTTPRequest:(NSURLRequest *)request retryNumber:(NSInteger)retryN
if (status != 200) {
if ([NSError branchDNSBlockingError:underlyingError]) {
[[BranchLogger shared] logWarning:[NSString stringWithFormat:@"Possible DNS Ad Blocker. Giving up on request with HTTP status code %ld", (long)status] error:underlyingError];
} else if ([NSError branchVPNBlockingError:underlyingError]) {
[[BranchLogger shared] logWarning:[NSString stringWithFormat:@"Possible VPN Ad Blocker. Giving up on request with HTTP status code %ld", (long)status] error:underlyingError];
} else {
[[BranchLogger shared] logWarning: [NSString stringWithFormat:@"Giving up on request with HTTP status code %ld", (long)status] error:underlyingError];
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/BranchSDK/BNCURLFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ - (BOOL)foundUpdatedURLList:(id<BNCNetworkOperationProtocol>)operation {
} else if (statusCode != 200 || error != nil || jsonString == nil) {
if ([NSError branchDNSBlockingError:error]) {
[[BranchLogger shared] logWarning:@"Possible DNS Ad Blocker" error:error];
} else if ([NSError branchVPNBlockingError:error]) {
[[BranchLogger shared] logWarning:@"Possible VPN Ad Blocker" error:error];
} else {
[[BranchLogger shared] logWarning:@"Failed to update URL ignore list" error:operation.error];
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/BranchSDK/BranchQRCode.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ - (void)callQRCodeAPI:(nullable NSDictionary *)params
if (error) {
if ([NSError branchDNSBlockingError:error]) {
[[BranchLogger shared] logWarning:@"Possible DNS Ad Blocker" error:error];
} else if ([NSError branchVPNBlockingError:error]) {
[[BranchLogger shared] logWarning:@"Possible VPN Ad Blocker" error:error];
} else {
[[BranchLogger shared] logError:@"QR Code request failed" error:error];
completion(nil, error);
Expand Down
46 changes: 46 additions & 0 deletions Sources/BranchSDK/NSError+Branch.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,50 @@ + (BOOL)branchDNSBlockingError:(NSError *)error {
return NO;
}

+ (BOOL)branchVPNBlockingError:(NSError *)error {
if (error) {
NSError *underlyingError = error.userInfo[@"NSUnderlyingError"];
if (underlyingError) {

/**
`Domain=kCFErrorDomainCFNetwork Code=-1004` indicates that the connection failed because a connection can't be made to the host.
Reference: https://developer.apple.com/documentation/cfnetwork/cfnetworkerrors/kcfurlerrorcannotconnecttohost?language=objc

`_kCFStreamErrorCodeKey=61` indicates that the connection was refused.
Reference: https://opensource.apple.com/source/xnu/xnu-792/bsd/sys/errno.h.auto.html
*/

BOOL isCouldntConnectErrorCode = [@(-1004) isEqual:@(underlyingError.code)];
BOOL isLocalHostErrorKey = [@(61) isEqual:error.userInfo[@"_kCFStreamErrorCodeKey"]];

if ([self isConnectedToVPN] && isCouldntConnectErrorCode && isLocalHostErrorKey) {
return YES;
}
}
}
return NO;
}

/**
Helper method to which checks the device's internet proxy settings for common VPN protocol and interface substrings to determine if a VPN enabled.
https://developer.apple.com/documentation/cfnetwork/cfnetworkcopysystemproxysettings()
*/
+ (BOOL)isConnectedToVPN {
NSDictionary *proxySettings = (__bridge NSDictionary *)(CFNetworkCopySystemProxySettings());
gdeluna-branch marked this conversation as resolved.
Show resolved Hide resolved
if (proxySettings) {
NSDictionary *scopedSettings = proxySettings[@"__SCOPED__"];
if (scopedSettings) {
for (NSString *key in scopedSettings) {
if ([key containsString:@"tap"] ||
[key containsString:@"tun"] ||
[key containsString:@"ppp"] ||
[key containsString:@"ipsec"]) {
gdeluna-branch marked this conversation as resolved.
Show resolved Hide resolved
return YES;
}
}
}
}
return NO;
}

@end
3 changes: 3 additions & 0 deletions Sources/BranchSDK/Private/NSError+Branch.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ typedef NS_ENUM(NSInteger, BNCErrorCode) {
// Checks if an NSError looks like a DNS blocking error
+ (BOOL)branchDNSBlockingError:(NSError *)error;

// Checks if an NSError looks like a VPN blocking error
+ (BOOL)branchVPNBlockingError:(NSError *)error;

@end

NS_ASSUME_NONNULL_END
Expand Down
Loading