問(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證書(shū)校驗(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è)抓包的自簽名證書(shū)豪墅,會(huì)通不過(guò)證書(shū)校驗(yàn),所以我們禁用證書(shū)校驗(yàn)
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
};
}
(2)ip
和 port
肯定不能通過(guò)硬編碼的方式寫(xiě)在代碼中黔寇,所以我們通過(guò)路由傳值的方式將 port
和 ip
從原生傳到 flutter
組件偶器;
獲取當(dāng)前代理 ip
和 port
(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 接收 ip
和 port
參數(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();
}
}