Flutter判断设备是否连接了VPN

2023-03-20  本文已影响0人  OwenWong

代码:

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
}
上一篇 下一篇

猜你喜欢

热点阅读