debugHttpOverride.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * @Author : WuWei
  3. * @LastEditors : WuWei
  4. * @Date : 2021-03-15 10:04:58
  5. * @LastEditTime : 2021-09-13 15:51:21
  6. * @FilePath : /wisdom-huiguanjia-app/app_project/lib/debugHttpOverride.dart
  7. * @Description : createHttpClient方法配置自己的ip和自己在抓包软件设置的端口
  8. */
  9. import 'dart:io';
  10. class MyHttpOverrides extends HttpOverrides {
  11. @override
  12. HttpClient createHttpClient(SecurityContext? context) {
  13. var httpClient = super.createHttpClient(context);
  14. // 设置代理
  15. httpClient.findProxy = (uri) {
  16. if (ProxyManager.isProxyEnabled && ProxyManager.proxyIP != null) {
  17. return 'PROXY ${ProxyManager.proxyIP}:${ProxyManager.proxyPort}';
  18. }
  19. return 'DIRECT';
  20. };
  21. // 允许自签名证书(仅用于开发环境)
  22. httpClient.badCertificateCallback =
  23. (X509Certificate cert, String host, int port) => true;
  24. return httpClient;
  25. }
  26. }
  27. class ProxyManager {
  28. static String? _proxyIP;
  29. static int _proxyPort = 52555;
  30. static bool _isProxyEnabled = false;
  31. // 获取代理状态
  32. static bool get isProxyEnabled => _isProxyEnabled;
  33. static String? get proxyIP => _proxyIP;
  34. static int get proxyPort => _proxyPort;
  35. // 初始化代理设置
  36. static Future<void> initialize() async {
  37. try {
  38. // 尝试获取代理IP和端口
  39. await _detectProxySettings();
  40. print('Proxy initialized - IP: $_proxyIP, Port: $_proxyPort');
  41. } catch (e) {
  42. print('Failed to initialize proxy: $e');
  43. _isProxyEnabled = false;
  44. }
  45. }
  46. // 检测代理设置
  47. static Future<void> _detectProxySettings() async {
  48. try {
  49. // 获取系统代理设置
  50. if (Platform.isIOS || Platform.isAndroid) {
  51. // 移动设备:获取局域网中的代理服务器IP
  52. _proxyIP = await _getLocalNetworkIP();
  53. } else {
  54. // 桌面设备:使用localhost
  55. _proxyIP = '127.0.0.1';
  56. }
  57. // 测试代理连接
  58. if (_proxyIP != null) {
  59. final isProxyAvailable = await _testProxyConnection(_proxyIP!, _proxyPort);
  60. _isProxyEnabled = isProxyAvailable;
  61. }
  62. } catch (e) {
  63. print('Error detecting proxy settings: $e');
  64. _isProxyEnabled = false;
  65. }
  66. }
  67. // 获取局域网IP地址
  68. static Future<String?> _getLocalNetworkIP() async {
  69. try {
  70. final interfaces = await NetworkInterface.list();
  71. for (var interface in interfaces) {
  72. for (var addr in interface.addresses) {
  73. if (addr.type == InternetAddressType.IPv4) {
  74. final ip = addr.address;
  75. if (ip.startsWith('192.168.') ||
  76. ip.startsWith('10.') ||
  77. (ip.startsWith('172.') &&
  78. int.parse(ip.split('.')[1]) >= 16 &&
  79. int.parse(ip.split('.')[1]) <= 31)) {
  80. if (!ip.startsWith('127.')) {
  81. return ip;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. } catch (e) {
  88. print('Error getting local network IP: $e');
  89. }
  90. return null;
  91. }
  92. // 测试代理连接
  93. static Future<bool> _testProxyConnection(String ip, int port) async {
  94. try {
  95. final socket = await Socket.connect(ip, port,
  96. timeout: Duration(seconds: 2));
  97. await socket.close();
  98. return true;
  99. } catch (e) {
  100. print('Proxy connection test failed: $e');
  101. return false;
  102. }
  103. }
  104. // 更新代理设置
  105. static Future<void> updateProxySettings() async {
  106. await _detectProxySettings();
  107. }
  108. }