From 13ce50f730aba6a2984faada001749abca211f6f Mon Sep 17 00:00:00 2001 From: guozhigq Date: Mon, 6 Nov 2023 00:04:54 +0800 Subject: [PATCH] =?UTF-8?q?mod:=20=E4=BB=A3=E7=90=86=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/http/init.dart | 36 +++++++++ lib/pages/setting/extra_setting.dart | 111 +++++++++++++++++++++++++++ lib/utils/proxy.dart | 28 +++++++ lib/utils/storage.dart | 5 ++ 4 files changed, 180 insertions(+) create mode 100644 lib/utils/proxy.dart diff --git a/lib/http/init.dart b/lib/http/init.dart index dbf189c6..e2b3cd1f 100644 --- a/lib/http/init.dart +++ b/lib/http/init.dart @@ -4,6 +4,7 @@ import 'dart:io'; import 'dart:async'; import 'package:dio/dio.dart'; import 'package:cookie_jar/cookie_jar.dart'; +import 'package:dio/io.dart'; import 'package:dio_http2_adapter/dio_http2_adapter.dart'; import 'package:hive/hive.dart'; import 'package:pilipala/utils/storage.dart'; @@ -17,6 +18,11 @@ class Request { static late CookieManager cookieManager; static late final Dio dio; factory Request() => _instance; + Box setting = GStrorage.setting; + static Box localCache = GStrorage.localCache; + late dynamic enableSystemProxy; + late String systemProxyHost; + late String systemProxyPort; /// 设置cookie static setCookie() async { @@ -92,6 +98,13 @@ class Request { headers: {}, ); + enableSystemProxy = + setting.get(SettingBoxKey.enableSystemProxy, defaultValue: false); + systemProxyHost = + localCache.get(LocalCacheKey.systemProxyHost, defaultValue: ''); + systemProxyPort = + localCache.get(LocalCacheKey.systemProxyPort, defaultValue: ''); + dio = Dio(options) /// fix 第三方登录 302重定向 跟iOS代理问题冲突 @@ -100,6 +113,29 @@ class Request { idleTimeout: const Duration(milliseconds: 10000), onClientCreate: (_, config) => config.onBadCertificate = (_) => true, ), + ) + + /// 设置代理 + ..httpClientAdapter = IOHttpClientAdapter( + createHttpClient: () { + final client = HttpClient(); + // Config the client. + client.findProxy = (uri) { + if (enableSystemProxy) { + print('🌹:$systemProxyHost'); + print('🌹:$systemProxyPort'); + + // return 'PROXY host:port'; + return 'PROXY $systemProxyHost:$systemProxyPort'; + } else { + // 不设置代理 + return 'DIRECT'; + } + }; + client.badCertificateCallback = + (X509Certificate cert, String host, int port) => true; + return client; + }, ); //添加拦截器 diff --git a/lib/pages/setting/extra_setting.dart b/lib/pages/setting/extra_setting.dart index 4bbb841c..6f1ff07e 100644 --- a/lib/pages/setting/extra_setting.dart +++ b/lib/pages/setting/extra_setting.dart @@ -16,8 +16,12 @@ class ExtraSetting extends StatefulWidget { class _ExtraSettingState extends State { Box setting = GStrorage.setting; + static Box localCache = GStrorage.localCache; late dynamic defaultReplySort; late dynamic defaultDynamicType; + late dynamic enableSystemProxy; + late String defaultSystemProxyHost; + late String defaultSystemProxyPort; @override void initState() { @@ -28,6 +32,86 @@ class _ExtraSettingState extends State { // 优先展示全部动态 all defaultDynamicType = setting.get(SettingBoxKey.defaultDynamicType, defaultValue: 0); + enableSystemProxy = + setting.get(SettingBoxKey.enableSystemProxy, defaultValue: false); + defaultSystemProxyHost = + localCache.get(LocalCacheKey.systemProxyHost, defaultValue: ''); + defaultSystemProxyPort = + localCache.get(LocalCacheKey.systemProxyPort, defaultValue: ''); + } + + // 设置代理 + void twoFADialog() { + var systemProxyHost = ''; + var systemProxyPort = ''; + + SmartDialog.show( + useSystem: true, + animationType: SmartAnimationType.centerFade_otherSlide, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('设置代理'), + content: Column( + mainAxisSize: MainAxisSize.min, + children: [ + const SizedBox(height: 6), + TextField( + decoration: InputDecoration( + isDense: true, + labelText: defaultSystemProxyHost != '' + ? defaultSystemProxyHost + : '请输入Host,使用 . 分割', + border: OutlineInputBorder( + borderRadius: BorderRadius.circular(6.0), + ), + hintText: defaultSystemProxyHost, + ), + onChanged: (e) { + systemProxyHost = e; + }, + ), + const SizedBox(height: 10), + TextField( + keyboardType: TextInputType.number, + decoration: InputDecoration( + isDense: true, + labelText: defaultSystemProxyPort != '' + ? defaultSystemProxyPort + : '请输入Port', + border: OutlineInputBorder( + borderRadius: BorderRadius.circular(6.0), + ), + hintText: defaultSystemProxyPort, + ), + onChanged: (e) { + systemProxyPort = e; + }, + ), + ], + ), + actions: [ + TextButton( + onPressed: () async { + SmartDialog.dismiss(); + }, + child: Text( + '取消', + style: TextStyle(color: Theme.of(context).colorScheme.outline), + ), + ), + TextButton( + onPressed: () async { + localCache.put(LocalCacheKey.systemProxyHost, systemProxyHost); + localCache.put(LocalCacheKey.systemProxyPort, systemProxyPort); + SmartDialog.dismiss(); + // Request.dio; + }, + child: const Text('确认'), + ) + ], + ); + }, + ); } @override @@ -135,6 +219,33 @@ class _ExtraSettingState extends State { ], ), ), + ListTile( + enableFeedback: true, + onTap: () => twoFADialog(), + title: Text('设置代理', style: titleStyle), + subtitle: Text('设置代理 host:port', style: subTitleStyle), + trailing: Transform.scale( + scale: 0.8, + child: Switch( + thumbIcon: MaterialStateProperty.resolveWith( + (Set states) { + if (states.isNotEmpty && + states.first == MaterialState.selected) { + return const Icon(Icons.done); + } + return null; // All other states will use the default thumbIcon. + }), + value: enableSystemProxy, + onChanged: (val) { + setting.put( + SettingBoxKey.enableSystemProxy, !enableSystemProxy); + setState(() { + enableSystemProxy = !enableSystemProxy; + }); + }, + ), + ), + ), const SetSwitchItem( title: '检查更新', subTitle: '每次启动时检查是否需要更新', diff --git a/lib/utils/proxy.dart b/lib/utils/proxy.dart new file mode 100644 index 00000000..cba74c42 --- /dev/null +++ b/lib/utils/proxy.dart @@ -0,0 +1,28 @@ +import 'dart:io'; +import 'package:system_proxy/system_proxy.dart'; + +class CustomProxy { + init() async { + Map? proxy = await SystemProxy.getProxySettings(); + if (proxy != null) { + HttpOverrides.global = + ProxiedHttpOverrides(proxy['host']!, proxy['port']!); + } + } +} + +class ProxiedHttpOverrides extends HttpOverrides { + final String _port; + final String _host; + + ProxiedHttpOverrides(this._host, this._port); + + @override + HttpClient createHttpClient(SecurityContext? context) { + return super.createHttpClient(context) + // set proxy + ..findProxy = (uri) { + return "PROXY $_host:$_port;"; + }; + } +} diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart index dc00ec43..e0e240a1 100644 --- a/lib/utils/storage.dart +++ b/lib/utils/storage.dart @@ -125,6 +125,7 @@ class SettingBoxKey { static const String enableSearchWord = 'enableSearchWord'; static const String enableRcmdDynamic = 'enableRcmdDynamic'; static const String enableSaveLastData = 'enableSaveLastData'; + static const String enableSystemProxy = 'enableSystemProxy'; /// 外观 static const String themeMode = 'themeMode'; @@ -154,6 +155,10 @@ class LocalCacheKey { static const String danmakuOpacity = 'danmakuOpacity'; static const String danmakuFontScale = 'danmakuFontScale'; static const String danmakuSpeed = 'danmakuSpeed'; + + // 代理host port + static const String systemProxyHost = 'systemProxyHost'; + static const String systemProxyPort = 'systemProxyPort'; } class VideoBoxKey {