mod: 代理设置
This commit is contained in:
@ -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;
|
||||
},
|
||||
);
|
||||
|
||||
//添加拦截器
|
||||
|
@ -16,8 +16,12 @@ class ExtraSetting extends StatefulWidget {
|
||||
|
||||
class _ExtraSettingState extends State<ExtraSetting> {
|
||||
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<ExtraSetting> {
|
||||
// 优先展示全部动态 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<ExtraSetting> {
|
||||
],
|
||||
),
|
||||
),
|
||||
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<Icon?>(
|
||||
(Set<MaterialState> 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: '每次启动时检查是否需要更新',
|
||||
|
28
lib/utils/proxy.dart
Normal file
28
lib/utils/proxy.dart
Normal file
@ -0,0 +1,28 @@
|
||||
import 'dart:io';
|
||||
import 'package:system_proxy/system_proxy.dart';
|
||||
|
||||
class CustomProxy {
|
||||
init() async {
|
||||
Map<String, String>? 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;";
|
||||
};
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user