network_helper.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import 'dart:io';
  2. import 'package:connectivity_plus/connectivity_plus.dart';
  3. import 'package:network_info_plus/network_info_plus.dart';
  4. class NetworkHelper {
  5. static final NetworkHelper _instance = NetworkHelper._();
  6. static NetworkHelper get instance => _instance;
  7. final _connectivity = Connectivity();
  8. final _networkInfo = NetworkInfo();
  9. NetworkHelper._();
  10. // 检查网络是否可用
  11. Future<bool> isNetworkAvailable() async {
  12. try {
  13. final connectivityResult = await _connectivity.checkConnectivity();
  14. if (connectivityResult.contains(ConnectivityResult.none)) {
  15. return false;
  16. }
  17. // 如果是WiFi,进一步检查WiFi信息
  18. if (connectivityResult.contains(ConnectivityResult.wifi)) {
  19. final wifiName = await _networkInfo.getWifiName();
  20. final wifiIP = await _networkInfo.getWifiIP();
  21. // 如果无法获取WiFi名称和IP,可能表示网络异常
  22. if (wifiName == null && wifiIP == null) {
  23. return false;
  24. }
  25. }
  26. return true;
  27. } catch (e) {
  28. return false;
  29. }
  30. }
  31. // 获取当前网络类型
  32. Future<String> getNetworkType() async {
  33. try {
  34. final connectivityResult = await _connectivity.checkConnectivity();
  35. if (connectivityResult.contains(ConnectivityResult.mobile)) {
  36. return 'Mobile';
  37. } else if (connectivityResult.contains(ConnectivityResult.wifi)) {
  38. final wifiName = await _networkInfo.getWifiName();
  39. return 'WiFi${wifiName != null ? " ($wifiName)" : ""}';
  40. } else if (connectivityResult.contains(ConnectivityResult.ethernet)) {
  41. return 'Ethernet';
  42. } else if (connectivityResult.contains(ConnectivityResult.vpn)) {
  43. return 'VPN';
  44. } else if (connectivityResult.contains(ConnectivityResult.bluetooth)) {
  45. return 'Bluetooth';
  46. } else if (connectivityResult.contains(ConnectivityResult.other)) {
  47. return 'Other';
  48. } else if (connectivityResult.contains(ConnectivityResult.none)) {
  49. return 'No Network';
  50. } else {
  51. return 'Unknown';
  52. }
  53. } catch (e) {
  54. return 'Error';
  55. }
  56. }
  57. static final List<String> _commonVpnInterfaceNamePatterns = [
  58. 'tun', // Linux/Unix TUN interface
  59. 'tap', // Linux/Unix TAP interface
  60. 'ppp', // Point-to-Point Protocol
  61. 'pptp', // PPTP VPN
  62. 'l2tp', // L2TP VPN
  63. 'ipsec', // IPsec VPN
  64. 'vpn', // Generic "VPN" keyword
  65. 'wireguard', // WireGuard VPN
  66. 'openvpn', // OpenVPN VPN
  67. 'softether', // SoftEther VPN
  68. ];
  69. // 获取当前网络类型有没有包含VPN
  70. Future<bool> isVPN() async {
  71. try {
  72. final interfaces = await NetworkInterface.list();
  73. bool isIosDevice = Platform.isIOS;
  74. return interfaces.any((interface) {
  75. return _commonVpnInterfaceNamePatterns.any((pattern) {
  76. if (isIosDevice &&
  77. (interface.name.toLowerCase().contains('ipsec') ||
  78. interface.name.toLowerCase().contains('utun6') ||
  79. interface.name.toLowerCase().contains('ikev2') ||
  80. interface.name.toLowerCase().contains('l2tp'))) {
  81. return false;
  82. }
  83. return interface.name.toLowerCase().contains(pattern);
  84. });
  85. });
  86. } catch (e) {
  87. return false;
  88. }
  89. }
  90. // 监听网络状态变化
  91. Stream<List<ConnectivityResult>> onConnectivityChanged() {
  92. return _connectivity.onConnectivityChanged;
  93. }
  94. }