From 65b41b287323f09f4c2ad9f08ec2ba0ddd4fe383 Mon Sep 17 00:00:00 2001 From: boa <42885162+boa-z@users.noreply.github.com> Date: Thu, 14 Nov 2024 20:40:28 +0800 Subject: [PATCH] add option to ignore certificate errors (#46) add option to ignore certificate errors --- assets/translation.json | 6 ++++-- lib/foundation/appdata.dart | 1 + lib/network/app_dio.dart | 19 +++++++++++++------ lib/pages/settings/network.dart | 17 +++++++++++++---- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/assets/translation.json b/assets/translation.json index 9917cd2..14604bb 100644 --- a/assets/translation.json +++ b/assets/translation.json @@ -209,7 +209,8 @@ "Update Comics Info": "更新漫画信息", "Create Folder": "新建文件夹", "Select an image on screen": "选择屏幕上的图片", - "Added @count comics to download queue.": "已添加 @count 本漫画到下载队列" + "Added @count comics to download queue.": "已添加 @count 本漫画到下载队列", + "Ignore Certificate Errors": "忽略证书错误" }, "zh_TW": { "Home": "首頁", @@ -421,6 +422,7 @@ "Update Comics Info": "更新漫畫信息", "Create Folder": "新建文件夾", "Select an image on screen": "選擇屏幕上的圖片", - "Added @count comics to download queue.": "已添加 @count 本漫畫到下載隊列" + "Added @count comics to download queue.": "已添加 @count 本漫畫到下載隊列", + "Ignore Certificate Errors": "忽略證書錯誤" } } \ No newline at end of file diff --git a/lib/foundation/appdata.dart b/lib/foundation/appdata.dart index afd1522..1cc7ed3 100644 --- a/lib/foundation/appdata.dart +++ b/lib/foundation/appdata.dart @@ -119,6 +119,7 @@ class _Settings with ChangeNotifier { 'quickFavorite': null, 'enableTurnPageByVolumeKey': true, 'enableClockAndBatteryInfoInReader': true, + 'ignoreCertificateErrors': false, }; operator [](String key) { diff --git a/lib/network/app_dio.dart b/lib/network/app_dio.dart index 4608952..26db4da 100644 --- a/lib/network/app_dio.dart +++ b/lib/network/app_dio.dart @@ -106,6 +106,7 @@ class MyLogInterceptor implements Interceptor { class AppDio with DioMixin { String? _proxy = proxy; + static bool get ignoreCertificateErrors => appdata.settings['ignoreCertificateErrors'] == true; AppDio([BaseOptions? options]) { this.options = options ?? BaseOptions(); @@ -123,6 +124,7 @@ class AppDio with DioMixin { client.idleTimeout = const Duration(seconds: 100); client.badCertificateCallback = (X509Certificate cert, String host, int port) { + if (ignoreCertificateErrors) return true; if (host.contains("cdn")) return true; final ipv4RegExp = RegExp( r'^((25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})$'); @@ -189,8 +191,8 @@ class AppDio with DioMixin { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - if(options?.headers?['prevent-parallel'] == 'true') { - while(_requests.containsKey(path)) { + if (options?.headers?['prevent-parallel'] == 'true') { + while (_requests.containsKey(path)) { await Future.delayed(const Duration(milliseconds: 20)); } _requests[path] = true; @@ -204,6 +206,9 @@ class AppDio with DioMixin { proxySettings: proxy == null ? const rhttp.ProxySettings.noProxy() : rhttp.ProxySettings.proxy(proxy!), + tlsSettings: rhttp.TlsSettings( + verifyCertificates: !ignoreCertificateErrors, + ), )); } try { @@ -216,9 +221,8 @@ class AppDio with DioMixin { onSendProgress: onSendProgress, onReceiveProgress: onReceiveProgress, ); - } - finally { - if(_requests.containsKey(path)) { + } finally { + if (_requests.containsKey(path)) { _requests.remove(path); } } @@ -237,6 +241,9 @@ class RHttpAdapter implements HttpClientAdapter { keepAlivePing: Duration(seconds: 30), ), throwOnStatusCode: false, + tlsSettings: rhttp.TlsSettings( + verifyCertificates: !AppDio.ignoreCertificateErrors, + ), ); } @@ -284,7 +291,7 @@ class RHttpAdapter implements HttpClientAdapter { headers[key]!.add(entry.$2); } var data = res.body; - if(headers['content-encoding']?.contains('gzip') ?? false) { + if (headers['content-encoding']?.contains('gzip') ?? false) { // rhttp does not support gzip decoding data = gzip.decoder.bind(data).map((data) => Uint8List.fromList(data)); } diff --git a/lib/pages/settings/network.dart b/lib/pages/settings/network.dart index 21042ea..9396b7e 100644 --- a/lib/pages/settings/network.dart +++ b/lib/pages/settings/network.dart @@ -38,14 +38,11 @@ class _ProxySettingView extends StatefulWidget { class _ProxySettingViewState extends State<_ProxySettingView> { String type = ''; - String host = ''; - String port = ''; - String username = ''; - String password = ''; + bool ignoreCertificateErrors = false; // USERNAME:PASSWORD@HOST:PORT String toProxyStr() { @@ -103,6 +100,7 @@ class _ProxySettingViewState extends State<_ProxySettingView> { void initState() { var proxy = appdata.settings['proxy']; parseProxyString(proxy); + ignoreCertificateErrors = appdata.settings['ignoreCertificateErrors'] ?? false; super.initState(); } @@ -148,6 +146,17 @@ class _ProxySettingViewState extends State<_ProxySettingView> { }, ), if (type == 'manual') buildManualProxy(), + SwitchListTile( + title: Text("Ignore Certificate Errors".tl), + value: ignoreCertificateErrors, + onChanged: (v) { + setState(() { + ignoreCertificateErrors = v; + }); + appdata.settings['ignoreCertificateErrors'] = ignoreCertificateErrors; + appdata.saveData(); + }, + ), ], ), ),