flutter完美抓包解决方案

github地址
pub地址

集成方法:

flutter_packet_capture: ^1.0.0
//scheme配置
Packetcapture.setConfig(schemeConfig: 'capture://',modeConfig: "debug");
  Packetcapture.initUniLinks(callBack: (hostPort){
    //设置代理
    Network.setHttpProxy(hostPort);
  });

抓包的三种方法:

方法一:直接在dio里面设置ip以及端口(缺点:代理只能写死,不灵活)

(_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
          (client) {
        //这一段是解决安卓https抓包的问题
        client.badCertificateCallback =
            (X509Certificate cert, String host, int port) {
          return Platform.isAndroid;
        };
        client.findProxy = (uri) {
          return "PROXY 代理ip:代理port";
        };
      };

方法二:写原生插件获取手代理ip和代理端口(缺点:安卓好像无法获取)
ios原生代码:

 //自动获取手机代理
  NSDictionary *proxySettings = (__bridge NSDictionary *)(CFNetworkCopySystemProxySettings());
           NSArray *proxies = (__bridge NSArray *)(CFNetworkCopyProxiesForURL((__bridge CFURLRef _Nonnull)([NSURL URLWithString:call.arguments]), (__bridge CFDictionaryRef _Nonnull)(proxySettings)));
           NSString *hostName = proxySettings[@"HTTPSProxy"];
           NSString *portName = [NSString stringWithFormat:@"%@",proxySettings[@"HTTPPort"]];
           long HTTPEnable = [proxySettings[@"HTTPEnable"] longValue];
           if (HTTPEnable==0) {
               hostName = @"";
           }

方法三:采用scheme协议的方式传入代理ip和代理端口(完美解决iOS,安卓抓包问题)

1,注册自己的URL Scheme,例如:scheme://
2,定义参数规则,例如:scheme://xxxx?host=10.0.206.163&port=8888 (port可不要,默认8888)
3,flutter监听解析参数,并在dio里面设置代理
4,使用草料生成一个二维码:内容:scheme://xxxx?host=10.0.206.163

然后使用原生相机扫码进入app就可以抓包啦,这里需要注意需要先打开app再扫码进入app才行

flutter代码:

 Future initUniLinks() async {
   
    // 监听插件scheme数据
      getLinksStream().listen((String link) {
        link =  Uri.decodeComponent(link);
        if(link.contains("scheme://")){
          String type = getTypeStr(link);
          String param = link.replaceAll("scheme://$type?", "");
          Map dict = getUrlParams(param);
          if(type=="tiaoshi"){//设置抓包代理
            String host = dict["host"];
            String port = dict["port"];
            //这里是网络请求封装
            Net.setHttpProxy(host,port==null?"8888":port);
           }
        }
      // Parse the link and warn the user, if it is not correct
    }, onError: (err) {
      // Handle exception by warning the user their action did not succeed
    });
  }

//获取scheme 要处理的业务类型
  String getTypeStr(String link){
    List params = link.split("?");
    String typeStr = params[0];
    typeStr =  typeStr.replaceAll("scheme://", "");
    return typeStr;
  }

//url参数转map
  Map getUrlParams(String paramStr) {
    Map map = Map();
    List params = paramStr.split("&");
    for(int i=0;i

Net里面的关键代码

//设置代理
 static void setHttpProxy(String host,String port) {
    Application.httpProxy = host+':'+port;
    _initDio();
  }

static Future _initDio() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      _androidInfo = await deviceInfo.androidInfo;
    } else if (Platform.isIOS) {
      _iosInfo = await deviceInfo.iosInfo;
    }
    
    _dio = Dio(BaseOptions(
      contentType: 'application/json',
      baseUrl: Config.BASE_URL,
    ));
    _dio.options.receiveTimeout = 5000;
    _dio.options.connectTimeout = 10000;

    if (Application.httpProxy.length != 0) {
      (_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
          (client) {
        //这一段是解决安卓https抓包的问题
        client.badCertificateCallback =
            (X509Certificate cert, String host, int port) {
          return Platform.isAndroid;
        };
       //这是抓包代理
        client.findProxy = (uri) {
          return "PROXY ${Application.httpProxy}";
        };
      };
    }
    _dio.interceptors.addAll([
      InterceptorsWrapper(
        onRequest: (Options options) {
          options.headers['DeviceName'] = 'xxxx';
          return options;
        },
        onResponse: (Response res) {
          try {

          ...

            return res;
          } catch (e) {
            return res;
          }
        },
        onError: (DioError e) {
          print(e);
         }
              break;
            default:
          }
          return e;
        },
      ),
    ]);
  }

static Future get(
    String path, {
    Map queryParameters,
    Options options,
    CancelToken cancelToken,
    void Function(int, int) onReceiveProgress,
  }) async {
    if (_dio == null) {
      await _initDio();
    }

    final res = await _dio.get(
      path,
      queryParameters: queryParameters,
      options: options,
      cancelToken: cancelToken,
      onReceiveProgress: onReceiveProgress,
    );

    return res.data;
  }

  static Future post(
    String path, {
    dynamic data,
    Map queryParameters,
    Options options,
    CancelToken cancelToken,
    void Function(int, int) onSendProgress,
    void Function(int, int) onReceiveProgress,
  }) async {
    if (_dio == null) {
      await _initDio();
    }
    final res = await _dio.post(
      path,
      data: data,
      queryParameters: queryParameters,
      options: options,
      cancelToken: cancelToken,
      onSendProgress: onSendProgress,
      onReceiveProgress: onReceiveProgress,
    );

    return res.data;
  }

如有更好的办法请指正

你可能感兴趣的:(flutter完美抓包解决方案)