Don't send a STUN request when a VPN is connected

This commit is contained in:
Cameron Gutman
2019-11-04 21:28:53 -08:00
parent c89de92e23
commit e89707e601

View File

@@ -163,6 +163,20 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp";
return nil;
}
- (BOOL)isActiveNetworkVPN {
NSDictionary *dict = CFBridgingRelease(CFNetworkCopySystemProxySettings());
NSArray *keys = [dict[@"__SCOPED__"] allKeys];
for (NSString *key in keys) {
if ([key containsString:@"tap"] ||
[key containsString:@"tun"] ||
[key containsString:@"ppp"] ||
[key containsString:@"ipsec"]) {
return YES;
}
}
return NO;
}
- (void)netServiceDidResolveAddress:(NSNetService *)service {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray<NSData*>* addresses = [service addresses];
@@ -180,19 +194,23 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp";
continue;
}
// Since we discovered this host over IPv4 mDNS, we know we're on the same network
// as the PC and we can use our current WAN address as a likely candidate
// for our PC's external address.
struct in_addr wanAddr;
int err = LiFindExternalAddressIP4("stun.moonlight-stream.org", 3478, &wanAddr.s_addr);
if (err == 0) {
char addrStr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &wanAddr, addrStr, sizeof(addrStr));
host.externalAddress = [NSString stringWithFormat: @"%s", addrStr];
Log(LOG_I, @"External IPv4 address (STUN): %@ -> %@", [service hostName], host.externalAddress);
}
else {
Log(LOG_E, @"STUN failed to get WAN address: %d", err);
// Don't send a STUN request if we're connected to a VPN. We'll likely get the VPN
// gateway's external address rather than the external address of the LAN.
if (![self isActiveNetworkVPN]) {
// Since we discovered this host over IPv4 mDNS, we know we're on the same network
// as the PC and we can use our current WAN address as a likely candidate
// for our PC's external address.
struct in_addr wanAddr;
int err = LiFindExternalAddressIP4("stun.moonlight-stream.org", 3478, &wanAddr.s_addr);
if (err == 0) {
char addrStr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &wanAddr, addrStr, sizeof(addrStr));
host.externalAddress = [NSString stringWithFormat: @"%s", addrStr];
Log(LOG_I, @"External IPv4 address (STUN): %@ -> %@", [service hostName], host.externalAddress);
}
else {
Log(LOG_E, @"STUN failed to get WAN address: %d", err);
}
}
host.localAddress = [MDNSManager sockAddrToString:addrData];