Skip to content

Instantly share code, notes, and snippets.

@AlexFlyce
Created April 15, 2020 14:13
Show Gist options
  • Save AlexFlyce/3161cb1976a7d921d255bd5576c89195 to your computer and use it in GitHub Desktop.
Save AlexFlyce/3161cb1976a7d921d255bd5576c89195 to your computer and use it in GitHub Desktop.
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