Flutter 組件抓包問(wèn)題

問(wèn)題詳情: flutter 組件未進(jìn)行特殊設(shè)置的情況下不會(huì)走代理,無(wú)法被抓包;

解決方案:

(1)在 dio 網(wǎng)絡(luò)請(qǐng)求組件中設(shè)置代理的 ip 地址和 port,強(qiáng)制使用代理請(qǐng)求網(wǎng)絡(luò);


    class ProxyUtils {
      // 是否啟用代理
      static bool PROXY_ENABLE = false;
      /// 代理服務(wù)IP
      static String PROXY_IP = '172.0.0.1';
      /// 代理服務(wù)端口
      static int PROXY_PORT = 8888;  
    }

    // 在調(diào)試模式下需要抓包調(diào)試,所以我們使用代理,并禁用HTTPS證書校驗(yàn)
    if (ProxyUtils.PROXY_ENABLE) {
      (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
          (client) {
        client.findProxy = (uri) {
          var ip = ProxyUtils.PROXY_IP;
          var port = ProxyUtils.PROXY_PORT;
          return "PROXY $ip:$port";
        };
        // 代理工具會(huì)提供一個(gè)抓包的自簽名證書,會(huì)通不過(guò)證書校驗(yàn),所以我們禁用證書校驗(yàn)
        client.badCertificateCallback =
            (X509Certificate cert, String host, int port) => true;
      };
    }

(2)ipport 肯定不能通過(guò)硬編碼的方式寫在代碼中,所以我們通過(guò)路由傳值的方式將 portip 從原生傳到 flutter 組件;

獲取當(dāng)前代理 ipport (iOS)

// 自動(dòng)獲取手機(jī)代理
NSString *portalBaseUrlStr = @"http://www.baidu.com";
NSDictionary *proxySettings = (__bridge NSDictionary *)(CFNetworkCopySystemProxySettings());
NSArray *proxies = (__bridge NSArray *)(CFNetworkCopyProxiesForURL((__bridge CFURLRef _Nonnull)([NSURL URLWithString:portalBaseUrlStr]), (__bridge CFDictionaryRef _Nonnull)(proxySettings)));
NSDictionary *settings = [proxies firstObject];
NSString *hostName = [NSString stringWithFormat:@"%@",settings[@"kCFProxyHostNameKey"]];
NSString *portName = [NSString stringWithFormat:@"%@",settings[@"kCFProxyPortNumberKey"]];
// 獲取為空時(shí)居然是字符串"(null)"
if ([hostName isEqualToString:@"(null)"]) {
    hostName = @"";
}
if ([portName isEqualToString:@"(null)"]) {
    portName = @"";
}
NSString *proxy_ip = hostName;
NSString *proxy_port = portName;
通過(guò)路由傳遞 ip 和 port(iOS):

FlutterEngine *flutterEngine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil];
        
if ([SystemInfoUtils isProxy]) {
    NSString *route = [NSString stringWithFormat:@"wallpaper?port=%@&ip=%@", [SystemInfoUtils proxy_port],[SystemInfoUtils proxy_ip]];
    [self.flutterEngine runWithEntrypoint:nil initialRoute:route];
} else {
    [self.flutterEngine runWithEntrypoint:nil];
}

flutter 接收 ipport 參數(shù):


void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        primaryColor: Colors.white,
      ),
      home: _route(window.defaultRouteName),
      builder: EasyLoading.init(),
    );
  }
}

Widget _route(String url) {
  // 解析路由參數(shù)
  Uri u = Uri.parse(url);
  Map<String, String> qp = u.queryParameters;

  final port = qp["port"];
  final ip = qp["ip"];

  if (port != null && ip != null) {
    ProxyUtils.PROXY_ENABLE = true;
    ProxyUtils.PROXY_IP = ip;
    ProxyUtils.PROXY_PORT = int.parse(port);

    print("Flutter config proxy ip is $ip port is $port");
  }

  //跳轉(zhuǎn)頁(yè)面
  switch (u.path) {
    case 'wallpaper':
      return WallpaperViewController();
    default:
      return WallpaperViewController();
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容