Skip to content

Commit

Permalink
Fix: fix domain fronting
Browse files Browse the repository at this point in the history
  • Loading branch information
honjow committed Jul 17, 2021
1 parent 7cbf1c8 commit f11ec29
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 87 deletions.
7 changes: 7 additions & 0 deletions lib/common/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions lib/common/service/base_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(RxInterface<T> listener, ValueChanged<T> onChange) {
return ever<T>(listener, (value) {
onChange(value);
Expand Down
37 changes: 15 additions & 22 deletions lib/common/service/dns_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -140,39 +142,30 @@ class DnsService extends ProfileService {
@override
void onInit() {
super.onInit();
final DnsConfig _dnsConfig = Global.profile.dnsConfig;

enableCustomHosts = _dnsConfig.enableCustomHosts ?? false;
ever<bool>(_enableCustomHosts, (bool value) {
Global.profile = Global.profile
.copyWith(dnsConfig: _dnsConfig.copyWith(enableCustomHosts: value));
Global.saveProfile();
enableCustomHosts = dnsConfig.enableCustomHosts ?? false;
everProfile<bool>(_enableCustomHosts, (bool value) {
dnsConfig = dnsConfig.copyWith(enableCustomHosts: value);
});

_hosts(_dnsConfig.hosts);
ever<List<DnsCache>>(_hosts, (List<DnsCache> value) {
Global.profile =
Global.profile.copyWith(dnsConfig: _dnsConfig.copyWith(hosts: value));
Global.saveProfile();
_hosts(dnsConfig.hosts);
everProfile<List<DnsCache>>(_hosts, (List<DnsCache> value) {
dnsConfig = dnsConfig.copyWith(hosts: value);
});

enableDoH = _dnsConfig.enableDoH ?? false;
ever<bool>(_enableDoH, (bool value) {
Global.profile = Global.profile
.copyWith(dnsConfig: _dnsConfig.copyWith(enableDoH: value));
Global.saveProfile();
enableDoH = dnsConfig.enableDoH ?? false;
everProfile<bool>(_enableDoH, (bool value) {
dnsConfig = dnsConfig.copyWith(enableDoH: value);
});

_dohCache(_dnsConfig.dohCache);
_dohCache(dnsConfig.dohCache);
everProfile<List<DnsCache>>(_dohCache, (List<DnsCache> 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<bool>(_enableDomainFronting, (bool value) {
Global.profile = Global.profile.copyWith(
dnsConfig: _dnsConfig.copyWith(enableDomainFronting: value));
dnsConfig = dnsConfig.copyWith(enableDomainFronting: value);
});
}
}
34 changes: 18 additions & 16 deletions lib/pages/gallery/view/gallery_widget.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -86,6 +87,7 @@ class CoverImage extends StatelessWidget {
return Builder(builder: (_) {
final Map<String, String> _httpHeaders = {
'Cookie': Global.profile.user.cookie ?? '',
'host': Uri.parse(imageUrl ?? '').host,
};

if (imageUrl != null && imageUrl!.isNotEmpty) {
Expand All @@ -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,
// ),
),
),
),
Expand Down
36 changes: 17 additions & 19 deletions lib/pages/item/gallery_item.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
);
Expand Down
40 changes: 21 additions & 19 deletions lib/pages/item/gallery_item_simple.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -291,35 +292,36 @@ class CoverImg extends StatelessWidget {
final EhConfigService _ehConfigService = Get.find();
final Map<String, String> _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,
// ),
);
},
);
Expand Down
19 changes: 12 additions & 7 deletions lib/pages/setting/advanced_setting_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<Widget> _list = <Widget>[
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/dio_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class HttpManager {
}

if (domainFronting) {
logger.d('domainFronting');
// logger.d('domainFronting');
final DnsService dnsServices = Get.find();
final bool enableDoH = dnsServices.enableDoH;

Expand Down
13 changes: 13 additions & 0 deletions lib/utils/http_override.dart
Original file line number Diff line number Diff line change
@@ -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;
}
}
15 changes: 14 additions & 1 deletion lib/widget/network_extended_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ class _NetworkExtendedImageState extends State<NetworkExtendedImage>

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(
Expand All @@ -44,9 +50,11 @@ class _NetworkExtendedImageState extends State<NetworkExtendedImage>
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(
Expand All @@ -56,6 +64,11 @@ class _NetworkExtendedImageState extends State<NetworkExtendedImage>
case LoadState.completed:
animationController.forward();

return ExtendedRawImage(
fit: BoxFit.contain,
image: state.extendedImageInfo?.image,
);

return FadeTransition(
opacity: animationController,
child: ExtendedRawImage(
Expand Down
Loading

0 comments on commit f11ec29

Please sign in to comment.