feat: 新增 Hosts 写入功能,通过预设的干净 DNS 获取ip 并 写入 hosts 文件

This commit is contained in:
xkeyC 2024-03-12 22:08:46 +08:00
parent 26b58324c4
commit cb924f267c
5 changed files with 321 additions and 3 deletions

View File

@ -257,4 +257,10 @@ foreach ($adapter in $adapterMemory) {
await Process.run( await Process.run(
SystemHelper.powershellPath, ["explorer.exe", "/select,\"$path\""]); SystemHelper.powershellPath, ["explorer.exe", "/select,\"$path\""]);
} }
static String getHostsFilePath() {
final envVars = Platform.environment;
final systemRoot = envVars["SYSTEMROOT"];
return "$systemRoot\\System32\\drivers\\etc\\hosts";
}
} }

View File

@ -51,13 +51,20 @@ class RSHttp {
} }
static Future<RustHttpResponse> head(String url, static Future<RustHttpResponse> head(String url,
{Map<String, String>? headers}) async { {Map<String, String>? headers, String? withIpAddress}) async {
final r = await rust_http.fetch( final r = await rust_http.fetch(
method: MyMethod.head, url: url, headers: headers); method: MyMethod.head,
url: url,
headers: headers,
withIpAddress: withIpAddress);
return r; return r;
} }
static Future<List<String>> dnsLookupTxt(String host) async { static Future<List<String>> dnsLookupTxt(String host) async {
return await rust_http.dnsLookupTxt(host: host); return await rust_http.dnsLookupTxt(host: host);
} }
static Future<List<String>> dnsLookupIps(String host) async {
return await rust_http.dnsLookupIps(host: host);
}
} }

View File

@ -8,7 +8,7 @@ extension AsyncError on Future {
return await this; return await this;
} catch (e) { } catch (e) {
dPrint("unwrap error:$e"); dPrint("unwrap error:$e");
if (context != null) { if (context != null && context.mounted) {
showToast(context, "出现错误: $e"); showToast(context, "出现错误: $e");
} }
return null; return null;

View File

@ -0,0 +1,290 @@
import 'dart:io';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:starcitizen_doctor/common/helper/system_helper.dart';
import 'package:starcitizen_doctor/common/io/rs_http.dart';
import 'package:starcitizen_doctor/common/utils/async.dart';
import 'package:starcitizen_doctor/common/utils/log.dart';
class HostsBoosterDialogUI extends HookConsumerWidget {
const HostsBoosterDialogUI({super.key});
static const _hostsMap = {
"Recaptcha": ["www.recaptcha.net", "recaptcha.net"],
"RSI 官网": ["robertsspaceindustries.com"],
"RSI Zendesk 客服站": ["cloudimperiumservicesllc.zendesk.com"],
"RSI 客服站": ["support.robertsspaceindustries.com"],
};
@override
Widget build(BuildContext context, WidgetRef ref) {
final checkedMap = useState<Map<String, bool>>({});
final workingMap = useState<Map<String, int?>>({});
final workingText = useState<String>("");
doHost(BuildContext context) async {
if (workingMap.value.isEmpty) {
final hasTrue =
checkedMap.value.values.where((element) => element).firstOrNull !=
null;
if (!hasTrue) {
for (var k in _hostsMap.keys) {
checkedMap.value[k] = true;
}
checkedMap.value = Map.from(checkedMap.value);
}
}
workingText.value = "正在查询 DNS 并测试可访问性 请耐心等待...";
final ipsMap = await _doCheckDns(workingMap, checkedMap);
workingText.value = "正在写入 Hosts ...";
if (!context.mounted) return;
await _doWriteHosts(ipsMap).unwrap(context: context);
workingText.value = "读取配置 ...";
await _readHostsState(workingMap, checkedMap);
workingText.value = "";
}
useEffect(() {
// Hosts
_readHostsState(workingMap, checkedMap);
return null;
}, const []);
return ContentDialog(
constraints:
BoxConstraints(maxWidth: MediaQuery.of(context).size.width * .55),
title: Row(
children: [
IconButton(
icon: const Icon(
FluentIcons.back,
size: 22,
),
onPressed:
workingText.value.isEmpty ? Navigator.of(context).pop : null),
const SizedBox(width: 12),
const Text("Hosts 加速"),
const Spacer(),
Button(
onPressed: () => _openHostsFile(context),
child: const Padding(
padding: EdgeInsets.all(3),
child: Row(
children: [
Icon(FluentIcons.open_file),
SizedBox(width: 6),
Text("打开 Hosts 文件"),
],
),
))
],
),
content: AnimatedSize(
duration: const Duration(milliseconds: 200),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const SizedBox(height: 12),
const Row(
children: [
SizedBox(width: 12),
Text("状态"),
SizedBox(width: 38),
Text("站点"),
Spacer(),
Text("是否启用"),
SizedBox(width: 12),
],
),
const SizedBox(height: 12),
ListView.builder(
itemCount: _hostsMap.length,
shrinkWrap: true,
padding: const EdgeInsets.all(6),
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) {
final isEnable =
checkedMap.value[_hostsMap.keys.elementAt(index)] ?? false;
final workingState =
workingMap.value[_hostsMap.keys.elementAt(index)];
return Container(
padding: const EdgeInsets.all(12),
margin: const EdgeInsets.only(bottom: 12),
decoration: BoxDecoration(
color: FluentTheme.of(context).cardColor,
borderRadius: BorderRadius.circular(12),
),
child: Row(
children: [
if (workingState == null)
Icon(FontAwesomeIcons.xmark,
size: 24, color: Colors.red),
if (workingState == 0)
const SizedBox(
width: 24, height: 24, child: ProgressRing()),
if (workingState == 1)
Icon(FontAwesomeIcons.check,
size: 24, color: Colors.green),
const SizedBox(width: 24),
const SizedBox(width: 12),
Text(_hostsMap.keys.elementAt(index)),
const Spacer(),
ToggleSwitch(
onChanged: (value) {
checkedMap.value[_hostsMap.keys.elementAt(index)] =
value;
checkedMap.value = Map.from(checkedMap.value);
},
checked: isEnable,
),
],
),
);
},
),
const SizedBox(height: 12),
if (workingText.value.isNotEmpty)
SizedBox(
height: 86,
child: Column(
children: [
const SizedBox(
height: 42, width: 42, child: ProgressRing()),
const SizedBox(height: 12),
Text(workingText.value),
],
),
)
else
Padding(
padding: const EdgeInsets.all(12),
child: FilledButton(
onPressed: () => doHost(context),
child: const Padding(
padding:
EdgeInsets.only(top: 3, bottom: 3, left: 12, right: 12),
child: Text("一键加速"),
),
),
),
],
),
),
);
}
Future<void> _openHostsFile(BuildContext context) async {
// 使 Hosts
Process.run(SystemHelper.powershellPath, [
"-Command",
"Start-Process notepad.exe -Verb runAs -ArgumentList ${SystemHelper.getHostsFilePath()}"
// ignore: use_build_context_synchronously
]).unwrap(context: context);
}
Future<Map<String, String>> _doCheckDns(
ValueNotifier<Map<String, int?>> workingMap,
ValueNotifier<Map<String, bool>> checkedMap) async {
Map<String, String> result = {};
final trueLen = checkedMap.value.values.where((element) => element).length;
if (trueLen == 0) {
return result;
}
for (var kv in _hostsMap.entries) {
final siteName = kv.key;
final siteHost = kv.value.first;
if (!(checkedMap.value[siteName] ?? false)) {
continue;
}
workingMap.value[siteName] = 0;
workingMap.value = Map.from(workingMap.value);
RSHttp.dnsLookupIps(siteHost).then((ips) async {
int tryCount = ips.length;
try {
for (var ip in ips) {
final resp =
await RSHttp.head("https://$siteHost", withIpAddress: ip);
dPrint(
"[HostsBooster] host== $siteHost ip== $ip resp== ${resp.headers}");
if (resp.headers.isNotEmpty) {
if (result[siteName] == null) {
result[siteName] = ip;
workingMap.value[siteName] = 1;
workingMap.value = Map.from(workingMap.value);
break;
}
}
}
} catch (e) {
tryCount--;
if (tryCount == 0) {
workingMap.value[siteName] = null;
workingMap.value = Map.from(workingMap.value);
result[siteName] = "";
}
}
}, onError: (e) {
workingMap.value[siteName] = null;
workingMap.value = Map.from(workingMap.value);
result[siteName] = "";
});
}
while (true) {
await Future.delayed(const Duration(milliseconds: 100));
if (result.length == trueLen) {
return result;
}
}
}
Future<void> _doWriteHosts(Map<String, String> ipsMap) async {
// hosts
final hostsFile = File(SystemHelper.getHostsFilePath());
final hostsFileString = await hostsFile.readAsString();
final hostsFileLines = hostsFileString.split("\n");
final newHostsFileLines = <String>[];
// copy Lines
for (var line in hostsFileLines) {
if (line.contains("#StarCitizenToolBox")) {
break;
}
newHostsFileLines.add(line);
}
dPrint("userHostsFile == $hostsFileString");
for (var kv in ipsMap.entries) {
final domains = _hostsMap[kv.key] ?? <String>[];
for (var domain in domains) {
if (kv.value != "") {
newHostsFileLines
.add("${kv.value} $domain #StarCitizenToolBox");
}
}
}
await hostsFile.writeAsString(newHostsFileLines.join("\n"), flush: true);
}
Future<void> _readHostsState(ValueNotifier<Map<String, int?>> workingMap,
ValueNotifier<Map<String, bool>> checkedMap) async {
workingMap.value.clear();
final hostsFile = File(SystemHelper.getHostsFilePath());
final hostsFileString = await hostsFile.readAsString();
final hostsFileLines = hostsFileString.split("\n");
dPrint("userHostsFile == $hostsFileString");
for (var line in hostsFileLines) {
if (line.contains("#StarCitizenToolBox")) {
for (var host in _hostsMap.entries) {
if (line.contains(host.value.first)) {
workingMap.value[host.key] = 1;
workingMap.value = Map.from(workingMap.value);
checkedMap.value[host.key] = true;
checkedMap.value = Map.from(checkedMap.value);
}
}
}
}
}
}

View File

@ -22,6 +22,8 @@ import 'package:starcitizen_doctor/widgets/widgets.dart';
import 'package:url_launcher/url_launcher_string.dart'; import 'package:url_launcher/url_launcher_string.dart';
import 'package:xml/xml.dart'; import 'package:xml/xml.dart';
import 'dialogs/hosts_booster_dialog_ui.dart';
part 'tools_ui_model.g.dart'; part 'tools_ui_model.g.dart';
part 'tools_ui_model.freezed.dart'; part 'tools_ui_model.freezed.dart';
@ -81,6 +83,13 @@ class ToolsUIModel extends _$ToolsUIModel {
const Icon(FontAwesomeIcons.download, size: 28), const Icon(FontAwesomeIcons.download, size: 28),
onTap: () => _downloadP4k(context), onTap: () => _downloadP4k(context),
), ),
ToolsItemData(
"hosts_booster",
"Hosts 加速",
"将 IP 信息写入 Hosts 文件,解决部分地区的 DNS 污染导致无法登录官网等问题。",
const Icon(FluentIcons.virtual_network, size: 28),
onTap: () => _doHostsBooster(context),
),
ToolsItemData( ToolsItemData(
"reinstall_eac", "reinstall_eac",
"重装 EasyAntiCheat 反作弊", "重装 EasyAntiCheat 反作弊",
@ -549,4 +558,10 @@ class ToolsUIModel extends _$ToolsUIModel {
void onChangeLauncherPath(String s) { void onChangeLauncherPath(String s) {
state = state.copyWith(rsiLauncherInstalledPath: s); state = state.copyWith(rsiLauncherInstalledPath: s);
} }
_doHostsBooster(BuildContext context) async {
showDialog(
context: context,
builder: (BuildContext context) => const HostsBoosterDialogUI());
}
} }