From bfad0dc1761ceb9f322add2cedd77c43ad975d21 Mon Sep 17 00:00:00 2001 From: wgh19 Date: Thu, 16 May 2024 17:54:17 +0800 Subject: [PATCH] fix proxy --- ios/Runner/AppDelegate.swift | 2 +- lib/network/app_dio.dart | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index 81c3805..3819609 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -17,7 +17,7 @@ import Flutter let proxyConfig = "\(host):\(port)" result(proxyConfig) } else { - result("") + result("no proxy") } } diff --git a/lib/network/app_dio.dart b/lib/network/app_dio.dart index c9bf186..21d3a09 100644 --- a/lib/network/app_dio.dart +++ b/lib/network/app_dio.dart @@ -125,23 +125,22 @@ class AppDio extends DioForNative { } void setSystemProxy() { - HttpOverrides.global = _ProxyHttpOverrides() - ..findProxy(Uri()); + HttpOverrides.global = _ProxyHttpOverrides()..findProxy(Uri()); } class _ProxyHttpOverrides extends HttpOverrides { String proxy = "DIRECT"; String findProxy(Uri uri) { - var haveUserProxy = appdata.settings["proxy"] != null - && appdata.settings["proxy"].toString().isNotEmpty; - if(!App.isLinux && !haveUserProxy){ + var haveUserProxy = appdata.settings["proxy"] != null && + appdata.settings["proxy"].toString().removeAllBlank.isNotEmpty; + if (!App.isLinux && !haveUserProxy) { var channel = const MethodChannel("pixes/proxy"); channel.invokeMethod("getProxy").then((value) { - if(value.toString().toLowerCase() == "no proxy"){ + if (value.toString().toLowerCase() == "no proxy") { proxy = "DIRECT"; } else { - if(proxy.contains("https")){ + if (proxy.contains("https")) { var proxies = value.split(";"); for (String proxy in proxies) { proxy = proxy.removeAllBlank; @@ -154,10 +153,20 @@ class _ProxyHttpOverrides extends HttpOverrides { } }); } else { - if(haveUserProxy){ + if (haveUserProxy) { proxy = "PROXY ${appdata.settings["proxy"]}"; } } + // check validation + if (proxy.startsWith("PROXY")) { + var uri = proxy.replaceFirst("PROXY", "").removeAllBlank; + if (!uri.startsWith("http")) { + uri += "http://"; + } + if (!uri.isURL) { + return "DIRECT"; + } + } return proxy; }