Created
April 15, 2020 14:13
-
-
Save AlexFlyce/3161cb1976a7d921d255bd5576c89195 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct VpnChecker { | |
private static let vpnProtocolsKeysIdentifiers = [ | |
"tap", "tun", "ppp", "ipsec", "utun" | |
] | |
static func isVpnActive() -> Bool { | |
guard let cfDict = CFNetworkCopySystemProxySettings() else { return false } | |
let nsDict = cfDict.takeRetainedValue() as NSDictionary | |
guard let keys = nsDict["__SCOPED__"] as? NSDictionary, | |
let allKeys = keys.allKeys as? [String] else { return false } | |
// Checking for tunneling protocols in the keys | |
for key in allKeys { | |
for protocolId in vpnProtocolsKeysIdentifiers | |
where key.starts(with: protocolId) { | |
return true | |
} | |
} | |
return false | |
} | |
} | |
/// Notes: I use `start(with:)` method, so that I can cover also `ipsec4`, `ppp0`, `utun0` etc... | |
// Usage: | |
// VpnChecker.isVpnActive() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment