From f11ec29943d87460bcdf77787bba2f9410b7c83e Mon Sep 17 00:00:00 2001 From: honjow Date: Sun, 18 Jul 2021 00:12:39 +0800 Subject: [PATCH] Fix: fix domain fronting --- lib/common/global.dart | 7 ++++ lib/common/service/base_service.dart | 4 ++ lib/common/service/dns_service.dart | 37 ++++++++---------- lib/pages/gallery/view/gallery_widget.dart | 34 +++++++++-------- lib/pages/item/gallery_item.dart | 36 +++++++++--------- lib/pages/item/gallery_item_simple.dart | 40 ++++++++++---------- lib/pages/setting/advanced_setting_page.dart | 19 ++++++---- lib/utils/dio_util.dart | 2 +- lib/utils/http_override.dart | 13 +++++++ lib/widget/network_extended_image.dart | 15 +++++++- pubspec.lock | 35 +++++++++++++++++ pubspec.yaml | 4 +- 12 files changed, 159 insertions(+), 87 deletions(-) create mode 100644 lib/utils/http_override.dart diff --git a/lib/common/global.dart b/lib/common/global.dart index 739e1bd05..3e272e843 100644 --- a/lib/common/global.dart +++ b/lib/common/global.dart @@ -14,6 +14,7 @@ import 'package:fehviewer/network/gallery_request.dart'; import 'package:fehviewer/store/floor/database.dart'; import 'package:fehviewer/store/get_store.dart'; import 'package:fehviewer/store/hive/hive.dart'; +import 'package:fehviewer/utils/http_override.dart'; import 'package:fehviewer/utils/logger.dart'; import 'package:fehviewer/utils/storage.dart'; import 'package:fehviewer/utils/utility.dart'; @@ -172,6 +173,7 @@ class Global { static late PersistCookieJar cookieJar; // static HttpProxy httpProxy = HttpProxy('localhost', '$kProxyPort'); + static DFHttpOverrides dfHttpOverrides = DFHttpOverrides(); static String appSupportPath = ''; static String appDocPath = ''; @@ -278,6 +280,11 @@ class Global { // logger.v('${profile.dnsConfig.enableCustomHosts}'); // HttpOverrides.global = httpProxy; // } + + if (profile.dnsConfig.enableDomainFronting ?? false) { + logger.d('enableDomainFronting'); + HttpOverrides.global = dfHttpOverrides; + } } static void _initProfile() { diff --git a/lib/common/service/base_service.dart b/lib/common/service/base_service.dart index bfa509f0e..54a4a5771 100644 --- a/lib/common/service/base_service.dart +++ b/lib/common/service/base_service.dart @@ -13,6 +13,10 @@ class ProfileService extends GetxService { set downloadConfig(DownloadConfig val) => Global.profile = Global.profile.copyWith(downloadConfig: val); + DnsConfig get dnsConfig => Global.profile.dnsConfig; + set dnsConfig(DnsConfig val) => + Global.profile = Global.profile.copyWith(dnsConfig: val); + Worker everProfile(RxInterface listener, ValueChanged onChange) { return ever(listener, (value) { onChange(value); diff --git a/lib/common/service/dns_service.dart b/lib/common/service/dns_service.dart index 819d28b58..9ea6fca04 100644 --- a/lib/common/service/dns_service.dart +++ b/lib/common/service/dns_service.dart @@ -53,6 +53,8 @@ class DnsService extends ProfileService { for (final host in EHConst.internalHosts.entries) { coutomHosts.putIfAbsent(host.key, () => host.value); } + } else { + return EHConst.internalHosts; } return coutomHosts; @@ -140,39 +142,30 @@ class DnsService extends ProfileService { @override void onInit() { super.onInit(); - final DnsConfig _dnsConfig = Global.profile.dnsConfig; - enableCustomHosts = _dnsConfig.enableCustomHosts ?? false; - ever(_enableCustomHosts, (bool value) { - Global.profile = Global.profile - .copyWith(dnsConfig: _dnsConfig.copyWith(enableCustomHosts: value)); - Global.saveProfile(); + enableCustomHosts = dnsConfig.enableCustomHosts ?? false; + everProfile(_enableCustomHosts, (bool value) { + dnsConfig = dnsConfig.copyWith(enableCustomHosts: value); }); - _hosts(_dnsConfig.hosts); - ever>(_hosts, (List value) { - Global.profile = - Global.profile.copyWith(dnsConfig: _dnsConfig.copyWith(hosts: value)); - Global.saveProfile(); + _hosts(dnsConfig.hosts); + everProfile>(_hosts, (List value) { + dnsConfig = dnsConfig.copyWith(hosts: value); }); - enableDoH = _dnsConfig.enableDoH ?? false; - ever(_enableDoH, (bool value) { - Global.profile = Global.profile - .copyWith(dnsConfig: _dnsConfig.copyWith(enableDoH: value)); - Global.saveProfile(); + enableDoH = dnsConfig.enableDoH ?? false; + everProfile(_enableDoH, (bool value) { + dnsConfig = dnsConfig.copyWith(enableDoH: value); }); - _dohCache(_dnsConfig.dohCache); + _dohCache(dnsConfig.dohCache); everProfile>(_dohCache, (List value) { - Global.profile = Global.profile - .copyWith(dnsConfig: _dnsConfig.copyWith(dohCache: value)); + dnsConfig = dnsConfig.copyWith(dohCache: value); }); - enableDomainFronting = _dnsConfig.enableDomainFronting ?? false; + enableDomainFronting = dnsConfig.enableDomainFronting ?? false; everProfile(_enableDomainFronting, (bool value) { - Global.profile = Global.profile.copyWith( - dnsConfig: _dnsConfig.copyWith(enableDomainFronting: value)); + dnsConfig = dnsConfig.copyWith(enableDomainFronting: value); }); } } diff --git a/lib/pages/gallery/view/gallery_widget.dart b/lib/pages/gallery/view/gallery_widget.dart index 6c9ed2247..0a69be9c0 100644 --- a/lib/pages/gallery/view/gallery_widget.dart +++ b/lib/pages/gallery/view/gallery_widget.dart @@ -1,5 +1,6 @@ -// import 'package:cached_network_image/cached_network_image.dart'; +import 'package:cached_network_image/cached_network_image.dart'; import 'package:extended_text/extended_text.dart'; +import 'package:fehviewer/common/exts.dart'; import 'package:fehviewer/common/global.dart'; import 'package:fehviewer/common/service/depth_service.dart'; import 'package:fehviewer/common/service/ehconfig_service.dart'; @@ -86,6 +87,7 @@ class CoverImage extends StatelessWidget { return Builder(builder: (_) { final Map _httpHeaders = { 'Cookie': Global.profile.user.cookie ?? '', + 'host': Uri.parse(imageUrl ?? '').host, }; if (imageUrl != null && imageUrl!.isNotEmpty) { @@ -111,23 +113,23 @@ class CoverImage extends StatelessWidget { child: Container( height: imgHeight, width: imgWidth, - // child: CachedNetworkImage( - // placeholder: (_, __) { - // return Container( - // alignment: Alignment.center, - // color: CupertinoDynamicColor.resolve( - // CupertinoColors.systemGrey5, context), - // child: const CupertinoActivityIndicator(), - // ); - // }, - // imageUrl: imageUrl ?? '', - // fit: BoxFit.cover, - // httpHeaders: _httpHeaders, - // ), - child: NetworkExtendedImage( - url: imageUrl ?? '', + child: CachedNetworkImage( + placeholder: (_, __) { + return Container( + alignment: Alignment.center, + color: CupertinoDynamicColor.resolve( + CupertinoColors.systemGrey5, context), + child: const CupertinoActivityIndicator(), + ); + }, + imageUrl: (imageUrl ?? '').dfUrl, fit: BoxFit.cover, + httpHeaders: _httpHeaders, ), + // child: NetworkExtendedImage( + // url: imageUrl ?? '', + // fit: BoxFit.cover, + // ), ), ), ), diff --git a/lib/pages/item/gallery_item.dart b/lib/pages/item/gallery_item.dart index d0c4fc16f..6c536ffbb 100644 --- a/lib/pages/item/gallery_item.dart +++ b/lib/pages/item/gallery_item.dart @@ -1,9 +1,7 @@ -// import 'package:cached_network_image/cached_network_image.dart'; -import 'package:extended_image/extended_image.dart'; -import 'package:fehviewer/common/global.dart'; +import 'package:cached_network_image/cached_network_image.dart'; import 'package:fehviewer/common/exts.dart'; +import 'package:fehviewer/common/global.dart'; import 'package:fehviewer/common/service/ehconfig_service.dart'; -import 'package:fehviewer/const/const.dart'; import 'package:fehviewer/const/theme_colors.dart'; import 'package:fehviewer/extension.dart'; import 'package:fehviewer/models/index.dart'; @@ -456,21 +454,21 @@ class CoverImg extends StatelessWidget { Widget image() { if (imgUrl.isNotEmpty) { - // return CachedNetworkImage( - // placeholder: (_, __) { - // return Container( - // alignment: Alignment.center, - // color: CupertinoDynamicColor.resolve( - // CupertinoColors.systemGrey5, context), - // child: const CupertinoActivityIndicator(), - // ); - // }, - // // height: height, - // width: width, - // httpHeaders: _httpHeaders, - // imageUrl: imgUrl.dfUrl, - // fit: BoxFit.contain, - // ); + return CachedNetworkImage( + placeholder: (_, __) { + return Container( + alignment: Alignment.center, + color: CupertinoDynamicColor.resolve( + CupertinoColors.systemGrey5, context), + child: const CupertinoActivityIndicator(), + ); + }, + // height: height, + width: width, + httpHeaders: _httpHeaders, + imageUrl: imgUrl.dfUrl, + fit: BoxFit.contain, + ); return NetworkExtendedImage( url: imgUrl, ); diff --git a/lib/pages/item/gallery_item_simple.dart b/lib/pages/item/gallery_item_simple.dart index 3ec596a56..95980d0df 100644 --- a/lib/pages/item/gallery_item_simple.dart +++ b/lib/pages/item/gallery_item_simple.dart @@ -1,4 +1,4 @@ -// import 'package:cached_network_image/cached_network_image.dart'; +import 'package:cached_network_image/cached_network_image.dart'; import 'package:fehviewer/common/global.dart'; import 'package:fehviewer/common/service/ehconfig_service.dart'; import 'package:fehviewer/const/theme_colors.dart'; @@ -11,6 +11,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:get/get.dart'; +import 'package:fehviewer/common/exts.dart'; const double kCoverImageWidth = 70.0; const double kItemWidth = 115.0; @@ -291,35 +292,36 @@ class CoverImg extends StatelessWidget { final EhConfigService _ehConfigService = Get.find(); final Map _httpHeaders = { 'Cookie': Global.profile.user.cookie ?? '', + 'host': Uri.parse(imgUrl).host, }; - if (imgUrl != null && imgUrl.isNotEmpty) { + if (imgUrl.isNotEmpty) { return Obx(() { final bool _isBlur = _ehConfigService.isGalleryImgBlur.value; return LayoutBuilder( builder: (context, constraints) { return BlurImage( isBlur: _isBlur, - // child: CachedNetworkImage( - // placeholder: (_, __) { - // return Container( - // alignment: Alignment.center, - // color: CupertinoDynamicColor.resolve( - // CupertinoColors.systemGrey5, context), - // child: const CupertinoActivityIndicator(), - // ); - // }, - // height: (height ?? 0) * constraints.maxWidth / (width ?? 0), - // width: constraints.maxWidth, - // httpHeaders: _httpHeaders, - // imageUrl: imgUrl, - // fit: BoxFit.fitWidth, - // ), - child: NetworkExtendedImage( - url: imgUrl, + child: CachedNetworkImage( + placeholder: (_, __) { + return Container( + alignment: Alignment.center, + color: CupertinoDynamicColor.resolve( + CupertinoColors.systemGrey5, context), + child: const CupertinoActivityIndicator(), + ); + }, height: (height ?? 0) * constraints.maxWidth / (width ?? 0), width: constraints.maxWidth, + httpHeaders: _httpHeaders, + imageUrl: imgUrl.dfUrl, fit: BoxFit.fitWidth, ), + // child: NetworkExtendedImage( + // url: imgUrl, + // height: (height ?? 0) * constraints.maxWidth / (width ?? 0), + // width: constraints.maxWidth, + // fit: BoxFit.fitWidth, + // ), ); }, ); diff --git a/lib/pages/setting/advanced_setting_page.dart b/lib/pages/setting/advanced_setting_page.dart index 0718459fb..b03773aed 100644 --- a/lib/pages/setting/advanced_setting_page.dart +++ b/lib/pages/setting/advanced_setting_page.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:extended_image/extended_image.dart'; import 'package:fehviewer/common/controller/cache_controller.dart'; +import 'package:fehviewer/common/global.dart'; import 'package:fehviewer/common/service/dns_service.dart'; import 'package:fehviewer/common/service/ehconfig_service.dart'; import 'package:fehviewer/common/service/locale_service.dart'; @@ -63,13 +64,17 @@ class ListViewAdvancedSetting extends StatelessWidget { void _handleEFChanged(bool newValue) { _dnsService.enableDomainFronting = newValue; - if (!newValue) return; - final HttpClient eClient = - ExtendedNetworkImageProvider.httpClient as HttpClient; - eClient.badCertificateCallback = - (X509Certificate cert, String host, int port) { - return true; - }; + if (!newValue) { + HttpOverrides.global = null; + } else { + HttpOverrides.global = Global.dfHttpOverrides; + final HttpClient eClient = + ExtendedNetworkImageProvider.httpClient as HttpClient; + eClient.badCertificateCallback = + (X509Certificate cert, String host, int port) { + return true; + }; + } } final List _list = [ diff --git a/lib/utils/dio_util.dart b/lib/utils/dio_util.dart index db0c8a332..61b36f1ac 100644 --- a/lib/utils/dio_util.dart +++ b/lib/utils/dio_util.dart @@ -114,7 +114,7 @@ class HttpManager { } if (domainFronting) { - logger.d('domainFronting'); + // logger.d('domainFronting'); final DnsService dnsServices = Get.find(); final bool enableDoH = dnsServices.enableDoH; diff --git a/lib/utils/http_override.dart b/lib/utils/http_override.dart new file mode 100644 index 000000000..a5f512cb8 --- /dev/null +++ b/lib/utils/http_override.dart @@ -0,0 +1,13 @@ +import 'dart:io'; + +class DFHttpOverrides extends HttpOverrides { + @override + HttpClient createHttpClient(SecurityContext? context) { + final HttpClient client = super.createHttpClient(context); + client.badCertificateCallback = + (X509Certificate cert, String host, int port) { + return true; + }; + return client; + } +} diff --git a/lib/widget/network_extended_image.dart b/lib/widget/network_extended_image.dart index e10ad1494..e3c67ec5e 100644 --- a/lib/widget/network_extended_image.dart +++ b/lib/widget/network_extended_image.dart @@ -32,10 +32,16 @@ class _NetworkExtendedImageState extends State animationController = AnimationController( vsync: this, - duration: const Duration(milliseconds: 200), + duration: const Duration(milliseconds: 0), ); } + @override + void dispose() { + animationController.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { return ExtendedImage.network( @@ -44,9 +50,11 @@ class _NetworkExtendedImageState extends State height: widget.height, headers: _httpHeaders, fit: widget.fit, + // enableLoadState: false, loadStateChanged: (ExtendedImageState state) { switch (state.extendedImageLoadState) { case LoadState.loading: + // return null; return Container( alignment: Alignment.center, color: CupertinoDynamicColor.resolve( @@ -56,6 +64,11 @@ class _NetworkExtendedImageState extends State case LoadState.completed: animationController.forward(); + return ExtendedRawImage( + fit: BoxFit.contain, + image: state.extendedImageInfo?.image, + ); + return FadeTransition( opacity: animationController, child: ExtendedRawImage( diff --git a/pubspec.lock b/pubspec.lock index b857fb844..49af3fa44 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -120,6 +120,27 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "8.1.1" + cached_network_image: + dependency: "direct main" + description: + name: cached_network_image + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.1.0" + cached_network_image_platform_interface: + dependency: transitive + description: + name: cached_network_image_platform_interface + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.0.0" + cached_network_image_web: + dependency: transitive + description: + name: cached_network_image_web + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.0.0" characters: dependency: transitive description: @@ -456,6 +477,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_blurhash: + dependency: transitive + description: + name: flutter_blurhash + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.6.0" flutter_cache_manager: dependency: "direct main" description: @@ -850,6 +878,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "4.0.0" + octo_image: + dependency: transitive + description: + name: octo_image + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.0.0+1" oktoast: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 89601b648..0e0fda312 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ description: fehviewer publish_to: 'none' # Remove this line if you wish to publish to pub.dev -version: 1.1.19+236 +version: 1.1.20+238 environment: sdk: '>=2.13.0 <3.0.0' @@ -59,7 +59,7 @@ dependencies: get_storage: ^2.0.2 photo_view: ^0.11.1 # 图片相关 -# cached_network_image: ^3.0.0 + cached_network_image: ^3.0.0 extended_image: ^4.1.0 liquid_progress_indicator_ns: ^1.0.0 logger: ^1.0.0