diff --git a/lib/main.dart b/lib/main.dart index 58ea22a2..10c679a2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -52,6 +52,9 @@ void main() async { ], ); } + if (GStorage.badCertificateCallback) { + HttpOverrides.global = _CustomHttpOverrides(); + } await setupServiceLocator(); Request(); await Request.setCookie(); @@ -275,3 +278,12 @@ class MyApp extends StatelessWidget { ); } } + +class _CustomHttpOverrides extends HttpOverrides { + @override + HttpClient createHttpClient(SecurityContext? context) { + return super.createHttpClient(context) + ..badCertificateCallback = + (X509Certificate cert, String host, int port) => true; + } +} diff --git a/lib/pages/setting/extra_setting.dart b/lib/pages/setting/extra_setting.dart index fe3fd649..fe23231a 100644 --- a/lib/pages/setting/extra_setting.dart +++ b/lib/pages/setting/extra_setting.dart @@ -408,6 +408,13 @@ class _ExtraSettingState extends State { setKey: SettingBoxKey.reverseFromFirst, defaultVal: true, ), + SetSwitchItem( + title: '禁用 SSL 证书验证', + subTitle: '谨慎开启,禁用容易受到中间人攻击', + leading: Icon(Icons.security), + needReboot: true, + setKey: SettingBoxKey.badCertificateCallback, + ), Obx( () => ListTile( enableFeedback: true, diff --git a/lib/pages/setting/widgets/switch_item.dart b/lib/pages/setting/widgets/switch_item.dart index 00bdfe7a..11a56f51 100644 --- a/lib/pages/setting/widgets/switch_item.dart +++ b/lib/pages/setting/widgets/switch_item.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:PiliPalaX/utils/storage.dart'; +import 'package:get/get.dart'; class SetSwitchItem extends StatefulWidget { final String? title; @@ -43,7 +44,45 @@ class _SetSwitchItemState extends State { } void switchChange(value) async { + if (widget.setKey == SettingBoxKey.badCertificateCallback && + (value ?? !val)) { + showDialog( + context: context, + builder: (context) => AlertDialog( + title: Text( + '确定禁用 SSL 证书验证?', + style: TextStyle(fontSize: 18), + ), + content: const Text('禁用容易受到中间人攻击'), + actions: [ + TextButton( + onPressed: Get.back, + child: Text( + '取消', + style: TextStyle( + color: Theme.of(context).colorScheme.outline, + ), + ), + ), + TextButton( + onPressed: () async { + Get.back(); + await GStorage.setting + .put(SettingBoxKey.badCertificateCallback, true); + val = true; + SmartDialog.showToast('重启生效'); + setState(() {}); + }, + child: const Text('确认'), + ), + ], + ), + ); + return; + } + val = value ?? !val; + await GStorage.setting.put(widget.setKey, val); // if (widget.setKey == SettingBoxKey.autoUpdate && value == true) { // Utils.checkUpdate(); diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart index ae6bb732..b2c3e4dd 100644 --- a/lib/utils/storage.dart +++ b/lib/utils/storage.dart @@ -160,6 +160,9 @@ class GStorage { static double get subtitleBgOpaticy => setting.get(SettingBoxKey.subtitleBgOpaticy, defaultValue: 0.67); + static bool get badCertificateCallback => + setting.get(SettingBoxKey.badCertificateCallback, defaultValue: false); + static List get dynamicDetailRatio => List.from(setting .get(SettingBoxKey.dynamicDetailRatio, defaultValue: [60.0, 40.0])); @@ -366,6 +369,7 @@ class SettingBoxKey { subtitlePaddingH = 'subtitlePaddingH', subtitlePaddingB = 'subtitlePaddingB', subtitleBgOpaticy = 'subtitleBgOpaticy', + badCertificateCallback = 'badCertificateCallback', // Sponsor Block enableSponsorBlock = 'enableSponsorBlock',