feat: 初始化并发问题处理
Some checks failed
Build Windows / 编译 libcore (Windows) (20.15.1) (push) Has been cancelled
Build Windows / build (push) Has been cancelled

This commit is contained in:
speakeloudest 2025-11-13 05:15:18 -08:00
parent 748ec6bee9
commit a47174df56
5 changed files with 400 additions and 118 deletions

View File

@ -28,13 +28,10 @@ class KROutboundItem {
/// URL
String url = "";
@override
String toString() {
return 'KROutboundItem(tag: $tag, country: $country, delay: ${urlTestDelay.value}ms)';
}
///
/// KrNodeListItem KROutboundItem
/// KrItem KROutboundItem
KROutboundItem(KrNodeListItem nodeListItem) {
id = nodeListItem.id.toString();
protocol = nodeListItem.protocol;
@ -274,6 +271,7 @@ class KROutboundItem {
// 🔧 config transport
Map<String, dynamic>? transportConfig;
Map<String, dynamic>? securityConfig;
int actualPort = nodeListItem.port; // 🔧 使
// 🔧 protocols
if (nodeListItem.protocols.isNotEmpty) {
@ -307,6 +305,28 @@ class KROutboundItem {
}
if (matchedProtocol != null) {
// 🔧 0使 protocols
// 53441 protocols 287
if (actualPort == 0 && matchedProtocol['port'] != null) {
//
int protocolPort = 0;
final portValue = matchedProtocol['port'];
if (portValue is int) {
protocolPort = portValue;
} else if (portValue is String) {
protocolPort = int.tryParse(portValue) ?? 0;
}
if (protocolPort > 0) {
actualPort = protocolPort;
if (kDebugMode) {
print(' ✅ 从 protocols 使用端口: $protocolPort (顶层端口为0)');
}
}
} else if (kDebugMode && matchedProtocol['port'] != null) {
print(' ✅ 保留顶层端口: $actualPort (protocols中的端口: ${matchedProtocol['port']})');
}
// transport
if (matchedProtocol['network'] != null || matchedProtocol['transport'] != null) {
final network = matchedProtocol['network'] ?? matchedProtocol['transport'];
@ -411,18 +431,17 @@ class KROutboundItem {
"type": "shadowsocks",
"tag": nodeListItem.name,
"server": nodeListItem.serverAddr,
"server_port": nodeListItem.port,
"server_port": actualPort,
"method": finalMethod,
"password": nodeListItem.uuid
};
if (kDebugMode) {
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
print('✅ Shadowsocks 节点配置构建成功: ${nodeListItem.name}');
}
if (kDebugMode) {
print('📄 使用加密方法: $finalMethod');
}
if (kDebugMode) {
print('📄 完整配置: $config');
print('📄 完整配置 JSON:');
print(jsonEncode(config));
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
}
break;
case "vless":
@ -436,17 +455,25 @@ class KROutboundItem {
serverName = securityConfig['sni'].toString();
}
// 🔧 security_config TLS
final bool vlessTlsEnabled = securityConfig?['tls_enabled'] ?? false;
// 🔧 TLS
// 1. 使 securityConfig['tls_enabled']
// 2.
bool vlessTlsEnabled = securityConfig?['tls_enabled'] ?? true; // TLS
// TLS端口80, 8080 TLS
if (nodeListItem.port == 80 || nodeListItem.port == 8080) {
vlessTlsEnabled = false;
}
if (kDebugMode) {
print('🔐 VLESS TLS 状态: enabled=$vlessTlsEnabled');
print('🔐 VLESS TLS 状态: enabled=$vlessTlsEnabled (port=${nodeListItem.port}, isDomain=$isDomain)');
}
config = {
"type": "vless",
"tag": nodeListItem.name,
"server": nodeListItem.serverAddr,
"server_port": nodeListItem.port,
"server_port": actualPort,
"uuid": nodeListItem.uuid,
if (transportConfig != null) "transport": transportConfig,
if (vlessTlsEnabled) "tls": {
@ -460,10 +487,23 @@ class KROutboundItem {
}
};
if (kDebugMode) {
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
print('✅ VLESS 节点配置构建成功: ${nodeListItem.name}');
}
if (kDebugMode) {
print('📄 完整配置: $config');
print('📋 原始节点信息:');
print(' - serverAddr: ${nodeListItem.serverAddr}');
print(' - port: ${nodeListItem.port}');
print(' - uuid: ${nodeListItem.uuid}');
print(' - protocols: ${nodeListItem.protocols}');
print('🔐 安全配置:');
print(' - TLS 启用: $vlessTlsEnabled');
print(' - 是域名: $isDomain');
print(' - server_name: $serverName');
print(' - securityConfig: $securityConfig');
print('📡 传输配置:');
print(' - transportConfig: $transportConfig');
print('📄 最终生成的完整配置 JSON:');
print(jsonEncode(config));
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
}
break;
case "vmess":
@ -477,17 +517,25 @@ class KROutboundItem {
serverName = securityConfig['sni'].toString();
}
// 🔧 security_config TLS
final bool tlsEnabled = securityConfig?['tls_enabled'] ?? false;
// 🔧 TLS
// 1. 使 securityConfig['tls_enabled']
// 2.
bool tlsEnabled = securityConfig?['tls_enabled'] ?? true; // TLS
// TLS端口80, 8080 TLS
if (nodeListItem.port == 80 || nodeListItem.port == 8080) {
tlsEnabled = false;
}
if (kDebugMode) {
print('🔐 TLS 状态: enabled=$tlsEnabled');
print('🔐 VMess TLS 状态: enabled=$tlsEnabled (port=${nodeListItem.port}, isDomain=$isDomain)');
}
config = {
"type": "vmess",
"tag": nodeListItem.name,
"server": nodeListItem.serverAddr,
"server_port": nodeListItem.port,
"server_port": actualPort,
"uuid": nodeListItem.uuid,
"alter_id": 0,
"security": "auto",
@ -503,10 +551,23 @@ class KROutboundItem {
}
};
if (kDebugMode) {
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
print('✅ VMess 节点配置构建成功: ${nodeListItem.name}');
}
if (kDebugMode) {
print('📄 完整配置: $config');
print('📋 原始节点信息:');
print(' - serverAddr: ${nodeListItem.serverAddr}');
print(' - port: ${nodeListItem.port}');
print(' - uuid: ${nodeListItem.uuid}');
print(' - protocols: ${nodeListItem.protocols}');
print('🔐 安全配置:');
print(' - TLS 启用: $tlsEnabled');
print(' - 是域名: $isDomain');
print(' - server_name: $serverName');
print(' - securityConfig: $securityConfig');
print('📡 传输配置:');
print(' - transportConfig: $transportConfig');
print('📄 最终生成的完整配置 JSON:');
print(jsonEncode(config));
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
}
break;
case "trojan":
@ -524,7 +585,7 @@ class KROutboundItem {
"type": "trojan",
"tag": nodeListItem.name,
"server": nodeListItem.serverAddr,
"server_port": nodeListItem.port,
"server_port": actualPort,
"password": nodeListItem.uuid,
if (transportConfig != null) "transport": transportConfig,
"tls": {

View File

@ -136,25 +136,68 @@ class KrNodeListItem {
String method = json['method']?.toString() ?? ''; // Shadowsocks等
final protocols = json['protocols']?.toString() ?? ''; // JSON
// 🔧 protocols port cipher
// 🔧 JSON
KRLogUtil.kr_i('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', tag: 'NodeList');
KRLogUtil.kr_i('📥 收到节点 API 原始数据:', tag: 'NodeList');
KRLogUtil.kr_i('节点名称: ${json['name']}', tag: 'NodeList');
KRLogUtil.kr_i('协议类型: ${json['protocol']}', tag: 'NodeList');
KRLogUtil.kr_i('完整 JSON:', tag: 'NodeList');
KRLogUtil.kr_i(jsonEncode(json), tag: 'NodeList');
// 🔧 protocols cipher port
if (protocols.isNotEmpty) {
try {
final protocolsList = jsonDecode(protocols) as List;
final currentProtocol = json['protocol']?.toString().toLowerCase() ?? '';
KRLogUtil.kr_i('📋 protocols 字段内容 (${protocolsList.length} 个协议):', tag: 'NodeList');
for (var i = 0; i < protocolsList.length; i++) {
KRLogUtil.kr_i(' 协议 $i: ${jsonEncode(protocolsList[i])}', tag: 'NodeList');
}
if (protocolsList.isNotEmpty) {
final firstProtocol = protocolsList[0] as Map<String, dynamic>;
// 使 protocols
if (firstProtocol['port'] != null) {
port = _parseIntSafely(firstProtocol['port']);
// 🔧 使
Map<String, dynamic>? matchedProtocolConfig;
//
for (var protocolConfig in protocolsList) {
final configMap = protocolConfig as Map<String, dynamic>;
// 🔧 API 使 'type' 'protocol'
final protocolType = (configMap['type'] ?? configMap['protocol'])?.toString().toLowerCase() ?? '';
// shadowsocks/ss, vmess, vless, trojan
if (protocolType == currentProtocol ||
(currentProtocol == 'shadowsocks' && protocolType == 'ss') ||
(currentProtocol == 'ss' && protocolType == 'shadowsocks')) {
matchedProtocolConfig = configMap;
KRLogUtil.kr_i('🎯 找到匹配的协议配置: $protocolType', tag: 'NodeList');
break;
}
if (firstProtocol['cipher'] != null && firstProtocol['cipher'].toString().isNotEmpty) {
method = firstProtocol['cipher'].toString();
}
KRLogUtil.kr_i('从 protocols 解析: port=$port, cipher=$method', tag: 'NodeList');
// 使API
final targetProtocol = matchedProtocolConfig ?? (protocolsList[0] as Map<String, dynamic>);
// 🔧 port 使 protocols
// 53441 protocols 287
if (port == 0 && targetProtocol['port'] != null) {
port = _parseIntSafely(targetProtocol['port']);
KRLogUtil.kr_i('✅ 从 protocols 解析端口: $port', tag: 'NodeList');
} else {
KRLogUtil.kr_i('✅ 保留顶层端口: $port (protocols中的端口: ${targetProtocol['port']})', tag: 'NodeList');
}
// cipher
if (targetProtocol['cipher'] != null && targetProtocol['cipher'].toString().isNotEmpty) {
method = targetProtocol['cipher'].toString();
KRLogUtil.kr_i('✅ 从 protocols 解析 cipher: $method', tag: 'NodeList');
}
}
} catch (e) {
KRLogUtil.kr_w('解析 protocols 字段失败: $e', tag: 'NodeList');
KRLogUtil.kr_w('⚠️ 解析 protocols 字段失败: $e', tag: 'NodeList');
}
}
KRLogUtil.kr_i('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', tag: 'NodeList');
return KrNodeListItem(
id: _parseIntSafely(json['id']),

View File

@ -80,8 +80,8 @@ class HINodeListView extends GetView<HINodeListController> {
//
return Material(
color: Colors.transparent,
// child: _buildSubscribeList(context)
child: _kr_buildRegionList(context)
child: _buildSubscribeList(context)
// child: _kr_buildRegionList(context)
);
}
@ -380,7 +380,7 @@ class HINodeListView extends GetView<HINodeListController> {
children: [
Flexible(
child: Text(
controller.homeController.kr_getCountryFullName(item.country),
'${controller.homeController.kr_getCountryFullName(item.country)}-${item.tag}',
style: KrAppTextStyle(fontSize: 14, color: Colors.white),
overflow: TextOverflow.ellipsis,
maxLines: 1,

View File

@ -849,6 +849,7 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
if (kDebugMode) {
}
await KRSingBoxImp.instance.kr_start();
KRLogUtil.kr_i('✅ 连接命令已发送', tag: 'HomeController');
if (kDebugMode) {
}
@ -2185,6 +2186,7 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
kr_isConnected.value = true;
kr_startConnectionTimer();
kr_updateConnectionInfo();
KRSingBoxImp.instance.kr_debugTunConnectivity();
// 🔧
if (!_kr_tryUpdateDelayFromActiveGroups()) {

View File

@ -107,6 +107,9 @@ class KRSingBoxImp {
///
bool _kr_isInitialized = false;
/// Futuresingle-flight
Future<void>? _kr_initFuture;
///
bool get kr_isProxyReady => kr_status.value is SingboxStarted;
@ -139,12 +142,23 @@ class KRSingBoxImp {
///
Future<void> init() async {
//
//
if (_kr_isInitialized) {
KRLogUtil.kr_i('SingBox 已经初始化,跳过重复初始化', tag: 'SingBox');
return;
}
// Future
if (_kr_initFuture != null) {
KRLogUtil.kr_i('SingBox 初始化进行中等待完成single-flight', tag: 'SingBox');
await _kr_initFuture;
return;
}
// single-flight Future
final completer = Completer<void>();
_kr_initFuture = completer.future;
try {
KRLogUtil.kr_i('开始初始化 SingBox');
//
@ -416,6 +430,10 @@ class KRSingBoxImp {
// UI libcore
_kr_subscribeToStatus();
KRLogUtil.kr_i('✅ 状态订阅已设置', tag: 'SingBox');
// single-flight
completer.complete();
_kr_initFuture = null;
} catch (e, stackTrace) {
KRLogUtil.kr_e('❌ SingBox 初始化失败: $e');
KRLogUtil.kr_e('📚 错误堆栈: $stackTrace');
@ -451,6 +469,11 @@ class KRSingBoxImp {
//
_kr_isInitialized = false;
// single-flight
if (!completer.isCompleted) {
completer.completeError(e, stackTrace);
}
_kr_initFuture = null;
rethrow;
}
}
@ -464,13 +487,13 @@ class KRSingBoxImp {
"region": "other", // hiddify-app: 使 "other"
"block-ads": false, // hiddify-app: 广
"use-xray-core-when-possible": false,
"execute-config-as-is": 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": "udp://1.1.1.1", // hiddify-app: 使 Cloudflare DNS
"remote-dns-address": "https://dns.google/dns-query", // 使 Google DoH DNS
"remote-dns-domain-strategy": "prefer_ipv4",
"direct-dns-address": "udp://1.1.1.1", // hiddify-app: 使 1.1.1.1
"direct-dns-address": "local" ,
"direct-dns-domain-strategy": "prefer_ipv4",
"mixed-port": kr_port,
"tproxy-port": kr_port,
@ -1031,17 +1054,86 @@ class KRSingBoxImp {
KRLogUtil.kr_i('✅ 过滤后节点数量: ${kr_outbounds.length}/${outbounds.length}', tag: 'SingBox');
// outboundsMobile.buildConfig()
final map = {
"outbounds": kr_outbounds
// 🔧 SingBox
// {"outbounds": [...]}, libcore
//
final Map<String, dynamic> fullConfig = {
"log": {
"level": "debug",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "dns-remote",
"address": "https://dns.google/dns-query",
"detour": "direct",
"address_resolver": "dns-direct"
},
{
"tag": "dns-direct",
"address": "local",
"detour": "direct"
}
],
"rules": [],
"final": "dns-remote",
"strategy": "prefer_ipv4"
},
"inbounds": [
{
"type": "tun",
"tag": "tun-in",
"interface_name": "utun",
"inet4_address": "172.19.0.1/30",
"auto_route": true,
"strict_route": true,
"sniff": true,
"sniff_override_destination": false
}
],
"outbounds": [
// 🔧 selector
{
"type": "selector",
"tag": "proxy",
"outbounds": kr_outbounds.map((o) => o['tag'] as String).toList(),
"default": kr_outbounds.isNotEmpty ? kr_outbounds[0]['tag'] : "direct",
},
...kr_outbounds,
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
},
{
"type": "dns",
"tag": "dns-out"
}
],
"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
],
"final": "proxy", // 🔧 使 selector
"auto_detect_interface": true
}
};
final file = _file(kr_configName);
final temp = _tempFile(kr_configName);
final mapStr = jsonEncode(map);
final mapStr = jsonEncode(fullConfig);
KRLogUtil.kr_i('📄 配置文件内容长度: ${mapStr.length}', tag: 'SingBox');
KRLogUtil.kr_i('📄 完整配置文件内容: $mapStr', tag: 'SingBox');
KRLogUtil.kr_i('📄 完整配置文件长度: ${mapStr.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);
await temp.writeAsString(mapStr);
@ -1473,13 +1565,45 @@ class KRSingBoxImp {
if (country == 'auto') {
KRLogUtil.kr_i('🌀 国家为 auto执行自动节点选择逻辑', tag: 'SingBox');
try {
// auto/
await KRSecureStorage().kr_saveData(key: 'SELECTED_COUNTRY_TAG', value: 'auto');
await KRSecureStorage().kr_saveData(key: _keySelectedNode, value: 'auto');
await kr_selectOutbound('auto');
_nodeSelectionTimer?.cancel();
_nodeSelectionTimer = null;
KRLogUtil.kr_i('✅ 已切换为 auto 节点选择', tag: 'SingBox');
} catch (e) {
KRLogUtil.kr_e('❌ auto 国家选择失败: $e', tag: 'SingBox');
rethrow;
}
return;
}
try {
await KRSecureStorage().kr_saveData(key: 'SELECTED_COUNTRY_TAG', value: country);
final oOption = _getConfigOption();
oOption["region"] = country;
KRLogUtil.kr_i('📝 更新 region 配置为国家: $country', tag: 'SingBox');
final op = SingboxConfigOption.fromJson(oOption);
await kr_singBox.changeOptions(op)
..map((r) {
KRLogUtil.kr_i('✅ 国家 region 更新成功: $country', tag: 'SingBox');
}).mapLeft((err) {
KRLogUtil.kr_e('❌ 更新国家 region 失败: $err', tag: 'SingBox');
throw err;
}).run();
// auto 便
await KRSecureStorage().kr_saveData(key: _keySelectedNode, value: 'auto');
KRLogUtil.kr_i(' 已应用国家到配置,等待控制器执行重启', tag: 'SingBox');
} catch (e) {
KRLogUtil.kr_e('❌ 国家选择流程失败: $e', tag: 'SingBox');
rethrow;
}
if (kr_activeGroups.isEmpty) {
KRLogUtil.kr_w('⚠️ kr_activeGroups 当前为空,无法进行国家选择', tag: 'SingBox');
return;
KRLogUtil.kr_w('⚠️ kr_activeGroups 当前为空,国家选择后将由重启同步分组', tag: 'SingBox');
}
}
@ -1534,4 +1658,56 @@ 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');
// 1) TCP 1.1.1.1:443 IP HTTP
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 204
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');
}
// 4) TCP 53DNS TCP53可达性
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');
}
KRLogUtil.kr_i('✅ [TUN Debug] 结束调试', tag: 'SingBoxTun');
}
}