refactor: 优化日志输出,仅在调试模式下启用
- 为所有 print 语句添加 kDebugMode 检查 - 更新 KRLogUtil 工具类,Release 模式下禁用日志输出 - 优化 18 个文件中的 335+ 条日志语句 - 提升 Release 版本性能并增强安全性 (cherry picked from commit 301f1510ba81fe94fb08e013ca80b3ca708a2e15)
This commit is contained in:
@@ -2,6 +2,7 @@ import 'dart:convert';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import '../response/kr_node_list.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// 表示出站项的模型类
|
||||
class KROutboundItem {
|
||||
@@ -50,14 +51,18 @@ class KROutboundItem {
|
||||
// 🔧 优先使用直接字段构建配置(新API格式)
|
||||
// 判断条件:如果有 port 和 serverAddr,说明是新格式
|
||||
if (nodeListItem.port > 0 && nodeListItem.serverAddr.isNotEmpty) {
|
||||
print('ℹ️ 节点 ${nodeListItem.name} 使用直接字段构建配置');
|
||||
if (kDebugMode) {
|
||||
print('ℹ️ 节点 ${nodeListItem.name} 使用直接字段构建配置');
|
||||
}
|
||||
_buildConfigFromFields(nodeListItem);
|
||||
return;
|
||||
}
|
||||
|
||||
// 兜底:尝试解析 config 字段(旧API格式)
|
||||
if (nodeListItem.config.isEmpty) {
|
||||
print('❌ 节点 ${nodeListItem.name} 缺少配置信息(无port或config)');
|
||||
if (kDebugMode) {
|
||||
print('❌ 节点 ${nodeListItem.name} 缺少配置信息(无port或config)');
|
||||
}
|
||||
config = {};
|
||||
return;
|
||||
}
|
||||
@@ -66,8 +71,12 @@ class KROutboundItem {
|
||||
try {
|
||||
json = jsonDecode(nodeListItem.config) as Map<String, dynamic>;
|
||||
} catch (e) {
|
||||
print('❌ 节点 ${nodeListItem.name} 的 config 解析失败: $e,尝试使用直接字段');
|
||||
print('📄 Config 内容: ${nodeListItem.config}');
|
||||
if (kDebugMode) {
|
||||
print('❌ 节点 ${nodeListItem.name} 的 config 解析失败: $e,尝试使用直接字段');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('📄 Config 内容: ${nodeListItem.config}');
|
||||
}
|
||||
_buildConfigFromFields(nodeListItem);
|
||||
return;
|
||||
}
|
||||
@@ -237,14 +246,30 @@ class KROutboundItem {
|
||||
|
||||
/// 直接从节点字段构建配置(新API格式)
|
||||
void _buildConfigFromFields(KrNodeListItem nodeListItem) {
|
||||
print('🔧 开始构建节点配置 - 协议: ${nodeListItem.protocol}, 名称: ${nodeListItem.name}');
|
||||
print('📋 节点详细信息:');
|
||||
print(' - serverAddr: ${nodeListItem.serverAddr}');
|
||||
print(' - port: ${nodeListItem.port}');
|
||||
print(' - uuid: ${nodeListItem.uuid}');
|
||||
print(' - method: ${nodeListItem.method}');
|
||||
print(' - config: ${nodeListItem.config}');
|
||||
print(' - protocols: ${nodeListItem.protocols}');
|
||||
if (kDebugMode) {
|
||||
print('🔧 开始构建节点配置 - 协议: ${nodeListItem.protocol}, 名称: ${nodeListItem.name}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('📋 节点详细信息:');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print(' - serverAddr: ${nodeListItem.serverAddr}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print(' - port: ${nodeListItem.port}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print(' - uuid: ${nodeListItem.uuid}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print(' - method: ${nodeListItem.method}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print(' - config: ${nodeListItem.config}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print(' - protocols: ${nodeListItem.protocols}');
|
||||
}
|
||||
|
||||
// 🔧 尝试从 config 字段解析 transport 配置
|
||||
Map<String, dynamic>? transportConfig;
|
||||
@@ -254,7 +279,9 @@ class KROutboundItem {
|
||||
if (nodeListItem.protocols.isNotEmpty) {
|
||||
try {
|
||||
final protocolsList = jsonDecode(nodeListItem.protocols) as List;
|
||||
print('📄 解析到 protocols 数组,共 ${protocolsList.length} 个协议');
|
||||
if (kDebugMode) {
|
||||
print('📄 解析到 protocols 数组,共 ${protocolsList.length} 个协议');
|
||||
}
|
||||
|
||||
// 查找匹配当前协议类型的配置
|
||||
Map<String, dynamic>? matchedProtocol;
|
||||
@@ -263,14 +290,18 @@ class KROutboundItem {
|
||||
final type = protocolMap['type']?.toString().toLowerCase() ?? '';
|
||||
final enable = protocolMap['enable'] ?? true;
|
||||
|
||||
print(' 📋 协议: type=$type, enable=$enable');
|
||||
if (kDebugMode) {
|
||||
print(' 📋 协议: type=$type, enable=$enable');
|
||||
}
|
||||
|
||||
// 匹配协议类型(注意 hysteria 和 hysteria2 都匹配 hysteria)
|
||||
if (type == nodeListItem.protocol.toLowerCase() ||
|
||||
(nodeListItem.protocol == 'hysteria' && type == 'hysteria2') ||
|
||||
(nodeListItem.protocol == 'hysteria2' && type == 'hysteria')) {
|
||||
matchedProtocol = protocolMap;
|
||||
print(' ✅ 找到匹配的协议配置: $type');
|
||||
if (kDebugMode) {
|
||||
print(' ✅ 找到匹配的协议配置: $type');
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -279,7 +310,9 @@ class KROutboundItem {
|
||||
// 提取 transport 配置
|
||||
if (matchedProtocol['network'] != null || matchedProtocol['transport'] != null) {
|
||||
final network = matchedProtocol['network'] ?? matchedProtocol['transport'];
|
||||
print(' 📡 传输协议: $network');
|
||||
if (kDebugMode) {
|
||||
print(' 📡 传输协议: $network');
|
||||
}
|
||||
|
||||
if (network == 'ws' || network == 'websocket') {
|
||||
transportConfig = {
|
||||
@@ -290,20 +323,26 @@ class KROutboundItem {
|
||||
if (host != null && host.toString().isNotEmpty) {
|
||||
transportConfig['headers'] = {'Host': host.toString()};
|
||||
}
|
||||
print(' ✅ WebSocket transport: $transportConfig');
|
||||
if (kDebugMode) {
|
||||
print(' ✅ WebSocket transport: $transportConfig');
|
||||
}
|
||||
} else if (network == 'grpc') {
|
||||
transportConfig = {
|
||||
'type': 'grpc',
|
||||
'service_name': matchedProtocol['grpc_service_name'] ?? matchedProtocol['service_name'] ?? '',
|
||||
};
|
||||
print(' ✅ gRPC transport: $transportConfig');
|
||||
if (kDebugMode) {
|
||||
print(' ✅ gRPC transport: $transportConfig');
|
||||
}
|
||||
} else if (network == 'http' || network == 'h2') {
|
||||
transportConfig = {
|
||||
'type': 'http',
|
||||
'host': [matchedProtocol['http_host'] ?? matchedProtocol['host'] ?? ''],
|
||||
'path': matchedProtocol['http_path'] ?? matchedProtocol['path'] ?? '/',
|
||||
};
|
||||
print(' ✅ HTTP transport: $transportConfig');
|
||||
if (kDebugMode) {
|
||||
print(' ✅ HTTP transport: $transportConfig');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,11 +358,15 @@ class KROutboundItem {
|
||||
'allow_insecure': matchedProtocol['allow_insecure'] ?? matchedProtocol['insecure'] ?? true,
|
||||
'fingerprint': matchedProtocol['fingerprint'] ?? 'chrome',
|
||||
};
|
||||
print(' ✅ Security config: security=$security, tls_enabled=$tlsEnabled, config=$securityConfig');
|
||||
if (kDebugMode) {
|
||||
print(' ✅ Security config: security=$security, tls_enabled=$tlsEnabled, config=$securityConfig');
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('⚠️ 解析 protocols 字段失败: $e');
|
||||
if (kDebugMode) {
|
||||
print('⚠️ 解析 protocols 字段失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -331,21 +374,29 @@ class KROutboundItem {
|
||||
if (transportConfig == null && nodeListItem.config.isNotEmpty) {
|
||||
try {
|
||||
final configJson = jsonDecode(nodeListItem.config) as Map<String, dynamic>;
|
||||
print('📄 解析到 config JSON: $configJson');
|
||||
if (kDebugMode) {
|
||||
print('📄 解析到 config JSON: $configJson');
|
||||
}
|
||||
|
||||
// 提取 transport 配置
|
||||
if (configJson['transport'] != null && configJson['transport'] != 'tcp') {
|
||||
transportConfig = _buildTransport(configJson);
|
||||
print('✅ 从 config 找到 transport 配置: $transportConfig');
|
||||
if (kDebugMode) {
|
||||
print('✅ 从 config 找到 transport 配置: $transportConfig');
|
||||
}
|
||||
}
|
||||
|
||||
// 提取 security_config
|
||||
if (configJson['security_config'] != null) {
|
||||
securityConfig = configJson['security_config'] as Map<String, dynamic>;
|
||||
print('✅ 从 config 找到 security_config: $securityConfig');
|
||||
if (kDebugMode) {
|
||||
print('✅ 从 config 找到 security_config: $securityConfig');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('⚠️ 解析 config 字段失败: $e');
|
||||
if (kDebugMode) {
|
||||
print('⚠️ 解析 config 字段失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,9 +415,15 @@ class KROutboundItem {
|
||||
"method": finalMethod,
|
||||
"password": nodeListItem.uuid
|
||||
};
|
||||
print('✅ Shadowsocks 节点配置构建成功: ${nodeListItem.name}');
|
||||
print('📄 使用加密方法: $finalMethod');
|
||||
print('📄 完整配置: $config');
|
||||
if (kDebugMode) {
|
||||
print('✅ Shadowsocks 节点配置构建成功: ${nodeListItem.name}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('📄 使用加密方法: $finalMethod');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('📄 完整配置: $config');
|
||||
}
|
||||
break;
|
||||
case "vless":
|
||||
// 判断是否为域名(非IP地址)
|
||||
@@ -381,7 +438,9 @@ class KROutboundItem {
|
||||
|
||||
// 🔧 关键修复:根据 security_config 判断是否启用 TLS
|
||||
final bool vlessTlsEnabled = securityConfig?['tls_enabled'] ?? false;
|
||||
print('🔐 VLESS TLS 状态: enabled=$vlessTlsEnabled');
|
||||
if (kDebugMode) {
|
||||
print('🔐 VLESS TLS 状态: enabled=$vlessTlsEnabled');
|
||||
}
|
||||
|
||||
config = {
|
||||
"type": "vless",
|
||||
@@ -400,8 +459,12 @@ class KROutboundItem {
|
||||
}
|
||||
}
|
||||
};
|
||||
print('✅ VLESS 节点配置构建成功: ${nodeListItem.name}');
|
||||
print('📄 完整配置: $config');
|
||||
if (kDebugMode) {
|
||||
print('✅ VLESS 节点配置构建成功: ${nodeListItem.name}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('📄 完整配置: $config');
|
||||
}
|
||||
break;
|
||||
case "vmess":
|
||||
// 判断是否为域名(非IP地址)
|
||||
@@ -416,7 +479,9 @@ class KROutboundItem {
|
||||
|
||||
// 🔧 关键修复:根据 security_config 判断是否启用 TLS
|
||||
final bool tlsEnabled = securityConfig?['tls_enabled'] ?? false;
|
||||
print('🔐 TLS 状态: enabled=$tlsEnabled');
|
||||
if (kDebugMode) {
|
||||
print('🔐 TLS 状态: enabled=$tlsEnabled');
|
||||
}
|
||||
|
||||
config = {
|
||||
"type": "vmess",
|
||||
@@ -437,8 +502,12 @@ class KROutboundItem {
|
||||
}
|
||||
}
|
||||
};
|
||||
print('✅ VMess 节点配置构建成功: ${nodeListItem.name}');
|
||||
print('📄 完整配置: $config');
|
||||
if (kDebugMode) {
|
||||
print('✅ VMess 节点配置构建成功: ${nodeListItem.name}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('📄 完整配置: $config');
|
||||
}
|
||||
break;
|
||||
case "trojan":
|
||||
// 判断是否为域名(非IP地址)
|
||||
@@ -468,16 +537,28 @@ class KROutboundItem {
|
||||
}
|
||||
}
|
||||
};
|
||||
print('✅ Trojan 节点配置构建成功: ${nodeListItem.name}');
|
||||
print('📄 完整配置: $config');
|
||||
if (kDebugMode) {
|
||||
print('✅ Trojan 节点配置构建成功: ${nodeListItem.name}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('📄 完整配置: $config');
|
||||
}
|
||||
break;
|
||||
case "hysteria":
|
||||
case "hysteria2":
|
||||
// 后端的 "hysteria" 实际上是 Hysteria2 协议
|
||||
print('🔍 构建 Hysteria2 节点: ${nodeListItem.name}');
|
||||
print(' - serverAddr: ${nodeListItem.serverAddr}');
|
||||
print(' - port: ${nodeListItem.port}');
|
||||
print(' - uuid: ${nodeListItem.uuid}');
|
||||
if (kDebugMode) {
|
||||
print('🔍 构建 Hysteria2 节点: ${nodeListItem.name}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print(' - serverAddr: ${nodeListItem.serverAddr}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print(' - port: ${nodeListItem.port}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print(' - uuid: ${nodeListItem.uuid}');
|
||||
}
|
||||
|
||||
//判断是否为域名
|
||||
final bool isDomain = !RegExp(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
|
||||
@@ -494,11 +575,17 @@ class KROutboundItem {
|
||||
if (isDomain) "server_name": nodeListItem.serverAddr,
|
||||
}
|
||||
};
|
||||
print('✅ Hysteria2 节点配置构建成功');
|
||||
print('📄 完整配置: ${jsonEncode(config)}');
|
||||
if (kDebugMode) {
|
||||
print('✅ Hysteria2 节点配置构建成功');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('📄 完整配置: ${jsonEncode(config)}');
|
||||
}
|
||||
break;
|
||||
default:
|
||||
print('⚠️ 不支持的协议类型: ${nodeListItem.protocol}');
|
||||
if (kDebugMode) {
|
||||
print('⚠️ 不支持的协议类型: ${nodeListItem.protocol}');
|
||||
}
|
||||
config = {};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'kr_group_outbound_list.dart';
|
||||
|
||||
import '../response/kr_node_list.dart';
|
||||
import 'kr_outbound_item.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// 表示出站项列表的模型类
|
||||
class KrOutboundsList {
|
||||
@@ -47,14 +48,24 @@ class KrOutboundsList {
|
||||
final KROutboundItem item = KROutboundItem(element);
|
||||
|
||||
// 🔍 调试日志:验证 country 字段传递
|
||||
print('🗺️ 构建节点: name="${element.name}", tag="${item.tag}", country="${item.country}"');
|
||||
print(' - element.country: "${element.country}"');
|
||||
print(' - item.country: "${item.country}"');
|
||||
print(' - country.isEmpty: ${item.country.isEmpty}');
|
||||
if (kDebugMode) {
|
||||
print('🗺️ 构建节点: name="${element.name}", tag="${item.tag}", country="${item.country}"');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print(' - element.country: "${element.country}"');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print(' - item.country: "${item.country}"');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print(' - country.isEmpty: ${item.country.isEmpty}');
|
||||
}
|
||||
|
||||
// 检查节点配置是否有效(必须包含 type 字段)
|
||||
if (item.config.isEmpty || !item.config.containsKey('type')) {
|
||||
print('⚠️ 跳过无效节点: ${element.name},配置为空或缺少 type 字段');
|
||||
if (kDebugMode) {
|
||||
print('⚠️ 跳过无效节点: ${element.name},配置为空或缺少 type 字段');
|
||||
}
|
||||
continue; // 跳过无效节点
|
||||
}
|
||||
|
||||
@@ -72,7 +83,9 @@ class KrOutboundsList {
|
||||
|
||||
configJsonList.add(item.config);
|
||||
keyList[item.tag] = item;
|
||||
print('✅ keyList["${item.tag}"] 已设置,country="${item.country}"');
|
||||
if (kDebugMode) {
|
||||
print('✅ keyList["${item.tag}"] 已设置,country="${item.country}"');
|
||||
}
|
||||
}
|
||||
|
||||
// 将标签分组转换为 KRGroupOutboundList 并添加到 groupOutboundList
|
||||
|
||||
Reference in New Issue
Block a user