hi-client/TROJAN_SERVER_NAME_FIX.md
2025-10-13 18:08:02 +08:00

135 lines
2.9 KiB
Markdown
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Trojan 配置中 server_name 参数修复
## 🔍 问题分析
### **原始问题**
你的 Trojan 配置中存在 `server_name` 设置错误:
```json
{
"server": "156.224.78.176",
"server_name": "baidu.com" // ❌ 错误:服务器 IP 与 SNI 不匹配
}
```
### **问题原因**
1. **TLS 握手失败** - 服务器没有为 `baidu.com` 配置证书
2. **SNI 不匹配** - 客户端请求 `baidu.com`,但服务器只支持 IP 地址
3. **代理无法工作** - TLS 验证失败导致连接中断
## 🛠️ 修复方案
### **智能 server_name 设置**
已修改配置生成逻辑,现在会:
1. **优先使用配置的 SNI** - 如果服务器配置了 `sni` 参数
2. **回退到服务器地址** - 如果没有配置 SNI使用服务器 IP/域名
3. **避免不匹配** - 确保 `server_name` 与服务器实际配置一致
### **修复后的逻辑**
```dart
// 智能设置 server_name
String serverName = securityConfig["sni"] ?? "";
if (serverName.isEmpty) {
// 如果没有配置 SNI使用服务器地址
serverName = nodeListItem.serverAddr;
}
```
### **修复后的配置**
```json
{
"server": "156.224.78.176",
"server_name": "156.224.78.176" // ✅ 正确:使用服务器 IP
}
```
## 📋 server_name 参数说明
### **应该填什么值**
#### **1. 服务器实际域名** ✅ **最佳选择**
```json
{
"server_name": "your-server-domain.com"
}
```
#### **2. 服务器 IP 地址** ✅ **推荐**
```json
{
"server_name": "156.224.78.176"
}
```
#### **3. 空字符串** ✅ **某些情况下**
```json
{
"server_name": ""
}
```
### **不应该填什么值**
#### **❌ 随机域名**
```json
{
"server_name": "baidu.com" // 错误:服务器没有这个域名的证书
}
```
#### **❌ 不相关的域名**
```json
{
"server_name": "google.com" // 错误:与服务器不匹配
}
```
## 🔧 其他协议修复
已同时修复了以下协议的 `server_name` 设置:
- **VLESS** - 智能 SNI 设置
- **VMess** - 智能 SNI 设置
- **Trojan** - 智能 SNI 设置
## 🧪 测试步骤
1. **重新运行应用**
2. **检查新的配置** - 应该看到 `server_name` 使用服务器 IP
3. **测试连接** - 应该能正常通过代理访问网络
4. **验证延迟** - 延迟测试应该能正常工作
## 📊 预期结果
修复后应该看到:
```json
{
"type": "trojan",
"tag": "香港",
"server": "156.224.78.176",
"server_port": 27639,
"password": "cf6dc0d8-4997-4fc3-b790-1a54e38c6e8c",
"tls": {
"enabled": true,
"server_name": "156.224.78.176", // ✅ 修复后
"insecure": false,
"utls": {
"enabled": true,
"fingerprint": "chrome"
}
}
}
```
## 💡 关键要点
1. **`server_name` 必须与服务器配置匹配**
2. **优先使用服务器实际域名**
3. **IP 地址也是有效的选择**
4. **避免使用不相关的域名**
5. **TLS 验证失败会导致代理无法工作**
这个修复应该能解决你的 Trojan 连接问题!