Skip to content

Commit

Permalink
Feat: domain fronting
Browse files Browse the repository at this point in the history
  • Loading branch information
honjow committed Jul 17, 2021
1 parent 65eb275 commit 7cbf1c8
Show file tree
Hide file tree
Showing 40 changed files with 730 additions and 323 deletions.
1 change: 0 additions & 1 deletion lib/common/controller/archiver_download_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:fehviewer/models/index.dart';
import 'package:fehviewer/pages/tab/controller/download_view_controller.dart';
import 'package:fehviewer/store/floor/dao/gallery_task_dao.dart';
import 'package:fehviewer/store/floor/dao/image_task_dao.dart';
import 'package:fehviewer/store/floor/entity/gallery_task.dart';
import 'package:fehviewer/store/get_store.dart';
import 'package:fehviewer/utils/logger.dart';
import 'package:fehviewer/utils/toast.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/common/controller/download_controller.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:io';
import 'dart:math' as math;

import 'package:collection/collection.dart';
import 'package:dio/dio.dart';
Expand Down
29 changes: 23 additions & 6 deletions lib/common/exts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import 'package:get/get.dart';

extension EhString on String {
String toRealUrl() {
final DnsService dnsConfigController = Get.find();
final bool enableDoH = dnsConfigController.enableDoH.value;
final bool enableCustomHosts = dnsConfigController.enableCustomHosts.value;
final List<DnsCache> _dohDnsCacheList = dnsConfigController.dohCache;
final DnsService dnsService = Get.find();
final bool enableDoH = dnsService.enableDoH;
final bool enableCustomHosts = dnsService.enableCustomHosts;
final List<DnsCache> _dohDnsCacheList = dnsService.dohCache;
final String host = Uri.parse(this).host;
if (host.isEmpty) {
return this;
Expand All @@ -23,8 +23,8 @@ extension EhString on String {
return this;
} else if (enableDoH) {
// logger.d(' enableDoH');
Get.find<DnsService>().updateDoHCache(host);
final int _dohDnsCacheIndex = dnsConfigController.dohCache
Get.find<DnsService>().getDoHCache(host);
final int _dohDnsCacheIndex = dnsService.dohCache
.indexWhere((DnsCache element) => element.host == host);
final DnsCache? dohDnsCache =
_dohDnsCacheIndex > -1 ? _dohDnsCacheList[_dohDnsCacheIndex] : null;
Expand All @@ -36,6 +36,23 @@ extension EhString on String {
return this;
}

String get dfUrl {
final DnsService dnsService = Get.find();
final df = dnsService.enableDomainFronting;
final String host = Uri.parse(this).host;
if (host.isEmpty) {
return this;
}
if (df) {
final realHost = dnsService.hostMapMerge[host] ?? host;
final String realUrl = replaceFirst(host, realHost);
logger.v('realUrl: $realUrl');
return realUrl;
}

return this;
}

String get gid {
final RegExp urlRex = RegExp(r'/g/(\d+)/(\w+)/$');
final RegExpMatch? urlRult = urlRex.firstMatch(this);
Expand Down
32 changes: 22 additions & 10 deletions lib/common/global.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'dart:io';

// import 'package:cached_network_image/cached_network_image.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cookie_jar/cookie_jar.dart';
import 'package:device_info/device_info.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:extended_image/extended_image.dart';
import 'package:fehviewer/const/const.dart';
import 'package:fehviewer/const/storages.dart';
import 'package:fehviewer/models/index.dart';
Expand All @@ -12,7 +14,6 @@ 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/https_proxy.dart';
import 'package:fehviewer/utils/logger.dart';
import 'package:fehviewer/utils/storage.dart';
import 'package:fehviewer/utils/utility.dart';
Expand Down Expand Up @@ -170,7 +171,7 @@ class Global {

static late PersistCookieJar cookieJar;

static HttpProxy httpProxy = HttpProxy('localhost', '$kProxyPort');
// static HttpProxy httpProxy = HttpProxy('localhost', '$kProxyPort');

static String appSupportPath = '';
static String appDocPath = '';
Expand Down Expand Up @@ -224,9 +225,9 @@ class Global {
}

// 代理初始化
if (Platform.isIOS || Platform.isAndroid) {
await CustomHttpsProxy.instance.init();
}
// if (Platform.isIOS || Platform.isAndroid) {
// await CustomHttpsProxy.instance.init();
// }

logger.i('doc $appDocPath \napps $appSupportPath \ntemp $tempPath');

Expand Down Expand Up @@ -257,6 +258,8 @@ class Global {

// 数据更新
// await dataUpdate();

_initImageHttpClient();
}

static void creatDirs() {
Expand All @@ -270,11 +273,11 @@ class Global {

_initProfile();

if ((profile.dnsConfig.enableCustomHosts ?? false) ||
(profile.dnsConfig.enableDoH ?? false)) {
logger.v('${profile.dnsConfig.enableCustomHosts}');
HttpOverrides.global = httpProxy;
}
// if ((profile.dnsConfig.enableCustomHosts ?? false) ||
// (profile.dnsConfig.enableDoH ?? false)) {
// logger.v('${profile.dnsConfig.enableCustomHosts}');
// HttpOverrides.global = httpProxy;
// }
}

static void _initProfile() {
Expand All @@ -299,4 +302,13 @@ class Global {
StorageUtil().setString(CLEAN_VER, '${EHConst.cleanDataVer}');
}
}

static void _initImageHttpClient() {
final HttpClient eClient =
ExtendedNetworkImageProvider.httpClient as HttpClient;
eClient.badCertificateCallback =
(X509Certificate cert, String host, int port) {
return true;
};
}
}
94 changes: 65 additions & 29 deletions lib/common/service/dns_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:fehviewer/common/global.dart';
import 'package:fehviewer/const/const.dart';
import 'package:fehviewer/models/index.dart';
import 'package:fehviewer/utils/dns_util.dart';
import 'package:fehviewer/utils/logger.dart';
Expand All @@ -12,16 +13,51 @@ const String _regExpHost =
r'^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$';

class DnsService extends ProfileService {
RxBool enableCustomHosts = false.obs;
RxBool enableDoH = false.obs;
RxBool enableDomainFronting = false.obs;
// RxBool enableCustomHosts = false.obs;
final _enableCustomHosts = false.obs;
bool get enableCustomHosts => _enableCustomHosts.value;
set enableCustomHosts(val) => _enableCustomHosts.value = val;

// RxBool enableDoH = false.obs;
final _enableDoH = false.obs;
bool get enableDoH => _enableDoH.value;
set enableDoH(val) => _enableDoH.value = val;

// RxBool enableDomainFronting = false.obs;
final _enableDomainFronting = false.obs;
bool get enableDomainFronting => _enableDomainFronting.value;
set enableDomainFronting(val) => _enableDomainFronting.value = val;

final RxList<DnsCache> _hosts = <DnsCache>[].obs;
final RxList<DnsCache> _dohCache = <DnsCache>[].obs;

RxList<DnsCache> get hosts => _hosts;

RxList<DnsCache> get dohCache => _dohCache;

Map<String, String> get hostMap {
final _map = <String, String>{};
for (final dc in hosts) {
if (dc.host != null && dc.addr != null) {
_map.putIfAbsent(dc.host!, () => dc.addr!);
}
}
return _map;
}

Map<String, String> get hostMapMerge {
final coutomHosts = hostMap;

// 预置列表
if (enableCustomHosts) {
for (final host in EHConst.internalHosts.entries) {
coutomHosts.putIfAbsent(host.key, () => host.value);
}
}

return coutomHosts;
}

void removeCustomHostAt(int index) {
_hosts.removeAt(index);
}
Expand Down Expand Up @@ -60,9 +96,9 @@ class DnsService extends ProfileService {
}
}

Future<void> updateDoHCache(String host) async {
Future<DnsCache?> getDoHCache(String host) async {
if (host == 'cloudflare-dns.com') {
return;
return null;
}

// 24小时
Expand All @@ -75,66 +111,66 @@ class DnsService extends ProfileService {
if (dnsCache != null) {
if (dnsCache.lastResolve != null &&
nowTime - (dnsCache.lastResolve ?? -1) > updateInterval) {
logger.wtf(' updateDoHCache $host');
logger.d('updateDoHCache $host');
// get new and cache
final String _addr = await DnsUtil.doh(host);
// dnsCache
// ..lastResolve = nowTime
// ..addr = _addr;
dohCache[index] = dnsCache.copyWith(lastResolve: nowTime, addr: _addr);
final String? _addr = await DnsUtil.doh(host);
final _dc = dnsCache.copyWith(lastResolve: nowTime, addr: _addr);
dohCache[index] = _dc;
logger.d('rult ${_dc.toJson()}');
return _dc;
}
} else {
// get new
logger.wtf(' get new doh $host');
final String _addr = await DnsUtil.doh(host);
// logger.d(' get new doh $host addr=$_addr');
dohCache.add(DnsCache(
logger.d('get new doh $host');
final String? _addr = await DnsUtil.doh(host);
logger.d(' get new doh $host addr=$_addr');
final _dc = DnsCache(
host: host,
lastResolve: nowTime,
addr: _addr,
ttl: -1,
addrs: [],
));
);
dohCache.add(_dc);
// logger.d(_dc.toJson());
return _dc;
}
}

@override
void onInit() {
super.onInit();
final DnsConfig _dnsConfig = Global.profile.dnsConfig;
enableCustomHosts.value = _dnsConfig.enableCustomHosts ?? false;
_hosts(_dnsConfig.hosts);
_dohCache(_dnsConfig.dohCache);

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

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

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

_dohCache(_dnsConfig.dohCache);
everProfile<List<DnsCache>>(_dohCache, (List<DnsCache> value) {
// _dnsConfig.dohCache = value;
Global.profile = Global.profile
.copyWith(dnsConfig: _dnsConfig.copyWith(dohCache: value));
});

enableDomainFronting.value = _dnsConfig.enableDomainFronting ?? false;
everProfile<bool>(enableDomainFronting as RxInterface<bool>, (bool value) {
// _dnsConfig.enableDomainFronting = value;
enableDomainFronting = _dnsConfig.enableDomainFronting ?? false;
everProfile<bool>(_enableDomainFronting, (bool value) {
Global.profile = Global.profile.copyWith(
dnsConfig: _dnsConfig.copyWith(enableDomainFronting: value));
});
Expand Down
38 changes: 35 additions & 3 deletions lib/const/const.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'package:extended_image/extended_image.dart';
import 'package:fehviewer/common/global.dart';
import 'package:fehviewer/common/service/ehconfig_service.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

enum ListModeEnum {
Expand Down Expand Up @@ -63,6 +67,28 @@ class GetIds {
static const String TAG_ADD_CLEAR_BTN = 'TAG_ADD_CLEAR_BTN';
}

LoadStateChanged defLoadStateChanged = (ExtendedImageState state) {
switch (state.extendedImageLoadState) {
case LoadState.loading:
return Container(
alignment: Alignment.center,
color: CupertinoDynamicColor.resolve(
CupertinoColors.systemGrey5, Get.context!),
child: const CupertinoActivityIndicator(),
);
case LoadState.failed:
return Container(
alignment: Alignment.center,
child: const Icon(
Icons.error,
color: Colors.red,
),
);
default:
return null;
}
};

// ignore: avoid_classes_with_only_static_members
class EHConst {
// 网页登陆页面
Expand Down Expand Up @@ -252,18 +278,18 @@ class EHConst {
'vietnamese': 'VI',
};

static List<String> fontFamilyFallback = [
static const List<String> fontFamilyFallback = [
'miui',
// 'sans-serif',
];
static List<String> monoFontFamilyFallback = [
static const List<String> monoFontFamilyFallback = [
'monaco',
'monospace',
'Menlo',
'Courier New',
];

static List<int> invList = [
static const List<int> invList = [
500,
1000,
1500,
Expand All @@ -285,4 +311,10 @@ class EHConst {
9500,
10000,
];

static const Map<String, String> internalHosts = {
'e-hentai.org': '104.20.134.21',
'api.e-hentai.org': '37.48.89.16',
'exhentai.org': '178.175.129.252',
};
}
1 change: 1 addition & 0 deletions lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib/get_init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'common/controller/localfav_controller.dart';
import 'common/controller/quicksearch_controller.dart';
import 'common/controller/tag_trans_controller.dart';
import 'common/controller/user_controller.dart';
import 'common/isolate_download/download_manager.dart';
import 'common/service/depth_service.dart';
import 'common/service/dns_service.dart';
import 'common/service/ehconfig_service.dart';
Expand Down
File renamed without changes.
Loading

0 comments on commit 7cbf1c8

Please sign in to comment.