The ultimate VPN detection guide for iOS and Android

Md. Ibrahim Hassan
Tarka Labs Blog
Published in
3 min readApr 11, 2023

--

Artwork generated by Dall-E

A Virtual Private Network (VPN) creates a secure connection between a user’s device and the VPN server by encapsulating data packets in an outer packet using a tunneling protocol. Once the data packets reach the server, they are decrypted and forwarded to their destination on the internet.

Why do some service providers disallow VPN Use?

The reasons may vary.

  1. Banks may block VPN connections due to security concerns since they can hide a user’s IP address and encrypt their traffic, which cybercriminals normally use to evade detection.
  2. OTTs often ban VPNs use due to regional content licensing agreements, varying government laws, and censorship. VPNs can help users bypass content restrictions, leading to legal and ethical concerns for OTTs.
  3. To prevent users from subscribing from countries where the subscriptions are cheaper.

How can we detect VPN Usage?

VPN use can be detected both on the server and client sides. Let’s first explore how VPN detection can be done on the server side:

  • Check against a list of public / paid VPN Servers IP addresses.
  • Analyze the incoming traffic. If you can find a large number of users with the same IP address, then it’s probably a VPN server.
  • GeoIP tracking — the user’s location and last login date is analyzed. If there is an unrealistic change in the user’s location in an unrealistic timeframe, it could indicate VPN usage.

VPN detection on the client side is more straightforward since the data is encrypted on the client side using a tunneling protocol. On the client side, you can check the Network Proxy settings to check if any tunneling protocols are being used.

How to detect VPN usage in iOS (Swift)

By reading the network proxy settings, you can manually check if tunneling protocols are used. For this, you can use the following code snippet:

DetectVPNUse.swift

In the above code snippet, CFNetworkCopySystemProxySettings is a low-level API that returns current internet proxy settings as a CFDictionary. You can cast this to an NSDictionary and check if any of the tunneling protocols, i.e., tap, tun, ppp, ipsec, utun are in use.

How to detect VPN usage in Android (Kotlin)

In Android, you can check if the active network is using a VPN by the following code snippet:

DetectVPNUse.kt

ConnectivityManager is an Android API to manage network connections. It provides methods to check the availability and status of network connections and request and configure network connections. You can use ConnectivityManager to get the network capabilities of the active network. You can then check if the VPN transport protocol is being used.

How to detect VPN usage with Flutter (Dart)

In Flutter, the approach is similar to iOS. You can manually check if any of the tunneling protocols are in use:

DetectVPNUse.dart

Retrieve a list of network interfaces - a NetworkInterface represents an active network interface on the current system. You can then check if any of the interfaces have names that contain tun, ppp, and pptp, which are commonly used in VPN connections.

How to detect VPN usage with React Native

React Native leverages the platform-specific code discussed above to get the network configuration and detect if a VPN is being used. This can be done by using the react-native-vpn-detect package.

So there you have it!

VPN detection is super straightforward on native Android, purely because you don’t have to manually check for tunnelling protocol names in your network proxy settings. Almost all other frameworks manually check for tunnelling protocol names, which can break if the user uses a different tunnelling protocol. These checks can easily break if new tunnelling protocols are added or the APIs change.

Hi! I’m Ibrahim, an ex-operations executive turned innovative iOS developer. Currently, I’m creating cutting-edge experiences @ TarkaLabs and love to geek out on the latest tech stacks.

--

--