需要获取手机IP地址设置才能抓包,获取IP地址,需要跟原生通讯获取,
1:获取IP地址
安卓代码:
/** * 原生和flutter通讯交互 */ class MainActivity : FlutterActivity() { var methodChannel: MethodChannel? = null override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) methodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "channelName") methodChannel!!.setMethodCallHandler { call: MethodCall, result: MethodChannel.Result -> //判断方法名是否支持 if (call.method == "getProxy") { result.success(getProxy()) } else { //方法名暂不支持 result.notImplemented() } } } /** * 获取代理 */ private fun getProxy(): String { val proxyAddress = System.getProperty("http.proxyHost", "") val portStr = System.getProperty("http.proxyPort", "") return "$proxyAddress:$portStr" } }
flutter代码:
getProxyUrl() async { const platform = MethodChannel("channelName"); String result = await platform.invokeMethod("getProxy"); debugPrint('----------result $result'); return result; }
2:设置IP
void _switchProxy(bool enable, String? proxy) { /// 设置代理仅在启动的时候生效,所以仅设置开启即可,无需设置关闭,默认就是不开启代理 if (!enable) { return; } if (proxy == null || proxy.isEmpty || proxy == ":") { return; } //https证书校验 (_dio?.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { //非 debug 环境才配置代理 debugPrint("DioManager setProxy enable:$enable proxy:$proxy"); client.findProxy = (uri) { return "PROXY $proxy"; }; client.badCertificateCallback = (X509Certificate cert, String host, int port) { //全部通过 return true; }; _isProxyEnable = true; return null; }; }
//调用
String proxyUrl= await dioNetwork.getProxyUrl();
_switchProxy(kReleaseMode,proxyUrl );