feat: 节点测速调整

This commit is contained in:
speakeloudest 2025-11-25 23:06:20 -08:00
parent 1809c11473
commit 909020654f
6 changed files with 112 additions and 263 deletions

View File

@ -8,7 +8,7 @@ import 'package:flutter/foundation.dart';
///
class KrOutboundsList {
///
final List<KRGroupOutboundList> groupOutboundList = []; //
@ -18,20 +18,20 @@ class KrOutboundsList {
///
final List<KROutboundItem> allList = []; //
// json
final List<Map<String,dynamic>> configJsonList = [];
final List<Map<String,dynamic>> configJsonList = [];
///
final Map<String,KROutboundItem> keyList = {}; //
///
/// [list]
void processOutboundItems(List<KrNodeListItem> list,List<KRNodeGroupListItem> groupList) {
final Map<String, List<KROutboundItem>> tagGroups = {};
final Map<String, List<KROutboundItem>> countryGroups = {};
// 使
final Map<String, int> tagCounter = {};
@ -88,14 +88,11 @@ class KrOutboundsList {
}
}
//
_generateCountryAutoNodes(countryGroups);
// KRGroupOutboundList groupOutboundList
for (var tag in tagGroups.keys) {
final item = KRGroupOutboundList(
final item = KRGroupOutboundList(
tag: tag, outboundList: tagGroups[tag]!);
for (var group in groupList) {
if (item.tag == group.name) {
item.icon = group.icon;
@ -111,41 +108,7 @@ class KrOutboundsList {
country: country,
outboundList: countryGroups[country]!)); //
}
}
///
void _generateCountryAutoNodes(Map<String, List<KROutboundItem>> countryGroups) {
for (var entry in countryGroups.entries) {
final country = entry.key;
final nodes = entry.value;
final autoTag = '${country}-auto';
if (kDebugMode) {
print('🤖 生成国家自动选择节点: $autoTag, 包含 ${nodes.length} 个节点');
}
// urltest
final urltestConfig = {
'type': 'urltest',
'tag': autoTag,
'outbounds': nodes.map((node) => node.tag).toList(),
'url': 'https://www.google.com/generate_204',
'interval': '10m',
'tolerance': 50,
"interrupt_exist_connections": true,
};
//
final virtualNode = KROutboundItem.fromVirtual(autoTag, country, urltestConfig);
//
allList.add(virtualNode);
keyList[autoTag] = virtualNode;
// configJsonList.add(urltestConfig);
if (kDebugMode) {
print('✅ 生成虚拟节点: $autoTag, 配置: ${urltestConfig.toString()}');
}
}
}
}

View File

@ -95,6 +95,13 @@ class HINodeListController extends GetxController with WidgetsBindingObserver {
KRLogUtil.kr_i('调试模式已重置', tag: 'HINodeListController');
}
Future<void> kr_handleRefresh() async {
await kr_subscribeService.kr_refreshAll();
if (!homeController.kr_isLatency.value) {
homeController.kr_urlTest();
}
}
@override
void onInit() {
super.onInit();

View File

@ -23,22 +23,6 @@ class HINodeListView extends GetView<HINodeListController> {
static const Color krModernGreen = Color(0xFF4CAF50);
static const Color krModernGreenLight = Color(0xFF81C784);
// UI展示
static final Map<String, int> _fakeDelays = {};
///
int _getDisplayDelay(HINodeListController controller, KROutboundItem item) {
return item.urlTestDelay.value;
// if (controller.homeController.kr_isConnected.value) {
//
// }
// if (!_fakeDelays.containsKey(item.tag)) {
// final random = Random();
// _fakeDelays[item.tag] = 30 + random.nextInt(71); // 30-100ms
// }
// return _fakeDelays[item.tag] ?? 0;
}
/// ms
/// 0
int getFastestNodeDelay(
@ -77,14 +61,18 @@ class HINodeListView extends GetView<HINodeListController> {
Widget build(BuildContext context) {
// 1. 使 Material InkWell
//
return Material(
color: Colors.transparent,
child: _buildSubscribeList(context)
// child: _kr_buildRegionList(context)
);
return Obx(() {
// Material InkWell
return Material(
color: Colors.transparent,
child: controller.isDebugMode.value
? _buildSubscribeList(context)
: _kr_buildRegionList(context),
);
});
}
/// /使
/// /
Widget _kr_buildRegionList(BuildContext context) {
return Obx(() {
return _kr_buildListContainer(
@ -185,10 +173,8 @@ class HINodeListView extends GetView<HINodeListController> {
return InkWell(
onTap: () async {
try {
final success =
await controller.homeController.kr_performNodeSwitch('${country.country}-auto');
print('node 点击 ${country.country} 节点数量${country.outboundList.length} 节点详情 ${country.outboundList}');
await controller.homeController.kr_performNodeSwitch('auto');
if (success) {
controller.homeController.kr_currentListStatus.value =
KRHomeViewsListStatus.kr_none;
@ -216,10 +202,7 @@ class HINodeListView extends GetView<HINodeListController> {
padding: EdgeInsets.symmetric(vertical: 8.w),
// 2. 使 children
children: [
if (controller.kr_getFilteredNodeList().isEmpty)
_buildEmptyListPlaceholder(context,
controller.isDebugMode.value ? '调试模式:无节点数据' : AppTranslations.kr_home.noNodes)
else ...[
...[
InkWell(
// 🔧 async
onTap: () async {
@ -327,7 +310,7 @@ class HINodeListView extends GetView<HINodeListController> {
),
),
),
...controller.kr_getFilteredNodeList().map((item) {
...controller.kr_subscribeService.allList().map((item) {
return InkWell(
// 🔧 async
onTap: () async {
@ -423,18 +406,9 @@ class HINodeListView extends GetView<HINodeListController> {
//
print('🔄 _kr_buildNodeListItem节点: item.tag=${item.tag}, country=${item.country}');
// auto组节点
if (item.tag.endsWith('-auto')) {
print('🎯 检测到auto组节点获取最快节点信息');
final autoNodeInfo = controller.homeController.kr_getCountryAutoSelectedNode(item.country);
print('📊 _kr_buildNodeListItem获取auto最快节点: $autoNodeInfo');
displayDelay = autoNodeInfo?['delay'] ?? 0;
print('✅ _kr_buildNodeListItem使用auto最快节点延迟: $displayDelay');
} else {
//
displayDelay = item.urlTestDelay.value;
print('🔄 _kr_buildNodeListItem使用普通节点延迟: $displayDelay');
}
//
displayDelay = item.urlTestDelay.value;
print('🔄 _kr_buildNodeListItem使用普通节点延迟: $displayDelay');
// 2.
String delayText;

View File

@ -159,8 +159,7 @@ class HINodePageView extends GetView<HINodeListController> {
padding: EdgeInsets.only(left: 60.w, right: 60.w, bottom: 90.w), // HIHelpEntrance预留空间
// 2. Padding 使 EasyRefresh
child: EasyRefresh(
// 3. onRefresh
onRefresh: controller.kr_subscribeService.kr_refreshAll,
onRefresh: controller.kr_handleRefresh,
// 4. Header
header: ClassicHeader(
dragText: '下拉刷新',
@ -228,4 +227,4 @@ class HINodePageView extends GetView<HINodeListController> {
),
);
}
}
}

View File

@ -309,7 +309,6 @@ class KRSubscribeService {
//
KRSingBoxImp.instance.kr_saveOutbounds(listModel.configJsonList);
KRSingBoxImp.instance.kr_saveAllOutbounds(listModel.countryOutboundList);
//
_kr_updateSubscribeStatus();
@ -662,7 +661,6 @@ class KRSubscribeService {
//
KRSingBoxImp.instance.kr_saveOutbounds(listModel.configJsonList);
KRSingBoxImp.instance.kr_saveAllOutbounds(listModel.countryOutboundList);
//
_kr_updateSubscribeStatus();
@ -725,7 +723,6 @@ class KRSubscribeService {
//
KRSingBoxImp.instance.kr_saveOutbounds([]);
KRSingBoxImp.instance.kr_saveAllOutbounds([]);
}

View File

@ -12,15 +12,12 @@ import 'package:kaer_with_panels/singbox/service/singbox_service.dart';
import 'package:kaer_with_panels/singbox/service/singbox_service_provider.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
import 'package:device_info_plus/device_info_plus.dart';
import '../../../core/model/directories.dart';
import '../../../singbox/model/singbox_config_option.dart';
import '../../../singbox/model/singbox_outbound.dart';
import '../../../singbox/model/singbox_stats.dart';
import '../../../singbox/model/singbox_status.dart';
import '../../model/business/kr_group_outbound_list.dart';
import '../../model/business/kr_outbound_item.dart';
import '../../utils/kr_country_util.dart';
import '../../utils/kr_log_util.dart';
import '../../utils/kr_secure_storage.dart';
@ -68,7 +65,6 @@ class KRSingBoxImp {
Map<String, dynamic> kr_configOption = {};
List<Map<String, dynamic>> kr_outbounds = [];
List<KRCountryOutboundList> Kr_allOutbounds = [];
///
RxBool kr_isFristStart = false.obs;
@ -103,6 +99,9 @@ class KRSingBoxImp {
///
RxList<SingboxOutboundGroup> kr_activeGroups = <SingboxOutboundGroup>[].obs;
// 🔒 Windows DNS DNS
bool _dnsBackedUp = false;
///
RxList<SingboxOutboundGroup> kr_allGroups = <SingboxOutboundGroup>[].obs;
@ -115,62 +114,6 @@ class KRSingBoxImp {
/// Futuresingle-flight
Future<void>? _kr_initFuture;
/// Android模拟器
bool _kr_isAndroidEmulator = false;
///
///
/// - Android
/// - brand/model/product/manufacturer
/// `_kr_isAndroidEmulator`
Future<void> _kr_detectEmulator() async {
try {
if (!Platform.isAndroid) {
_kr_isAndroidEmulator = false;
return;
}
final info = await DeviceInfoPlugin().androidInfo;
final brand = (info.brand ?? '').toLowerCase();
final model = (info.model ?? '').toLowerCase();
final product = (info.product ?? '').toLowerCase();
final manufacturer = (info.manufacturer ?? '').toLowerCase();
final device = (info.device ?? '').toLowerCase();
bool containsAny(String s, List<String> keys) => keys.any((k) => s.contains(k));
final indicators = <bool>[
containsAny(brand, [
'generic', 'unknown', 'google', 'vbox', 'virtualbox',
'bluestacks', 'nox', 'ldplayer', 'genymotion', 'mumu', 'netease', 'leidian'
]),
containsAny(model, [
'emulator', 'sdk', 'sdk_gphone', 'google_sdk', 'android sdk built for x86',
'bluestacks', 'genymotion', 'nox', 'ldplayer', 'mumu'
]),
containsAny(product, [
'sdk', 'google_sdk', 'sdk_gphone', 'emulator', 'vbox', 'virtualbox'
]),
containsAny(manufacturer, [
'genymotion', 'unknown', 'bluestacks', 'nox', 'ld', 'netease', 'mumu'
]),
containsAny(device, [
'emulator', 'generic', 'vbox', 'virtualbox', 'sdk'
]),
];
_kr_isAndroidEmulator = indicators.any((v) => v);
KRLogUtil.kr_i(
'🔍 Android 模拟器检测: ${_kr_isAndroidEmulator ? '是模拟器' : '非模拟器'}'
' (brand=$brand, model=$model, product=$product, manufacturer=$manufacturer, device=$device)',
tag: 'SingBox',
);
} catch (e) {
_kr_isAndroidEmulator = false;
KRLogUtil.kr_w('⚠️ 模拟器检测失败,按真机处理: $e', tag: 'SingBox');
}
}
///
bool get kr_isProxyReady => kr_status.value is SingboxStarted;
@ -226,9 +169,6 @@ class KRSingBoxImp {
await KRCountryUtil.kr_init();
KRLogUtil.kr_i('国家工具初始化完成');
// Android模拟器
await _kr_detectEmulator();
final oOption = SingboxConfigOption.fromJson(_getConfigOption());
KRLogUtil.kr_i('配置选项初始化完成');
@ -617,11 +557,9 @@ class KRSingBoxImp {
"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": _kr_isAndroidEmulator
? "local"
: "https://dns.google/dns-query",
"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,
@ -743,56 +681,9 @@ class KRSingBoxImp {
addFromOutbound(g);
}
for (final g in Kr_allOutbounds) {
for (final it in g.outboundList) {
addFromOutbound(it.config);
}
}
return set;
}
/// DNS
///
/// - 使 DoH + DNSfinal
/// - 使 DNSfinal DNS
/// Sing-box `dns` Map
Map<String, dynamic> _kr_buildDnsConfig() {
if (_kr_isAndroidEmulator) {
KRLogUtil.kr_i('🛡️ 模拟器兼容模式:强制使用系统 DNS', tag: 'SingBox');
return {
"servers": [
{
"tag": "dns-direct",
"address": "local",
"detour": "direct"
}
],
"rules": [],
"final": "dns-direct",
"strategy": "prefer_ipv4"
};
}
return {
"servers": [
{
"tag": "dns-remote",
"address": "https://1.1.1.1/dns-query",
"address_resolver": "dns-direct"
},
{
"tag": "dns-direct",
"address": "local",
"detour": "direct"
}
],
"rules": _kr_buildDnsRules(),
"final": "dns-remote",
"strategy": "prefer_ipv4"
};
}
///
/// hiddify-app: libcore UI
void _kr_subscribeToStatus() {
@ -1206,7 +1097,7 @@ class KRSingBoxImp {
await Future.delayed(const Duration(milliseconds: 2000));
final savedNode = await KRSecureStorage().kr_readData(key: _keySelectedNode);
if (savedNode != null && savedNode.isNotEmpty && savedNode != 'auto' && !savedNode.endsWith('-auto')) {
if (savedNode != null && savedNode.isNotEmpty && savedNode != 'auto') {
KRLogUtil.kr_i('🔄 恢复用户选择的节点: $savedNode', tag: 'SingBox');
try {
@ -1247,14 +1138,7 @@ class KRSingBoxImp {
// print("错误堆栈: $stack");
// }
// }
void kr_saveAllOutbounds(List<KRCountryOutboundList> outbounds) {
Kr_allOutbounds = outbounds;
KRLogUtil.kr_i('📊 保存国家分组: ${Kr_allOutbounds.length}', tag: 'SingBox');
for (int i = 0; i < Kr_allOutbounds.length; i++) {
final c = Kr_allOutbounds[i];
KRLogUtil.kr_i(' group[$i] country="${c.country}" outbounds=${c.outboundList.length}', tag: 'SingBox');
}
}
///
void kr_saveOutbounds(List<Map<String, dynamic>> outbounds) async {
KRLogUtil.kr_i('💾 开始保存配置文件...', tag: 'SingBox');
@ -1303,7 +1187,23 @@ class KRSingBoxImp {
"level": "debug",
"timestamp": true
},
"dns": _kr_buildDnsConfig(),
"dns": {
"servers": [
{
"tag": "dns-remote",
"address": "https://1.1.1.1/dns-query",
"address_resolver": "dns-direct"
},
{
"tag": "dns-direct",
"address": "local",
"detour": "direct"
}
],
"rules": _kr_buildDnsRules(), // 使 DNS
"final": "dns-remote",
"strategy": "prefer_ipv4"
},
"inbounds": [
{
"type": "tun",
@ -1506,55 +1406,21 @@ class KRSingBoxImp {
// 🔧
if (kr_outbounds.isNotEmpty) {
KRLogUtil.kr_i('🔄 启动前强制重新生成配置文件...', tag: 'SingBox');
final selectedNode = await KRSecureStorage().kr_readData(key: _keySelectedNode);
KRLogUtil.kr_i('📊 国家分组数量: ${Kr_allOutbounds.length}', tag: 'SingBox');
for (int i = 0; i < Kr_allOutbounds.length; i++) {
final g = Kr_allOutbounds[i];
KRLogUtil.kr_i('Kr_allOutbounds[$i] country="${g.country}" outbounds=${g.outboundList.length}', tag: 'SingBox');
}
List<Map<String, dynamic>> toSave = [];
if (selectedNode != null && selectedNode.endsWith('-auto')) {
KRLogUtil.kr_i('🤖 检测到自动国家节点: $selectedNode', tag: 'SingBox');
String selectedCountry = selectedNode.replaceAll(RegExp(r'-auto$'), '');
final selectedCountryLower = selectedCountry.toLowerCase();
final matchedGroup = Kr_allOutbounds.firstWhere(
(g) => g.country.toLowerCase() == selectedCountryLower,
orElse: () => KRCountryOutboundList(country: '', outboundList: []),
);
if (matchedGroup.country.isNotEmpty) {
KRLogUtil.kr_i('🎯 命中分组: ${matchedGroup.country}, 节点数: ${matchedGroup.outboundList.length}', tag: 'SingBox');
toSave = matchedGroup.outboundList.map((it) => it.config).toList();
} else {
for (final g in Kr_allOutbounds) {
for (final it in g.outboundList) {
final tagLower = it.tag.toLowerCase();
if (tagLower == selectedNode.toLowerCase() || tagLower.contains(selectedCountryLower)) {
toSave.add(it.config);
}
}
}
}
KRLogUtil.kr_i('✅ 自动国家过滤: ${toSave.length}', tag: 'SingBox');
} else {
for (final g in Kr_allOutbounds) {
for (final it in g.outboundList) {
toSave.add(it.config);
}
}
}
kr_saveOutbounds(toSave);
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');
// 🔑 Windows DNS
if (Platform.isWindows && !_dnsBackedUp) {
KRLogUtil.kr_i('🪟 Windows 平台,首次启动备份 DNS 设置...', tag: 'SingBox');
try {
final backupSuccess = await KRWindowsDnsUtil.instance.kr_backupDnsSettings();
if (backupSuccess) {
_dnsBackedUp = true; //
KRLogUtil.kr_i('✅ Windows DNS 备份成功', tag: 'SingBox');
} else {
KRLogUtil.kr_w('⚠️ Windows DNS 备份失败,将在停止时使用兜底恢复', tag: 'SingBox');
@ -1562,6 +1428,8 @@ class KRSingBoxImp {
} catch (e) {
KRLogUtil.kr_w('⚠️ Windows DNS 备份异常: $e,将在停止时使用兜底恢复', tag: 'SingBox');
}
} else if (Platform.isWindows && _dnsBackedUp) {
KRLogUtil.kr_i('⏭️ Windows 平台DNS 已备份,跳过重复备份(节点切换优化)', tag: 'SingBox');
}
// 🔑 command.sock
@ -1731,12 +1599,42 @@ class KRSingBoxImp {
//
}
// 🔑 Windows DNS
if (Platform.isWindows) {
KRLogUtil.kr_i('🪟 Windows 平台,开始恢复 DNS 设置...', tag: 'SingBox');
// 🔑 Windows DNS
if (Platform.isWindows && _dnsBackedUp) {
KRLogUtil.kr_i('🪟 Windows 平台,等待 sing-box 完全停止...', tag: 'SingBox');
// sing-box
await Future.delayed(const Duration(milliseconds: 1000));
// 🔧 P3优化: sing-box DNS
try {
//
if (kr_status.value is SingboxStopped) {
KRLogUtil.kr_i('✅ sing-box 已经是停止状态,立即恢复 DNS', tag: 'SingBox');
} else {
// 3
final completer = Completer<void>();
late final Worker worker;
worker = ever(kr_status, (status) {
if (status is SingboxStopped) {
if (!completer.isCompleted) {
completer.complete();
worker.dispose();
}
}
});
await completer.future.timeout(
const Duration(seconds: 3),
onTimeout: () {
KRLogUtil.kr_w('⏱️ 等待停止状态超时,继续执行 DNS 恢复', tag: 'SingBox');
worker.dispose();
},
);
KRLogUtil.kr_i('✅ sing-box 已完全停止,开始恢复 DNS...', tag: 'SingBox');
}
} catch (e) {
KRLogUtil.kr_w('⚠️ 状态监听异常: $e,继续执行 DNS 恢复', tag: 'SingBox');
}
try {
// DNS
@ -1749,7 +1647,14 @@ class KRSingBoxImp {
} catch (e) {
KRLogUtil.kr_e('❌ Windows DNS 恢复异常: $e', tag: 'SingBox');
//
} finally {
// 🔧 P0修复2: 使 finally
_dnsBackedUp = false;
KRLogUtil.kr_i('🔄 重置 DNS 备份标志位', tag: 'SingBox');
}
} else if (Platform.isWindows && !_dnsBackedUp) {
KRLogUtil.kr_i('⏭️ Windows 平台DNS 未备份,跳过恢复', tag: 'SingBox');
await Future.delayed(const Duration(milliseconds: 500));
} else {
// Windows
await Future.delayed(const Duration(milliseconds: 500));
@ -1777,12 +1682,16 @@ class KRSingBoxImp {
KRLogUtil.kr_e('错误堆栈: $stackTrace');
// 🔑 使 Windows DNS
if (Platform.isWindows) {
if (Platform.isWindows && _dnsBackedUp) {
KRLogUtil.kr_w('⚠️ 停止异常,强制执行 DNS 恢复', tag: 'SingBox');
try {
await KRWindowsDnsUtil.instance.kr_restoreDnsSettings();
} catch (dnsError) {
KRLogUtil.kr_e('❌ 强制 DNS 恢复失败: $dnsError', tag: 'SingBox');
} finally {
// 🔧 P0修复2:
_dnsBackedUp = false;
KRLogUtil.kr_i('🔄 异常后重置 DNS 备份标志位', tag: 'SingBox');
}
}
@ -1989,7 +1898,7 @@ class KRSingBoxImp {
// 🔄 auto
// urltest
_nodeSelectionTimer?.cancel();
if (tag != 'auto' && !tag.endsWith('-auto')) {
if (tag != 'auto') {
KRLogUtil.kr_i('🔁 启动节点选择监控,防止被 auto 覆盖', tag: 'SingBox');
_nodeSelectionTimer = Timer.periodic(const Duration(seconds: 20), (timer) {
// 20