代码:
class CheckVpnConnectionStatus {
/// 如果设备具有 VPN 连接,返回 true
static 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;
}
}
使用:
if (await CheckVpnConnectionStatus.isVpnActive()) {
// existence vpn connect
}