feat: 1.新增在 VPN 配置中添加了一条规则,2.修复全局代理无法使用

This commit is contained in:
speakeloudest 2025-11-18 05:15:06 -08:00
parent 59aa67d456
commit 679c303457
4 changed files with 766 additions and 318 deletions

View File

@ -2116,7 +2116,6 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
kr_isConnected.value = true;
kr_startConnectionTimer();
kr_updateConnectionInfo();
KRSingBoxImp.instance.kr_debugTunConnectivity();
// 🔧
if (!_kr_tryUpdateDelayFromActiveGroups()) {

View File

@ -16,16 +16,14 @@ import 'package:path/path.dart' as p;
import '../../../core/model/directories.dart';
import '../../../singbox/model/singbox_config_option.dart';
import '../../../singbox/model/singbox_outbound.dart';
import '../../../singbox/model/singbox_proxy_type.dart';
import '../../../singbox/model/singbox_stats.dart';
import '../../../singbox/model/singbox_status.dart';
import '../../utils/kr_country_util.dart';
import '../../utils/kr_log_util.dart';
import '../../utils/kr_secure_storage.dart';
import '../../utils/kr_windows_dns_util.dart';
import '../../common/app_run_data.dart';
import 'package:flutter/foundation.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
enum KRConnectionType {
global,
@ -427,6 +425,12 @@ class KRSingBoxImp {
);
KRLogUtil.kr_i('✅ SingBox 初始化完成');
// 🔧 geosite workingDir
KRLogUtil.kr_i('📦 开始提取 geosite 文件...', tag: 'SingBox');
await _kr_extractGeositeFiles();
KRLogUtil.kr_i('✅ geosite 文件提取完成', tag: 'SingBox');
_kr_isInitialized = true;
// 🔑
@ -481,22 +485,78 @@ class KRSingBoxImp {
}
}
/// geosite workingDir
Future<void> _kr_extractGeositeFiles() async {
try {
// geosite
final geositeDir = Directory(p.join(kr_configDics.workingDir.path, 'geosite'));
if (!geositeDir.existsSync()) {
await geositeDir.create(recursive: true);
KRLogUtil.kr_i('✅ 已创建 geosite 目录: ${geositeDir.path}', tag: 'SingBox');
}
//
final files = ['geoip-cn.srs', 'geosite-cn.srs'];
for (final filename in files) {
final assetPath = 'assets/geosite/$filename';
final targetPath = p.join(geositeDir.path, filename);
final targetFile = File(targetPath);
//
if (targetFile.existsSync()) {
final fileSize = await targetFile.length();
KRLogUtil.kr_i('📄 $filename 已存在 (${fileSize} bytes), 跳过', tag: 'SingBox');
continue;
}
// assets
KRLogUtil.kr_i('📥 正在提取 $filename...', tag: 'SingBox');
final byteData = await rootBundle.load(assetPath);
final bytes = byteData.buffer.asUint8List();
//
await targetFile.writeAsBytes(bytes);
final writtenSize = await targetFile.length();
KRLogUtil.kr_i('✅ 提取成功: $filename (${writtenSize} bytes)', tag: 'SingBox');
}
KRLogUtil.kr_i('🎉 所有 geosite 文件提取完成', tag: 'SingBox');
} catch (e, stackTrace) {
KRLogUtil.kr_e('❌ 提取 geosite 文件失败: $e', tag: 'SingBox');
KRLogUtil.kr_e('堆栈: $stackTrace', tag: 'SingBox');
// ,(使)
}
}
Map<String, dynamic> _getConfigOption() {
// 使
// if (kr_configOption.isNotEmpty) {
// return kr_configOption;
// }
// 🔧 region 'other'libcore
final String effectiveRegion;
if (kr_connectionType.value == KRConnectionType.global) {
effectiveRegion = 'other'; //
KRLogUtil.kr_i('🌐 [全局代理模式] region 设为 other所有流量走代理', tag: 'SingBox');
} else {
effectiveRegion = KRCountryUtil.kr_getCurrentCountryCode(); // 使
KRLogUtil.kr_i('✅ [智能代理模式] region 设为 $effectiveRegion', tag: 'SingBox');
}
final op = {
"region": "other", // hiddify-app: 使 "other"
"region": effectiveRegion, // 🔧 region
"block-ads": false, // hiddify-app: 广
"use-xray-core-when-possible": false,
"execute-config-as-is": true, // 🔧 使 libcore
"log-level": "info", // 使 info warn
"resolve-destination": false,
"ipv6-mode": "ipv4_only", // hiddify-app: 使 IPv4 (: ipv4_only, prefer_ipv4, prefer_ipv6, ipv6_only)
"remote-dns-address": "https://dns.google/dns-query", // 使 Google DoH DNS
"remote-dns-address": "https://dns.google/dns-query", // 使 Google DoH DNS
"remote-dns-domain-strategy": "prefer_ipv4",
"direct-dns-address": "local" ,
"direct-dns-address": "local", // 使 DNS
"direct-dns-domain-strategy": "prefer_ipv4",
"mixed-port": kr_port,
"tproxy-port": kr_port,
@ -511,13 +571,21 @@ class KRSingBoxImp {
"enable-tun": Platform.isIOS || Platform.isAndroid,
"enable-tun-service": false,
"set-system-proxy":
Platform.isWindows || Platform.isLinux || Platform.isMacOS,
Platform.isWindows || Platform.isLinux || Platform.isMacOS,
"bypass-lan": false,
"allow-connection-from-lan": false,
"enable-fake-dns": false,
"enable-dns-routing": true,
"independent-dns-cache": true,
"rules": [],
"rules": [
// - HiddifyOptions.Rules
// Native config.BuildConfig()
// libcore domains 使 "domain:ip.sb" domain_suffix
{
"domains": "domain:api.hifast.biz", // domain: domain_suffix
"outbound": "bypass" // bypass = direct
}
],
"mux": {
"enable": false,
"padding": false,
@ -562,6 +630,17 @@ class KRSingBoxImp {
}
};
kr_configOption = op;
// 🔧
final rules = op["rules"] as List?;
KRLogUtil.kr_i('✅ HiddifyOptions 已生成,包含 ${rules?.length ?? 0} 条自定义路由规则', tag: 'SingBox');
if (rules != null && rules.isNotEmpty) {
for (var rule in rules) {
final ruleMap = rule as Map<String, dynamic>;
KRLogUtil.kr_i(' - 规则: domains=${ruleMap["domains"]}, outbound=${ruleMap["outbound"]}', tag: 'SingBox');
}
}
return op;
}
@ -1025,25 +1104,10 @@ class KRSingBoxImp {
KRLogUtil.kr_i('💾 开始保存配置文件...', tag: 'SingBox');
KRLogUtil.kr_i('📊 出站节点数量: ${outbounds.length}', tag: 'SingBox');
//
final realNodes = <Map<String, dynamic>>[];
final autoNodes = <Map<String, dynamic>>[];
for (final outbound in outbounds) {
if (outbound['tag']?.toString().endsWith('-auto') == true) {
autoNodes.add(outbound);
} else {
realNodes.add(outbound);
}
}
KRLogUtil.kr_i('📊 真实节点数量: ${realNodes.length}', tag: 'SingBox');
KRLogUtil.kr_i('📊 自动选择节点数量: ${autoNodes.length}', tag: 'SingBox');
//
for (int i = 0; i < realNodes.length; i++) {
final outbound = realNodes[i];
KRLogUtil.kr_i('📋 真实节点[$i] 配置:', tag: 'SingBox');
for (int i = 0; i < outbounds.length; i++) {
final outbound = outbounds[i];
KRLogUtil.kr_i('📋 节点[$i] 配置:', tag: 'SingBox');
KRLogUtil.kr_i(' - type: ${outbound['type']}', tag: 'SingBox');
KRLogUtil.kr_i(' - tag: ${outbound['tag']}', tag: 'SingBox');
KRLogUtil.kr_i(' - server: ${outbound['server']}', tag: 'SingBox');
@ -1051,26 +1115,20 @@ class KRSingBoxImp {
if (outbound['method'] != null) {
KRLogUtil.kr_i(' - method: ${outbound['method']}', tag: 'SingBox');
}
if (outbound['interval'] != null) {
KRLogUtil.kr_i(' - interval: ${outbound['interval']}', tag: 'SingBox');
}
if (outbound['password'] != null) {
KRLogUtil.kr_i(' - password: ${outbound['password']?.toString().substring(0, 8)}...', tag: 'SingBox');
}
if (outbound['uuid'] != null) {
KRLogUtil.kr_i(' - uuid: ${outbound['uuid']?.toString().substring(0, 8)}...', tag: 'SingBox');
}
}
for (int i = 0; i < autoNodes.length; i++) {
final outbound = autoNodes[i];
KRLogUtil.kr_i('🤖 自动选择节点[$i] 配置:', tag: 'SingBox');
KRLogUtil.kr_i(' - type: ${outbound['type']}', tag: 'SingBox');
KRLogUtil.kr_i(' - tag: ${outbound['tag']}', tag: 'SingBox');
if (outbound['outbounds'] != null) {
KRLogUtil.kr_i(' - outbounds: ${outbound['outbounds']}', tag: 'SingBox');
}
KRLogUtil.kr_i(' - 完整配置: ${jsonEncode(outbound)}', tag: 'SingBox');
}
// Hysteria2 libcore
final filteredRealNodes = realNodes.where((outbound) {
kr_outbounds = outbounds.where((outbound) {
final type = outbound['type'];
if (type == 'hysteria2' || type == 'hysteria') {
KRLogUtil.kr_w('⚠️ 跳过 Hysteria2 节点: ${outbound['tag']} (libcore bug)', tag: 'SingBox');
@ -1079,30 +1137,11 @@ class KRSingBoxImp {
return true;
}).toList();
// Hysteria2
final filteredAutoNodes = autoNodes.where((outbound) {
if (outbound['outbounds'] != null) {
final outbounds = outbound['outbounds'] as List<dynamic>;
// Hysteria2
final hasHysteria2 = outbounds.any((tag) => tag.toString().toLowerCase().contains('hysteria'));
if (hasHysteria2) {
KRLogUtil.kr_w('⚠️ 跳过包含 Hysteria2 的自动选择节点: ${outbound['tag']} (libcore bug)', tag: 'SingBox');
return false;
}
}
return true;
}).toList();
KRLogUtil.kr_i('✅ 过滤后真实节点数量: ${filteredRealNodes.length}/${realNodes.length}', tag: 'SingBox');
KRLogUtil.kr_i('✅ 过滤后自动选择节点数量: ${filteredAutoNodes.length}/${autoNodes.length}', tag: 'SingBox');
// selector
final allAvailableTags = <String>[];
allAvailableTags.addAll(filteredAutoNodes.map((o) => o['tag'] as String));
allAvailableTags.addAll(filteredRealNodes.map((o) => o['tag'] as String));
KRLogUtil.kr_i('✅ 过滤后节点数量: ${kr_outbounds.length}/${outbounds.length}', tag: 'SingBox');
// 🔧 SingBox
// outbounds selectordirect/block/dns-out
// {"outbounds": [...]}, libcore
//
final Map<String, dynamic> fullConfig = {
"log": {
"level": "debug",
@ -1112,8 +1151,7 @@ class KRSingBoxImp {
"servers": [
{
"tag": "dns-remote",
"address": "https://dns.google/dns-query",
"detour": "direct",
"address": "https://1.1.1.1/dns-query",
"address_resolver": "dns-direct"
},
{
@ -1122,7 +1160,7 @@ class KRSingBoxImp {
"detour": "direct"
}
],
"rules": [],
"rules": _kr_buildDnsRules(), // 使 DNS
"final": "dns-remote",
"strategy": "prefer_ipv4"
},
@ -1139,17 +1177,14 @@ class KRSingBoxImp {
}
],
"outbounds": [
// 🔧 selector
// 🔧 selector
{
"type": "selector",
"tag": "proxy",
"outbounds": allAvailableTags,
"default": allAvailableTags.isNotEmpty ? allAvailableTags[0] : "direct",
"outbounds": kr_outbounds.map((o) => o['tag'] as String).toList(),
"default": kr_outbounds.isNotEmpty ? kr_outbounds[0]['tag'] : "direct",
},
// 🔧
...filteredAutoNodes,
// 🔧
...filteredRealNodes,
...kr_outbounds,
{
"type": "direct",
"tag": "direct"
@ -1164,13 +1199,8 @@ class KRSingBoxImp {
}
],
"route": {
"rules": [
{ "type": "dns", "outbound": "dns-out" }, // sing-box dns-out
{ "type": "field", "port": 53, "network": "udp", "outbound": "direct" }, // UDP 53
{ "type": "field", "port": 53, "network": "tcp", "outbound": "direct" }, // TCP 53
{ "type": "field", "port": 853, "network": "tcp", "outbound": "direct" }, // TCP 853Private DNS
{ "type": "field", "domain": ["dns.google", "cloudflare-dns.com", "one.one.one.one"], "outbound": "direct" } // DoH
],
"rules": _kr_buildRouteRules(), // 使
"rule_set": _kr_buildRuleSets(), // 使
"final": "proxy", // 🔧 使 selector
"auto_detect_interface": true
}
@ -1181,8 +1211,7 @@ class KRSingBoxImp {
final mapStr = jsonEncode(fullConfig);
KRLogUtil.kr_i('📄 完整配置文件长度: ${mapStr.length}', tag: 'SingBox');
KRLogUtil.kr_i('📄 Outbounds 总数: ${filteredAutoNodes.length + filteredRealNodes.length}', tag: 'SingBox');
KRLogUtil.kr_i('📄 Selector 可用标签数: ${allAvailableTags.length}', tag: 'SingBox');
KRLogUtil.kr_i('📄 Outbounds 数量: ${kr_outbounds.length}', tag: 'SingBox');
KRLogUtil.kr_i('📄 配置前800字符:\n${mapStr.substring(0, mapStr.length > 800 ? 800 : mapStr.length)}', tag: 'SingBox');
await file.writeAsString(mapStr);
@ -1200,6 +1229,128 @@ class KRSingBoxImp {
KRLogUtil.kr_i('✅ 配置文件保存完成', tag: 'SingBox');
}
/// DNS
List<Map<String, dynamic>> _kr_buildDnsRules() {
final currentCountryCode = KRCountryUtil.kr_getCurrentCountryCode();
final rules = <Map<String, dynamic>>[];
// 🔧 "智能代理"
// "全局代理"使
if (kr_connectionType.value == KRConnectionType.rule && currentCountryCode != 'other') {
rules.add({
"rule_set": [
"geoip-$currentCountryCode",
"geosite-$currentCountryCode",
],
"server": "dns-direct"
});
KRLogUtil.kr_i('✅ [智能代理模式] 添加 DNS 规则: $currentCountryCode 域名使用直连 DNS', tag: 'SingBox');
} else if (kr_connectionType.value == KRConnectionType.global) {
KRLogUtil.kr_i('🌐 [全局代理模式] 跳过国家 DNS 规则所有DNS查询走代理', tag: 'SingBox');
}
return rules;
}
///
List<Map<String, dynamic>> _kr_buildRouteRules() {
final currentCountryCode = KRCountryUtil.kr_getCurrentCountryCode();
final rules = <Map<String, dynamic>>[];
// : DNS dns-out
rules.add({
"protocol": "dns",
"outbound": "dns-out"
});
//
// rules.add({
// "domain_suffix": ["ip138.com"],
// "outbound": "direct"
// });
// KRLogUtil.kr_i('✅ 添加自定义域名直连规则: ip138.com -> direct', tag: 'SingBox');
// 🔧 "智能代理"
// "全局代理"使
if (kr_connectionType.value == KRConnectionType.rule && currentCountryCode != 'other') {
rules.add({
"rule_set": [
"geoip-$currentCountryCode",
"geosite-$currentCountryCode",
],
"outbound": "direct"
});
KRLogUtil.kr_i('✅ [智能代理模式] 添加路由规则: $currentCountryCode IP/域名直连', tag: 'SingBox');
} else if (kr_connectionType.value == KRConnectionType.global) {
KRLogUtil.kr_i('🌐 [全局代理模式] 跳过国家路由规则,所有流量走代理', tag: 'SingBox');
}
return rules;
}
///
List<Map<String, dynamic>> _kr_buildRuleSets() {
final currentCountryCode = KRCountryUtil.kr_getCurrentCountryCode();
final ruleSets = <Map<String, dynamic>>[];
// 🔧 "智能代理"
// "全局代理"使
if (kr_connectionType.value == KRConnectionType.rule && currentCountryCode != 'other') {
//
final geoipFile = File(p.join(kr_configDics.workingDir.path, 'geosite', 'geoip-$currentCountryCode.srs'));
final geositeFile = File(p.join(kr_configDics.workingDir.path, 'geosite', 'geosite-$currentCountryCode.srs'));
if (geoipFile.existsSync() && geositeFile.existsSync()) {
// 使
ruleSets.add({
"type": "local",
"tag": "geoip-$currentCountryCode",
"format": "binary",
"path": "./geosite/geoip-$currentCountryCode.srs" // workingDir
});
ruleSets.add({
"type": "local",
"tag": "geosite-$currentCountryCode",
"format": "binary",
"path": "./geosite/geosite-$currentCountryCode.srs"
});
KRLogUtil.kr_i('✅ 使用本地规则集: $currentCountryCode', tag: 'SingBox');
KRLogUtil.kr_i(' - geoip: ./geosite/geoip-$currentCountryCode.srs', tag: 'SingBox');
KRLogUtil.kr_i(' - geosite: ./geosite/geosite-$currentCountryCode.srs', tag: 'SingBox');
} else {
// ,使
KRLogUtil.kr_w('⚠️ 本地规则集不存在,使用远程规则集', tag: 'SingBox');
KRLogUtil.kr_w(' - geoip 文件存在: ${geoipFile.existsSync()}', tag: 'SingBox');
KRLogUtil.kr_w(' - geosite 文件存在: ${geositeFile.existsSync()}', tag: 'SingBox');
ruleSets.add({
"type": "remote",
"tag": "geoip-$currentCountryCode",
"format": "binary",
"url": "https://raw.githubusercontent.com/hiddify/hiddify-geo/rule-set/country/geoip-$currentCountryCode.srs",
"download_detour": "direct",
"update_interval": "7d"
});
ruleSets.add({
"type": "remote",
"tag": "geosite-$currentCountryCode",
"format": "binary",
"url": "https://raw.githubusercontent.com/hiddify/hiddify-geo/rule-set/country/geosite-$currentCountryCode.srs",
"download_detour": "direct",
"update_interval": "7d"
});
}
}
return ruleSets;
}
Future<void> kr_start() async {
// libcore status stream
try {
@ -1211,9 +1362,33 @@ class KRSingBoxImp {
// - v2.0-lazy-load
KRLogUtil.kr_i('🚀🚀🚀 [v2.0-lazy-load] 开始启动 SingBox...', tag: 'SingBox');
// 🔧
if (kr_outbounds.isNotEmpty) {
KRLogUtil.kr_i('🔄 启动前强制重新生成配置文件...', tag: 'SingBox');
kr_saveOutbounds(kr_outbounds);
//
await Future.delayed(const Duration(milliseconds: 100));
}
KRLogUtil.kr_i('📁 配置文件路径: $_cutPath', tag: 'SingBox');
KRLogUtil.kr_i('📝 配置名称: $kr_configName', tag: 'SingBox');
// 🔑 Windows DNS
if (Platform.isWindows) {
KRLogUtil.kr_i('🪟 Windows 平台,备份 DNS 设置...', tag: 'SingBox');
try {
final backupSuccess = await KRWindowsDnsUtil.instance.kr_backupDnsSettings();
if (backupSuccess) {
KRLogUtil.kr_i('✅ Windows DNS 备份成功', tag: 'SingBox');
} else {
KRLogUtil.kr_w('⚠️ Windows DNS 备份失败,将在停止时使用兜底恢复', tag: 'SingBox');
}
} catch (e) {
KRLogUtil.kr_w('⚠️ Windows DNS 备份异常: $e,将在停止时使用兜底恢复', tag: 'SingBox');
}
}
// 🔑 command.sock
// libcore "command.sock: no such file"
try {
@ -1367,12 +1542,12 @@ class KRSingBoxImp {
await Future.delayed(const Duration(milliseconds: 100));
// , stop()
//
// 🔧 10 Windows DNS
try {
await kr_singBox.stop().run().timeout(
const Duration(seconds: 3),
const Duration(seconds: 10), // 3 10
onTimeout: () {
KRLogUtil.kr_w('⚠️ 停止操作超时(3秒),强制继续', tag: 'SingBox');
KRLogUtil.kr_w('⚠️ 停止操作超时(10秒),强制继续', tag: 'SingBox');
return const Left('timeout');
},
);
@ -1381,7 +1556,29 @@ class KRSingBoxImp {
//
}
await Future.delayed(const Duration(milliseconds: 500));
// 🔑 Windows DNS
if (Platform.isWindows) {
KRLogUtil.kr_i('🪟 Windows 平台,开始恢复 DNS 设置...', tag: 'SingBox');
// sing-box
await Future.delayed(const Duration(milliseconds: 1000));
try {
// DNS
final restoreSuccess = await KRWindowsDnsUtil.instance.kr_restoreDnsSettings();
if (restoreSuccess) {
KRLogUtil.kr_i('✅ Windows DNS 恢复成功', tag: 'SingBox');
} else {
KRLogUtil.kr_e('❌ Windows DNS 恢复失败', tag: 'SingBox');
}
} catch (e) {
KRLogUtil.kr_e('❌ Windows DNS 恢复异常: $e', tag: 'SingBox');
//
}
} else {
// Windows
await Future.delayed(const Duration(milliseconds: 500));
}
// 便
final subscriptionsToCancel = _kr_subscriptions.where((sub) {
@ -1403,6 +1600,17 @@ class KRSingBoxImp {
} catch (e, stackTrace) {
KRLogUtil.kr_e('停止服务时出错: $e');
KRLogUtil.kr_e('错误堆栈: $stackTrace');
// 🔑 使 Windows DNS
if (Platform.isWindows) {
KRLogUtil.kr_w('⚠️ 停止异常,强制执行 DNS 恢复', tag: 'SingBox');
try {
await KRWindowsDnsUtil.instance.kr_restoreDnsSettings();
} catch (dnsError) {
KRLogUtil.kr_e('❌ 强制 DNS 恢复失败: $dnsError', tag: 'SingBox');
}
}
// libcore
rethrow;
}
@ -1426,10 +1634,25 @@ class KRSingBoxImp {
}
Future<void> kr_restart() async {
KRLogUtil.kr_i("restart");
kr_singBox.restart(_cutPath, kr_configName, false).mapLeft((err) {
KRLogUtil.kr_e('重启失败: $err');
}).run();
KRLogUtil.kr_i('🔄 重启 SingBox...', tag: 'SingBox');
// 🔧 使 stop + start restart
// Windows DNS
try {
// 1. DNS
await kr_stop();
// 2.
await Future.delayed(const Duration(milliseconds: 500));
// 3. DNS
await kr_start();
KRLogUtil.kr_i('✅ SingBox 重启完成', tag: 'SingBox');
} catch (e) {
KRLogUtil.kr_e('❌ 重启失败: $e', tag: 'SingBox');
rethrow;
}
}
////
@ -1571,9 +1794,11 @@ class KRSingBoxImp {
await _kr_ensureCommandClientInitialized();
KRLogUtil.kr_i('✅ Command client 已就绪,执行节点切换', tag: 'SingBox');
// 🔧 使 await
KRLogUtil.kr_i('⏳ 调用 selectOutbound("select", "$tag")...', tag: 'SingBox');
await _kr_selectOutboundWithRetry("select", tag, maxAttempts: 3, initialDelay: 50);
// 🔧 使 group tag
// libcore selector组的tag是"proxy""select"
final selectorGroupTag = kr_activeGroups.any((g) => g.tag == 'select') ? 'select' : 'proxy';
KRLogUtil.kr_i('⏳ 调用 selectOutbound("$selectorGroupTag", "$tag")...', tag: 'SingBox');
await _kr_selectOutboundWithRetry(selectorGroupTag, tag, maxAttempts: 3, initialDelay: 50);
KRLogUtil.kr_i('✅ 节点切换API调用完成: $tag', tag: 'SingBox');
// 🔧
@ -1607,7 +1832,6 @@ class KRSingBoxImp {
}
}
///
Directory get directory =>
@ -1657,221 +1881,4 @@ class KRSingBoxImp {
KRLogUtil.kr_e('📚 错误详情: ${e.toString()}', tag: 'SingBox');
}
}
Future<void> kr_debugTunConnectivity() async {
KRLogUtil.kr_i('🧪 [TUN Debug] 开始调试', tag: 'SingBoxTun');
final statusType = kr_status.value.runtimeType.toString();
KRLogUtil.kr_i('🔎 当前状态: $statusType', tag: 'SingBoxTun');
final selectedCountry = await KRSecureStorage().kr_readData(key: 'SELECTED_COUNTRY_TAG');
final selectedNode = await KRSecureStorage().kr_readData(key: 'SELECTED_NODE_TAG');
KRLogUtil.kr_i('🎯 选择: country=$selectedCountry, node=$selectedNode', tag: 'SingBox');
final activeTags = kr_activeGroups.map((g) => g.tag).join(', ');
KRLogUtil.kr_i('🧩 分组: all=${kr_allGroups.length}, active=${kr_activeGroups.length} [$activeTags]', tag: 'SingBoxTun');
// active
for (var group in kr_activeGroups) {
KRLogUtil.kr_i(
'📊 组[${group.tag}] 类型=${group.type} 节点数=${group.items.length}',
tag: 'SingBoxTun');
final selected = group.selected;
KRLogUtil.kr_i('🎯 当前选中节点: $selected', tag: 'SingBoxTun');
}
// 1 TCP
try {
final sock =
await Socket.connect('1.1.1.1', 443, timeout: const Duration(seconds: 3));
sock.destroy();
KRLogUtil.kr_i('🌐 TCP 443连接成功 (1.1.1.1:443)', tag: 'SingBoxTun');
} catch (e) {
KRLogUtil.kr_e('🌐 TCP 443连接失败: $e', tag: 'SingBoxTun');
}
// 2 HTTPS Google Connectivity
try {
final client = HttpClient()..connectionTimeout = const Duration(seconds: 3);
final req = await client
.getUrl(Uri.parse('https://connectivitycheck.gstatic.com/generate_204'));
req.headers.add('User-Agent', 'kr-debug');
final resp = await req.close();
KRLogUtil.kr_i('🔐 HTTPS 204 状态: ${resp.statusCode}', tag: 'SingBoxTun');
} catch (e) {
KRLogUtil.kr_e('🔐 HTTPS 204 错误: $e', tag: 'SingBoxTun');
}
// 3 DNS解析
try {
final addrs = await InternetAddress.lookup('google.com');
KRLogUtil.kr_i(
'🧭 DNS解析成功: ${addrs.map((a) => a.address).join(", ")}',
tag: 'SingBoxTun');
} catch (e) {
KRLogUtil.kr_e('🧭 DNS解析错误: $e', tag: 'SingBoxTun');
}
// 3 DNS解析
try {
final addrs = await InternetAddress.lookup('1.1.1.1');
KRLogUtil.kr_i(
'🧭 DNS解析成功1.1.1.1: ${addrs.map((a) => a.address).join(", ")}',
tag: 'SingBoxTun');
} catch (e) {
KRLogUtil.kr_e('🧭 DNS解析错误1.1.1.1: $e', tag: 'SingBoxTun');
}
// 4 TCP 53
try {
final sock =
await Socket.connect('8.8.8.8', 53, timeout: const Duration(seconds: 3));
sock.destroy();
KRLogUtil.kr_i('🛰️ TCP 53连接成功 (8.8.8.8:53)', tag: 'SingBoxTun');
} catch (e) {
KRLogUtil.kr_w('🛰️ TCP 53连接失败: $e', tag: 'SingBoxTun');
}
// 5 IP
try {
final client = HttpClient()..connectionTimeout = const Duration(seconds: 3);
final req = await client.getUrl(Uri.parse('https://api.ipify.org'));
final resp = await req.close();
final ip = await resp.transform(utf8.decoder).join();
KRLogUtil.kr_i('🌍 出口IP检测成功: $ip', tag: 'SingBoxTun');
} catch (e) {
KRLogUtil.kr_w('🌍 出口IP检测失败: $e', tag: 'SingBoxTun');
}
// 6
try {
final connectivity = await (Connectivity().checkConnectivity());
KRLogUtil.kr_i('📶 当前网络类型: $connectivity', tag: 'SingBoxTun');
} catch (e) {
KRLogUtil.kr_w('📶 网络类型检测失败: $e', tag: 'SingBoxTun');
}
// 7 HTTP延迟测试
try {
final sw = Stopwatch()..start();
final client = HttpClient()..connectionTimeout = const Duration(seconds: 3);
final req = await client
.getUrl(Uri.parse('https://www.google.com/generate_204'));
final resp = await req.close();
sw.stop();
KRLogUtil.kr_i('⏱️ HTTP延迟: ${sw.elapsedMilliseconds}ms (status=${resp.statusCode})',
tag: 'SingBoxTun');
} catch (e) {
KRLogUtil.kr_w('⏱️ HTTP延迟测试失败: $e', tag: 'SingBoxTun');
}
// 7 geoip API
try {
final client = HttpClient();
final req = await client.getUrl(Uri.parse('https://ipapi.co/json/'));
final resp = await req.close();
final data = await resp.transform(utf8.decoder).join();
KRLogUtil.kr_i('🌎 GeoIP: $data', tag: 'SingBoxTun');
} catch (e) {
KRLogUtil.kr_w('🌎 GeoIP检测失败: $e', tag: 'SingBoxTun');
}
// 8
final stopwatch = Stopwatch()..start();
try {
final client = HttpClient();
final req = await client.getUrl(Uri.parse('https://speed.cloudflare.com/__down?bytes=1000000'));
final resp = await req.close();
final totalBytes = await resp.fold<int>(0, (prev, e) => prev + e.length);
stopwatch.stop();
KRLogUtil.kr_i('⚡ 下载测试: ${totalBytes ~/ 1024} KB in ${stopwatch.elapsedMilliseconds}ms', tag: 'SingBoxTun');
} catch (e) {
KRLogUtil.kr_w('⚡ 下载测试失败: $e', tag: 'SingBoxTun');
}
KRLogUtil.kr_i('✅ [TUN Debug] 结束调试', tag: 'SingBoxTun');
}
// auto
//
// 1) activeGroups tag
// 2) 使 selected items
// 3) 退 items selected
SingboxOutboundGroupItem? _kr_resolveCurrentNodeForCountry(String? selectedCountry) {
if (selectedCountry == null || selectedCountry.isEmpty) return null;
final countryLower = selectedCountry.toLowerCase();
// 1) tag
for (final g in kr_activeGroups) {
final tagLower = g.tag.toLowerCase();
final tagMatch = tagLower == countryLower || tagLower.contains(countryLower);
if (tagMatch) {
final selTag = g.selected;
for (final item in g.items) {
if (item.tag == selTag) return item;
}
// selected null
return null;
}
}
// 2) items
for (final g in kr_activeGroups) {
final maybeCountryRelated = g.items.any(
(i) => i.tag.toLowerCase().contains(countryLower));
if (maybeCountryRelated) {
final selTag = g.selected;
for (final item in g.items) {
if (item.tag == selTag) return item;
}
}
}
return null;
}
/// active-groups
void kr_startAutoSwitchDebug() {
//
final sub = kr_singBox.watchActiveGroups().listen((groups) {
//
kr_activeGroups.value = groups;
for (final g in groups) {
final selected = g.selected;
final selItem = g.items.firstWhere(
(i) => i.tag == selected,
orElse: () => SingboxOutboundGroupItem(
tag: '(未知)',
type: ProxyType.unknown,
urlTestDelay: -1,
),
);
KRLogUtil.kr_i(
'🔄 组[${g.tag}] 选中 -> ${selItem.tag} (${selItem.type.label}), delay=${selItem.urlTestDelay}ms',
tag: 'SingBoxAuto',
);
}
}, onError: (e) {
KRLogUtil.kr_e('❌ AutoSwitch 调试订阅错误: $e', tag: 'SingBoxAuto');
});
_kr_subscriptions.add(sub);
KRLogUtil.kr_i('✅ AutoSwitch 调试订阅已开启', tag: 'SingBox');
}
/// DoH DNS 便 DNS
Future<void> kr_probeDoH(List<String> names) async {
final client = HttpClient()..connectionTimeout = const Duration(seconds: 5);
client.findProxy = (_) => kr_buildProxyRule();
for (final name in names) {
try {
final uri = Uri.parse('https://dns.google/resolve?name=$name&type=A');
final req = await client.getUrl(uri);
final resp = await req.close();
final body = await resp.transform(utf8.decoder).join();
KRLogUtil.kr_i('🧪 DoH 解析 $name -> 状态 ${resp.statusCode}, 响应 $body', tag: 'SingBoxDoH');
} catch (e) {
KRLogUtil.kr_w('🧪 DoH 解析 $name 失败: $e', tag: 'SingBoxDoH');
}
}
}
}

View File

@ -15,7 +15,8 @@ enum KRCountry {
ru('俄罗斯'),
id('印度尼西亚'),
tr('土耳其'),
br('巴西');
br('巴西'),
other('全局代理'); //
final String kr_name;
const KRCountry(this.kr_name);
@ -30,7 +31,7 @@ enum KRCountry {
static KRCountry? kr_fromCode(String code) {
try {
return KRCountry.values.firstWhere(
(country) => country.kr_code == code.toLowerCase(),
(country) => country.kr_code == code.toLowerCase(),
);
} catch (e) {
return null;
@ -53,7 +54,7 @@ class KRCountryUtil {
static Future<void> kr_init() async {
try {
final String? kr_savedCountry =
await _kr_storage.kr_readData(key: _kr_countryKey);
await _kr_storage.kr_readData(key: _kr_countryKey);
if (kr_savedCountry != null) {
final KRCountry? kr_country = KRCountry.kr_fromCode(kr_savedCountry);
if (kr_country != null) {
@ -66,7 +67,7 @@ class KRCountryUtil {
} else {
kr_currentCountry.value = KRCountry.cn;
}
} catch (err) {
KRLogUtil.kr_e('初始化国家设置失败: $err', tag: 'CountryUtil');
kr_currentCountry.value = KRCountry.cn;
@ -106,9 +107,9 @@ class KRCountryUtil {
return kr_currentCountry.value.kr_code;
}
static String kr_getCurrentCountryName() {
static String kr_getCurrentCountryName() {
return kr_getCountryName(kr_currentCountry.value);
}
///
@ -131,12 +132,14 @@ class KRCountryUtil {
return AppTranslations.kr_country.tr;
case KRCountry.br:
return AppTranslations.kr_country.br;
case KRCountry.other:
return '全局代理'; //
}
}
///
static List<KRCountry> kr_getSupportedCountries() {
if (AppConfig().kr_is_daytime == false) {
if (AppConfig().kr_is_daytime == false) {
return KRCountry.values.where((element) => element != KRCountry.cn).toList();
}
return KRCountry.values;
@ -146,9 +149,9 @@ class KRCountryUtil {
static List<Map<String, String>> kr_getCountryInfoList() {
return KRCountry.values
.map((country) => {
'code': country.kr_code,
'name': country.kr_countryName,
})
'code': country.kr_code,
'name': country.kr_countryName,
})
.toList();
}
}

View File

@ -0,0 +1,439 @@
import 'dart:io';
import 'package:kaer_with_panels/app/utils/kr_log_util.dart';
/// Windows DNS
///
/// Windows DNS
///
/// 1. DNS
/// 2. DNS
/// 3. DNS (223.5.5.5 114.114.114.114)
class KRWindowsDnsUtil {
///
KRWindowsDnsUtil._();
///
static final KRWindowsDnsUtil _instance = KRWindowsDnsUtil._();
///
factory KRWindowsDnsUtil() => _instance;
///
static KRWindowsDnsUtil get instance => _instance;
/// DNS
List<String>? _originalDnsServers;
///
String? _primaryInterfaceName;
/// DNS
///
/// VPN DNS
/// true-false-
Future<bool> kr_backupDnsSettings() async {
if (!Platform.isWindows) {
KRLogUtil.kr_w('❌ 非 Windows 平台,跳过 DNS 备份', tag: 'WindowsDNS');
return false;
}
try {
KRLogUtil.kr_i('📦 开始备份 Windows DNS 设置...', tag: 'WindowsDNS');
// 1.
final interfaceName = await _kr_getPrimaryNetworkInterface();
if (interfaceName == null) {
KRLogUtil.kr_e('❌ 无法获取主网络接口', tag: 'WindowsDNS');
return false;
}
_primaryInterfaceName = interfaceName;
KRLogUtil.kr_i('🔍 主网络接口: $_primaryInterfaceName', tag: 'WindowsDNS');
// 2. DNS
final dnsServers = await _kr_getCurrentDnsServers(interfaceName);
if (dnsServers.isEmpty) {
KRLogUtil.kr_w('⚠️ 当前 DNS 为空,可能是自动获取', tag: 'WindowsDNS');
_originalDnsServers = []; // DHCP
} else {
_originalDnsServers = dnsServers;
KRLogUtil.kr_i('✅ 已备份 DNS: ${dnsServers.join(", ")}', tag: 'WindowsDNS');
}
return true;
} catch (e) {
KRLogUtil.kr_e('❌ 备份 DNS 设置失败: $e', tag: 'WindowsDNS');
return false;
}
}
/// DNS
///
/// VPN DNS
/// DNS
/// true-false-
Future<bool> kr_restoreDnsSettings() async {
if (!Platform.isWindows) {
KRLogUtil.kr_w('❌ 非 Windows 平台,跳过 DNS 恢复', tag: 'WindowsDNS');
return false;
}
try {
KRLogUtil.kr_i('🔄 开始恢复 Windows DNS 设置...', tag: 'WindowsDNS');
// 1.
if (_primaryInterfaceName == null) {
KRLogUtil.kr_w('⚠️ 没有备份的网络接口,尝试自动检测', tag: 'WindowsDNS');
_primaryInterfaceName = await _kr_getPrimaryNetworkInterface();
if (_primaryInterfaceName == null) {
KRLogUtil.kr_e('❌ 无法检测网络接口,执行兜底恢复', tag: 'WindowsDNS');
return await _kr_fallbackRestoreDns();
}
}
// 2. DNS
if (_originalDnsServers == null) {
KRLogUtil.kr_w('⚠️ 没有备份的 DNS执行兜底恢复', tag: 'WindowsDNS');
return await _kr_fallbackRestoreDns();
}
if (_originalDnsServers!.isEmpty) {
// DHCP
KRLogUtil.kr_i('🔄 恢复为 DHCP 自动获取 DNS', tag: 'WindowsDNS');
final success = await _kr_setDnsToAuto(_primaryInterfaceName!);
if (!success) {
KRLogUtil.kr_w('⚠️ 恢复 DHCP 失败,执行兜底恢复', tag: 'WindowsDNS');
return await _kr_fallbackRestoreDns();
}
} else {
// DNS
KRLogUtil.kr_i('🔄 恢复原始 DNS: ${_originalDnsServers!.join(", ")}', tag: 'WindowsDNS');
final success = await _kr_setDnsServers(
_primaryInterfaceName!,
_originalDnsServers!,
);
if (!success) {
KRLogUtil.kr_w('⚠️ 恢复原始 DNS 失败,执行兜底恢复', tag: 'WindowsDNS');
return await _kr_fallbackRestoreDns();
}
}
// 3. DNS
await Future.delayed(const Duration(milliseconds: 500));
final currentDns = await _kr_getCurrentDnsServers(_primaryInterfaceName!);
KRLogUtil.kr_i('✅ 当前 DNS: ${currentDns.join(", ")}', tag: 'WindowsDNS');
// 4. DNS 127.0.0.1sing-box DNS
final hasLocalhost = currentDns.any((dns) => dns.startsWith('127.'));
if (hasLocalhost) {
KRLogUtil.kr_w('⚠️ DNS 仍包含 127.0.0.1,可能未完全恢复', tag: 'WindowsDNS');
KRLogUtil.kr_w('⚠️ 执行兜底恢复', tag: 'WindowsDNS');
return await _kr_fallbackRestoreDns();
}
return true;
} catch (e) {
KRLogUtil.kr_e('❌ 恢复 DNS 设置失败: $e', tag: 'WindowsDNS');
KRLogUtil.kr_w('⚠️ 执行兜底恢复', tag: 'WindowsDNS');
return await _kr_fallbackRestoreDns();
}
}
/// DNS
///
/// DNS
/// - DNS: 223.5.5.5 ()
/// - DNS: 114.114.114.114 (114DNS)
Future<bool> _kr_fallbackRestoreDns() async {
try {
KRLogUtil.kr_w('🆘 执行 DNS 兜底恢复机制', tag: 'WindowsDNS');
KRLogUtil.kr_i('🔧 设置为国内公共 DNS: 223.5.5.5, 114.114.114.114', tag: 'WindowsDNS');
// 1.
if (_primaryInterfaceName == null) {
_primaryInterfaceName = await _kr_getPrimaryNetworkInterface();
if (_primaryInterfaceName == null) {
KRLogUtil.kr_e('❌ 无法检测网络接口,兜底恢复失败', tag: 'WindowsDNS');
return false;
}
}
// 2. DNS
final fallbackDns = ['223.5.5.5', '114.114.114.114'];
final success = await _kr_setDnsServers(_primaryInterfaceName!, fallbackDns);
if (success) {
KRLogUtil.kr_i('✅ 兜底 DNS 设置成功', tag: 'WindowsDNS');
// 3.
await Future.delayed(const Duration(milliseconds: 500));
final currentDns = await _kr_getCurrentDnsServers(_primaryInterfaceName!);
KRLogUtil.kr_i('✅ 验证当前 DNS: ${currentDns.join(", ")}', tag: 'WindowsDNS');
return true;
} else {
KRLogUtil.kr_e('❌ 兜底 DNS 设置失败', tag: 'WindowsDNS');
return false;
}
} catch (e) {
KRLogUtil.kr_e('❌ 兜底恢复失败: $e', tag: 'WindowsDNS');
return false;
}
}
///
///
/// netsh "已连接"
/// null
Future<String?> _kr_getPrimaryNetworkInterface() async {
try {
// 使 netsh
final result = await Process.run('netsh', ['interface', 'show', 'interface']);
if (result.exitCode != 0) {
KRLogUtil.kr_e('❌ 获取网络接口失败: ${result.stderr}', tag: 'WindowsDNS');
return null;
}
final output = result.stdout.toString();
final lines = output.split('\n');
//
final connectedInterfaces = <String>[];
// "已连接"
// Windows
for (var line in lines) {
// : "已连接", : "Connected", "Enabled"
if (line.contains('已连接') ||
line.contains('Connected') ||
line.toLowerCase().contains('enabled')) {
//
if (line.contains('Admin') ||
line.contains('管理') ||
line.contains('State') ||
line.contains('状态')) {
continue;
}
//
final parts = line.trim().split(RegExp(r'\s{2,}'));
if (parts.length >= 4) {
final interfaceName = parts.last.trim();
//
if (interfaceName.isNotEmpty && interfaceName.length > 1) {
connectedInterfaces.add(interfaceName);
}
}
}
}
if (connectedInterfaces.isEmpty) {
KRLogUtil.kr_w('⚠️ 未找到已连接的网络接口', tag: 'WindowsDNS');
return null;
}
// 🔧 线 Wi-Fi
// 线
String? selectedInterface;
for (var interface in connectedInterfaces) {
final lowerName = interface.toLowerCase();
//
if (lowerName.contains('ethernet') ||
lowerName.contains('以太网') ||
lowerName.contains('lan') ||
lowerName.contains('local')) {
selectedInterface = interface;
KRLogUtil.kr_i('🔍 选择有线网络接口: $interface', tag: 'WindowsDNS');
break;
}
}
// 线 Wi-Fi
selectedInterface ??= connectedInterfaces.first;
if (selectedInterface != connectedInterfaces.first) {
KRLogUtil.kr_d('🔍 选择网络接口: $selectedInterface', tag: 'WindowsDNS');
} else {
KRLogUtil.kr_i('🔍 选择网络接口: $selectedInterface', tag: 'WindowsDNS');
}
return selectedInterface;
} catch (e) {
KRLogUtil.kr_e('❌ 获取网络接口异常: $e', tag: 'WindowsDNS');
return null;
}
}
/// DNS
///
///
/// - interfaceName:
///
/// DNS
Future<List<String>> _kr_getCurrentDnsServers(String interfaceName) async {
try {
final result = await Process.run('netsh', [
'interface',
'ipv4',
'show',
'dnsservers',
'name="$interfaceName"',
]);
if (result.exitCode != 0) {
KRLogUtil.kr_e('❌ 获取 DNS 失败: ${result.stderr}', tag: 'WindowsDNS');
return [];
}
final output = result.stdout.toString();
final dnsServers = <String>[];
// DNS
final lines = output.split('\n');
for (var line in lines) {
// IP
final ipMatch = RegExp(r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b').firstMatch(line);
if (ipMatch != null) {
final ip = ipMatch.group(0)!;
//
if (!ip.startsWith('127.')) {
dnsServers.add(ip);
}
}
}
KRLogUtil.kr_d('🔍 当前 DNS: ${dnsServers.join(", ")}', tag: 'WindowsDNS');
return dnsServers;
} catch (e) {
KRLogUtil.kr_e('❌ 获取 DNS 异常: $e', tag: 'WindowsDNS');
return [];
}
}
/// DNS
///
///
/// - interfaceName:
/// - dnsServers: DNS
///
/// true-false-
Future<bool> _kr_setDnsServers(String interfaceName, List<String> dnsServers) async {
try {
if (dnsServers.isEmpty) {
KRLogUtil.kr_w('⚠️ DNS 列表为空,无法设置', tag: 'WindowsDNS');
return false;
}
// 1. DNS
KRLogUtil.kr_i('🔧 设置主 DNS: ${dnsServers[0]}', tag: 'WindowsDNS');
var result = await Process.run('netsh', [
'interface',
'ipv4',
'set',
'dnsservers',
'name="$interfaceName"',
'source=static',
'address=${dnsServers[0]}',
'validate=no',
]);
if (result.exitCode != 0) {
KRLogUtil.kr_e('❌ 设置主 DNS 失败: ${result.stderr}', tag: 'WindowsDNS');
return false;
}
// 2. DNS
if (dnsServers.length > 1) {
for (int i = 1; i < dnsServers.length; i++) {
KRLogUtil.kr_i('🔧 设置备用 DNS ${i}: ${dnsServers[i]}', tag: 'WindowsDNS');
result = await Process.run('netsh', [
'interface',
'ipv4',
'add',
'dnsservers',
'name="$interfaceName"',
'address=${dnsServers[i]}',
'index=${i + 1}',
'validate=no',
]);
if (result.exitCode != 0) {
KRLogUtil.kr_w('⚠️ 设置备用 DNS $i 失败: ${result.stderr}', tag: 'WindowsDNS');
//
}
}
}
// 3. DNS
await _kr_flushDnsCache();
KRLogUtil.kr_i('✅ DNS 服务器设置完成', tag: 'WindowsDNS');
return true;
} catch (e) {
KRLogUtil.kr_e('❌ 设置 DNS 异常: $e', tag: 'WindowsDNS');
return false;
}
}
/// DNS DHCP
///
///
/// - interfaceName:
///
/// true-false-
Future<bool> _kr_setDnsToAuto(String interfaceName) async {
try {
KRLogUtil.kr_i('🔧 设置 DNS 为自动获取 (DHCP)', tag: 'WindowsDNS');
final result = await Process.run('netsh', [
'interface',
'ipv4',
'set',
'dnsservers',
'name="$interfaceName"',
'source=dhcp',
]);
if (result.exitCode != 0) {
KRLogUtil.kr_e('❌ 设置 DHCP 失败: ${result.stderr}', tag: 'WindowsDNS');
return false;
}
// DNS
await _kr_flushDnsCache();
KRLogUtil.kr_i('✅ DNS 已设置为自动获取', tag: 'WindowsDNS');
return true;
} catch (e) {
KRLogUtil.kr_e('❌ 设置 DHCP 异常: $e', tag: 'WindowsDNS');
return false;
}
}
/// DNS
///
/// ipconfig /flushdns DNS
Future<void> _kr_flushDnsCache() async {
try {
KRLogUtil.kr_i('🔄 刷新 DNS 缓存...', tag: 'WindowsDNS');
final result = await Process.run('ipconfig', ['/flushdns']);
if (result.exitCode == 0) {
KRLogUtil.kr_i('✅ DNS 缓存已刷新', tag: 'WindowsDNS');
} else {
KRLogUtil.kr_w('⚠️ 刷新 DNS 缓存失败: ${result.stderr}', tag: 'WindowsDNS');
}
} catch (e) {
KRLogUtil.kr_w('⚠️ 刷新 DNS 缓存异常: $e', tag: 'WindowsDNS');
}
}
///
///
/// 退
void kr_clearBackup() {
_originalDnsServers = null;
_primaryInterfaceName = null;
KRLogUtil.kr_d('🗑️ 已清除 DNS 备份数据', tag: 'WindowsDNS');
}
}