isVpnActive function
Checks whether a VPN connection is currently active on the device.
This function inspects network interfaces and returns true
if any interface
typically used by VPNs (like tun
, ppp
, or pptp
) is found.
Example:
bool active = await isVpnActive();
logFile(key: active ? 'VPN is active' : 'VPN is not active', name: 'is vpn active');
Returns a Future<bool> that completes with true
if VPN is detected.
Implementation
Future<bool> isVpnActive() async {
bool isVpnActive;
List<NetworkInterface> interfaces = await NetworkInterface.list(
includeLoopback: false,
type: InternetAddressType.any,
);
interfaces.isNotEmpty
? isVpnActive = interfaces.any(
(interface) =>
interface.name.contains("tun") ||
interface.name.contains("ppp") ||
interface.name.contains("pptp"),
)
: isVpnActive = false;
return isVpnActive;
}