AnyTLS协议完全指南:原理、部署与使用教程

AnyTLS协议完全指南:原理、部署与使用教程

摘要

本文全面介绍AnyTLS协议,从协议设计理念、技术原理到实际部署进行详细讲解。内容涵盖AnyTLS协议的发展历程、基于TLS的加密机制与填充方案、服务端部署(包括sing-box安装和配置)、各平台客户端配置使用、订阅链接格式,以及安全性注意事项和常见问题排查。旨在帮助读者深入理解AnyTLS协议并掌握其实际应用。

一、AnyTLS协议介绍

1.1 什么是AnyTLS协议

AnyTLS是一个新兴的TLS代理协议,由sing-box团队开发并维护。AnyTLS的设计目标是提供一个通用、灵活且安全的TLS代理解决方案,它支持多种传输模式和加密选项,致力于在安全性和易用性之间取得平衡。

AnyTLS的核心思想是:利用标准TLS协议作为传输载体,通过自定义的填充方案(Padding Scheme)来增强流量的隐蔽性,同时保持良好的性能表现和广泛的兼容性。

graph LR
A[AnyTLS] --> B[客户端]
A --> C[服务端]
A --> D[TLS传输]

B --> B1[SOCKS5监听]
B --> B2[会话管理]
B --> B3[填充处理]

C --> C1[用户认证]
C --> C2[多用户支持]
C --> C3[流量转发]

D --> D1[TLS加密]
D --> D2[填充方案]
D --> D3[证书验证]

1.2 协议发展历程

时间事件说明
2024协议设计sing-box团队开始设计AnyTLS协议
2024首个版本在sing-box dev-next分支中发布
2025功能完善新增更多填充方案和会话管理功能
2026+持续演进多平台客户端支持,功能不断完善

主要版本特点

graph TB
A[AnyTLS] --> B[灵活填充]
A --> C[TLS兼容]
A --> D[会话管理]
A --> E[多用户支持]

B --> B1[自定义填充方案]
B --> B2[流量隐蔽增强]
B --> B3[抗检测能力]

C --> C1[标准TLS协议]
C --> C2[证书支持]
C --> C3[广泛兼容]

D --> D1[空闲会话检测]
D --> D2[会话超时管理]
D --> D3[最小会话保持]

E --> E1[用户名密码认证]
E --> E2[多用户管理]
E --> E3[访问控制]

1.3 设计理念与特点

1.3.1 设计理念

AnyTLS的设计遵循以下原则:

  1. TLS兼容:基于标准TLS协议,确保广泛兼容性
  2. 灵活填充:支持自定义填充方案,增强流量隐蔽性
  3. 会话管理:完善的空闲会话检测和超时机制
  4. 简单配置:配置简洁,易于部署和维护
  5. 安全优先:使用现代加密算法,确保通信安全

1.3.2 协议特点

特点说明
基于TLS使用标准TLS协议传输,兼容性好
填充方案支持自定义填充,增强隐蔽性
会话管理空闲会话检测、超时机制
密码认证简单的用户名密码认证
多用户支持支持多用户管理
TCP/UDP支持同时支持TCP和UDP转发

1.4 适用场景

AnyTLS协议适用于以下场景:

  • 流量隐蔽需求:需要TLS流量伪装的场景
  • 实验性部署:对新协议感兴趣的技术用户
  • 特定需求:需要自定义填充方案的代理方案
  • 学习和研究:研究和学习代理协议
  • 稳定网络环境:在稳定网络环境下使用

重要说明:AnyTLS是一个新兴协议,目前仍在积极开发中。相比成熟的代理协议(如VLESS、Hysteria2等),AnyTLS的生态系统和客户端支持还在完善阶段,建议技术用户尝试使用。

1.5 与其他代理协议对比

特性AnyTLSVLESSTrojanHysteria2TUIC
传输协议TCP/TLSTCP/UDPTCPUDP(QUIC)UDP(QUIC)
加密方式TLSTLS/XTLSTLSQUIC TLSQUIC TLS
流量伪装填充方案Reality/WSHTTP/3
多用户支持支持支持支持支持支持
性能极高
配置复杂度
客户端支持sing-box广泛广泛较广较广

二、协议工作原理

2.1 整体架构

AnyTLS采用客户端-服务器架构,基于TLS协议构建:

graph TB
subgraph 客户端
A[应用程序] --> B[AnyTLS客户端<br/>SOCKS5代理]
B --> B1[填充处理]
end

subgraph TLS传输
B1 -->|TLS加密| C1[网络传输]
end

subgraph 服务端
C1 -->|TLS加密| C[AnyTLS服务端]
C --> C1[用户认证]
C1 --> D[解密模块]
D --> E[目标服务器]
end

组件说明

组件功能
TLS连接提供加密传输通道
填充处理添加填充数据,增强隐蔽性
用户认证验证客户端身份
会话管理管理连接会话生命周期

2.2 填充方案机制

AnyTLS通过填充方案增强流量隐蔽性:

sequenceDiagram
participant C as 客户端
participant S as 服务端

Note over C,S: TLS握手阶段
C->>S: ClientHello
S->>C: ServerHello + Certificate
C->>S: Finished

Note over C,S: 填充数据处理
C->>S: 应用数据 + 填充数据
S->>C: 响应数据 + 填充数据

Note over C,S: 会话管理
C->>S: 心跳/保活
S->>C: 会话确认

2.3 填充方案详解

AnyTLS支持自定义填充方案,默认填充方案如下:

1
2
3
4
5
6
7
8
9
10
11
12
默认填充方案:
┌──────────────────────────────────────────────────────────────────────┐
stop=8 # 停止条件 │
0=30-30 # 初始填充 │
1=100-400 # 小数据填充 │
2=400-500,c,500-1000,c,500-1000,c,500-1000,c,500-1000 # 中等数据 │
3=9-9,500-1000 # 快速填充 │
4=500-1000 # 大数据填充 │
5=500-1000 # 备用填充 │
6=500-1000 # 备用填充 │
7=500-1000 # 备用填充 │
└──────────────────────────────────────────────────────────────────────┘

填充方案参数说明

参数说明示例
stop停止填充的连接数stop=8
数字范围填充数据长度范围100-400
c继续填充标记500-1000,c,500-1000

2.4 会话管理机制

AnyTLS提供完善的会话管理功能:

1
2
3
4
5
6
会话管理参数:
┌────────────────────────────────────────────────────────────────┐
idle_session_check_interval: 30s # 空闲会话检查间隔 │
idle_session_timeout: 30s # 空闲会话超时时间 │
min_idle_session: 5 # 最小空闲会话数量 │
└────────────────────────────────────────────────────────────────┘

会话管理流程

  1. 空闲检测:定期检查会话空闲状态
  2. 超时处理:超过设定时间自动关闭空闲会话
  3. 最小保持:保持一定数量的空闲会话以快速响应

2.5 认证机制

AnyTLS使用用户名密码认证:

1
2
3
4
5
6
7
8
认证流程:
┌────────────────────────────────────────────────────────────────┐
│ 1. 客户端发起TLS连接 │
│ 2. 完成TLS握手 │
│ 3. 客户端发送用户名和密码 │
│ 4. 服务端验证用户身份 │
│ 5. 验证通过后建立代理通道 │
└────────────────────────────────────────────────────────────────┘

三、服务端部署教程

3.1 环境准备

3.1.1 服务器要求

项目最低要求推荐配置
CPU1核2核+
内存256MB1GB+
存储5GB20GB+
带宽10Mbps100Mbps+
系统Debian 10+/Ubuntu 18.04+/CentOS 7+最新稳定版

3.1.2 端口规划

服务端口说明
AnyTLS443/TCP推荐使用443端口
AnyTLS自定义可修改端口

3.2 安装sing-box

sing-box是AnyTLS协议的主要实现,需要安装sing-box来部署AnyTLS服务。

3.2.1 使用官方脚本安装

1
2
3
4
5
6
7
8
# 下载安装脚本
curl -fsSL https://sing-box.app/deb-install.sh | sh

# 或者使用apt安装(Debian/Ubuntu)
curl -fsSL https://deb.sagernet.org/pubkey.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/sagernet.gpg
echo "deb [signed-by=/etc/apt/keyrings/sagernet.gpg] https://deb.sagernet.org/ * *" | sudo tee /etc/apt/sources.list.d/sagernet.list
sudo apt update
sudo apt install sing-box

3.2.2 手动安装二进制

1
2
3
4
5
6
7
8
9
10
11
# 下载最新版本
SING_BOX_VERSION=$(curl -s https://api.github.com/repos/SagerNet/sing-box/releases/latest | grep '"tag_name"' | sed -E 's/.*"v([^"]+)".*/\1/')
wget https://github.com/SagerNet/sing-box/releases/download/v${SING_BOX_VERSION}/sing-box-${SING_BOX_VERSION}-linux-amd64.tar.gz

# 解压安装
tar -xzf sing-box-*-linux-amd64.tar.gz
sudo mv sing-box /usr/local/bin/
sudo chmod +x /usr/local/bin/sing-box

# 验证安装
sing-box version

3.2.3 Docker安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 拉取镜像
docker pull singbox/sing-box:latest

# 创建配置目录
sudo mkdir -p /etc/sing-box

# 运行容器(需要先创建配置文件)
docker run -d \
--name sing-box \
--restart=always \
-v /etc/sing-box:/etc/sing-box \
-p 443:443 \
singbox/sing-box:latest \
run -c /etc/sing-box/config.json

3.3 获取TLS证书

AnyTLS需要TLS证书,推荐使用ACME自动获取证书。

3.3.1 使用acme.sh获取证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 安装acme.sh
curl https://get.acme.sh | sh

# 设置默认CA
~/.acme.sh/acme.sh --set-default-ca --server letsencrypt

# 获取证书(需要域名已解析到服务器)
~/.acme.sh/acme.sh --issue -d your-domain.com --standalone

# 安装证书到指定目录
~/.acme.sh/acme.sh --install-cert -d your-domain.com \
--key-file /etc/sing-box/privkey.pem \
--fullchain-file /etc/sing-box/fullchain.pem \
--reloadcmd "systemctl restart sing-box"

3.3.2 使用certbot获取证书

1
2
3
4
5
6
7
8
9
10
11
12
# 安装certbot
sudo apt install certbot

# 获取证书
sudo certbot certonly --standalone -d your-domain.com

# 复制证书到sing-box目录
sudo cp /etc/letsencrypt/live/your-domain.com/fullchain.pem /etc/sing-box/
sudo cp /etc/letsencrypt/live/your-domain.com/privkey.pem /etc/sing-box/

# 设置自动续签
sudo systemctl enable certbot.timer

3.4 配置AnyTLS服务端

3.4.1 基础配置文件

创建配置文件 /etc/sing-box/config.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
"log": {
"level": "info",
"timestamp": true
},
"inbounds": [
{
"type": "anytls",
"tag": "anytls-in",
"listen": "::",
"listen_port": 443,
"users": [
{
"name": "user1",
"password": "your_password_here"
}
],
"tls": {
"enabled": true,
"server_name": "your-domain.com",
"key_path": "/etc/sing-box/privkey.pem",
"certificate_path": "/etc/sing-box/fullchain.pem"
}
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
}
]
}

3.4.2 完整配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{
"log": {
"level": "info",
"output": "/var/log/sing-box/access.log",
"timestamp": true
},
"inbounds": [
{
"type": "anytls",
"tag": "anytls-in",
"listen": "::",
"listen_port": 443,
"users": [
{
"name": "user1",
"password": "password_for_user1"
},
{
"name": "user2",
"password": "password_for_user2"
}
],
"padding_scheme": [
"stop=8",
"0=30-30",
"1=100-400",
"2=400-500,c,500-1000,c,500-1000,c,500-1000,c,500-1000",
"3=9-9,500-1000",
"4=500-1000",
"5=500-1000",
"6=500-1000",
"7=500-1000"
],
"tls": {
"enabled": true,
"server_name": "your-domain.com",
"key_path": "/etc/sing-box/privkey.pem",
"certificate_path": "/etc/sing-box/fullchain.pem"
}
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
}
],
"route": {
"final": "direct"
}
}

3.5 配置参数说明

3.5.1 Inbound部分

参数说明示例值
type协议类型anytls
tag标签名称anytls-in
listen监听地址::(IPv6)或 0.0.0.0
listen_port监听端口443
users用户列表用户名密码数组
padding_scheme填充方案数组形式
tlsTLS配置证书和密钥路径

3.5.2 用户配置

参数说明示例值
name用户名user1
password密码your_password

3.5.3 TLS配置

参数说明示例值
enabled是否启用TLStrue
server_name服务器名称your-domain.com
key_path私钥路径/etc/sing-box/privkey.pem
certificate_path证书路径/etc/sing-box/fullchain.pem

3.6 配置防火墙

3.6.1 UFW配置(Ubuntu/Debian)

1
2
3
4
5
6
7
8
# 允许AnyTLS端口
sudo ufw allow 443/tcp comment 'AnyTLS'

# 重新加载防火墙
sudo ufw reload

# 查看状态
sudo ufw status

3.6.2 firewalld配置(CentOS/RHEL)

1
2
3
4
5
6
7
8
# 添加AnyTLS端口
sudo firewall-cmd --permanent --add-port=443/tcp

# 重新加载防火墙
sudo firewall-cmd --reload

# 查看规则
sudo firewall-cmd --list-all

3.6.3 iptables配置

1
2
3
4
5
# 允许TCP端口
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 保存规则
sudo iptables-save > /etc/iptables/rules.v4

3.7 启动sing-box服务

3.7.1 创建systemd服务

创建文件 /etc/systemd/system/sing-box.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=sing-box service
Documentation=https://sing-box.sagernet.org
After=network.target nss-lookup.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/sing-box run -c /etc/sing-box/config.json
Restart=on-failure
RestartSec=5s
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

3.7.2 启动服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 设置权限
sudo chmod 600 /etc/sing-box/config.json

# 重载systemd
sudo systemctl daemon-reload

# 启动服务
sudo systemctl start sing-box

# 设置开机自启
sudo systemctl enable sing-box

# 查看状态
sudo systemctl status sing-box

3.7.3 验证服务

1
2
3
4
5
6
7
# 检查端口监听
sudo ss -tlnp | grep 443

# 查看日志
sudo journalctl -u sing-box -f

# 测试连接(需要客户端)

3.8 使用Docker部署

3.8.1 Docker Compose配置

创建 docker-compose.yml 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
version: "3.8"

services:
sing-box:
image: singbox/sing-box:latest
container_name: sing-box
restart: always
ports:
- "443:443"
volumes:
- ./config.json:/etc/sing-box/config.json:ro
- ./certs:/etc/sing-box/certs:ro
command: run -c /etc/sing-box/config.json

3.8.2 启动Docker服务

1
2
3
4
5
6
7
8
# 启动服务
docker-compose up -d

# 查看日志
docker-compose logs -f

# 停止服务
docker-compose down

四、客户端配置指南

4.1 各平台客户端推荐

平台客户端特点推荐度
Windowssing-box官方支持,功能完整★★★★★
Windowsv2rayN多协议支持,界面友好★★★★☆
macOSsing-box官方支持,功能强大★★★★★
Linuxsing-box官方支持,性能最佳★★★★★
Androidsing-box多协议支持,功能全面★★★★★
Androidv2rayNG多协议支持,界面友好★★★★☆
iOSsing-box多协议支持★★★★★
iOSShadowrocket功能全面,支持AnyTLS★★★★☆

4.2 配置参数说明

4.2.1 客户端配置字段

参数说明示例值
type协议类型anytls
tag标签名称anytls-out
server服务器地址your-domain.com
server_port服务器端口443
password密码your_password
idle_session_check_interval空闲会话检查间隔30s
idle_session_timeout空闲会话超时时间30s
min_idle_session最小空闲会话数5

4.3 Windows客户端配置

4.3.1 sing-box配置

创建配置文件 client.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
"log": {
"level": "info",
"timestamp": true
},
"inbounds": [
{
"type": "socks",
"tag": "socks-in",
"listen": "127.0.0.1",
"listen_port": 1080
},
{
"type": "http",
"tag": "http-in",
"listen": "127.0.0.1",
"listen_port": 8080
}
],
"outbounds": [
{
"type": "anytls",
"tag": "anytls-out",
"server": "your-domain.com",
"server_port": 443,
"password": "your_password",
"idle_session_check_interval": "30s",
"idle_session_timeout": "30s",
"min_idle_session": 5,
"tls": {
"enabled": true,
"server_name": "your-domain.com"
}
}
],
"route": {
"final": "anytls-out"
}
}

4.3.2 运行sing-box客户端

1
2
3
4
5
6
7
8
# 使用配置文件运行
sing-box run -c client.json

# 或使用命令行参数
sing-box run -c client.json --log-level info

# 后台运行
Start-Process -NoNewWindow sing-box -ArgumentList "run -c client.json"

4.3.3 v2rayN配置

  1. 打开v2rayN
  2. 点击"服务器" → “添加sing-box服务器”
  3. 配置如下:
1
2
3
4
5
6
7
8
9
10
11
{
"type": "anytls",
"tag": "proxy",
"server": "your-domain.com",
"server_port": 443,
"password": "your_password",
"tls": {
"enabled": true,
"server_name": "your-domain.com"
}
}

4.4 macOS客户端配置

4.4.1 安装sing-box

1
2
3
4
5
# 使用Homebrew安装
brew install sing-box

# 或手动下载
# https://github.com/SagerNet/sing-box/releases

4.4.2 创建配置文件

创建 /usr/local/etc/sing-box/config.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
"log": {
"level": "info",
"timestamp": true
},
"inbounds": [
{
"type": "socks",
"tag": "socks-in",
"listen": "127.0.0.1",
"listen_port": 1080
},
{
"type": "http",
"tag": "http-in",
"listen": "127.0.0.1",
"listen_port": 8080
}
],
"outbounds": [
{
"type": "anytls",
"tag": "anytls-out",
"server": "your-domain.com",
"server_port": 443,
"password": "your_password",
"idle_session_check_interval": "30s",
"idle_session_timeout": "30s",
"min_idle_session": 5,
"tls": {
"enabled": true,
"server_name": "your-domain.com"
}
}
],
"route": {
"final": "anytls-out"
}
}

4.4.3 运行sing-box

1
2
3
4
5
6
7
# 前台运行(测试)
sing-box run -c /usr/local/etc/sing-box/config.json

# 后台运行
nohup sing-box run -c /usr/local/etc/sing-box/config.json &

# 使用LaunchAgent服务(推荐)

4.4.4 配置LaunchAgent

创建文件 ~/Library/LaunchAgents/com.sagernet.sing-box.plist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.sagernet.sing-box</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/sing-box</string>
<string>run</string>
<string>-c</string>
<string>/usr/local/etc/sing-box/config.json</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>

加载服务:

1
launchctl load ~/Library/LaunchAgents/com.sagernet.sing-box.plist

4.5 Linux客户端配置

4.5.1 安装sing-box

1
2
3
4
5
6
7
8
9
10
# Debian/Ubuntu
sudo apt install sing-box

# Arch Linux
sudo pacman -S sing-box

# 或下载二进制
wget https://github.com/SagerNet/sing-box/releases/latest/download/sing-box-linux-amd64.tar.gz
tar -xzf sing-box-linux-amd64.tar.gz
sudo mv sing-box /usr/local/bin/

4.5.2 创建配置文件

创建 /etc/sing-box/config.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
"log": {
"level": "info",
"timestamp": true
},
"inbounds": [
{
"type": "socks",
"tag": "socks-in",
"listen": "127.0.0.1",
"listen_port": 1080
},
{
"type": "http",
"tag": "http-in",
"listen": "127.0.0.1",
"listen_port": 8080
}
],
"outbounds": [
{
"type": "anytls",
"tag": "anytls-out",
"server": "your-domain.com",
"server_port": 443,
"password": "your_password",
"idle_session_check_interval": "30s",
"idle_session_timeout": "30s",
"min_idle_session": 5,
"tls": {
"enabled": true,
"server_name": "your-domain.com"
}
}
],
"route": {
"final": "anytls-out"
}
}

4.5.3 配置systemd服务

创建文件 /etc/systemd/system/sing-box.service

1
2
3
4
5
6
7
8
9
10
11
[Unit]
Description=sing-box Client
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/sing-box run -c /etc/sing-box/config.json
Restart=on-failure

[Install]
WantedBy=multi-user.target

启动服务:

1
2
3
4
5
6
7
8
# 启动服务
sudo systemctl start sing-box

# 开机自启
sudo systemctl enable sing-box

# 查看状态
sudo systemctl status sing-box

4.6 Android客户端配置

4.6.1 sing-box配置

  1. 安装sing-box(从GitHub或Play Store)
  2. 创建配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
"log": {
"level": "info"
},
"inbounds": [
{
"type": "tun",
"tag": "tun-in",
"inet4_address": "172.19.0.1/30",
"auto_route": true,
"strict_route": true,
"stack": "system"
}
],
"outbounds": [
{
"type": "anytls",
"tag": "anytls-out",
"server": "your-domain.com",
"server_port": 443,
"password": "your_password",
"tls": {
"enabled": true,
"server_name": "your-domain.com"
}
}
],
"route": {
"final": "anytls-out"
}
}

4.6.2 v2rayNG配置

v2rayNG目前对AnyTLS的支持有限,建议使用sing-box客户端。

4.7 iOS客户端配置

4.7.1 sing-box配置

  1. 从App Store安装sing-box
  2. 创建配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
"log": {
"level": "info"
},
"inbounds": [
{
"type": "tun",
"tag": "tun-in",
"inet4_address": "172.19.0.1/30",
"auto_route": true,
"strict_route": true
}
],
"outbounds": [
{
"type": "anytls",
"tag": "anytls-out",
"server": "your-domain.com",
"server_port": 443,
"password": "your_password",
"tls": {
"enabled": true,
"server_name": "your-domain.com"
}
}
],
"route": {
"final": "anytls-out"
}
}

4.8 订阅链接格式

AnyTLS目前没有统一的订阅链接格式,主要通过JSON配置文件进行配置。

4.8.1 sing-box配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"outbounds": [
{
"type": "anytls",
"tag": "proxy",
"server": "your-domain.com",
"server_port": 443,
"password": "your_password",
"idle_session_check_interval": "30s",
"idle_session_timeout": "30s",
"min_idle_session": 5,
"tls": {
"enabled": true,
"server_name": "your-domain.com"
}
}
]
}

五、高级配置

5.1 多用户管理

5.1.1 服务端多用户配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
"inbounds": [
{
"type": "anytls",
"tag": "anytls-in",
"listen": "::",
"listen_port": 443,
"users": [
{
"name": "user1",
"password": "password_for_user1"
},
{
"name": "user2",
"password": "password_for_user2"
},
{
"name": "user3",
"password": "password_for_user3"
}
],
"tls": {
"enabled": true,
"server_name": "your-domain.com",
"key_path": "/etc/sing-box/privkey.pem",
"certificate_path": "/etc/sing-box/fullchain.pem"
}
}
]
}

5.2 自定义填充方案

5.2.1 理解填充方案

填充方案用于控制数据包的填充行为,增强流量隐蔽性:

1
2
3
4
5
6
填充方案格式说明:
┌────────────────────────────────────────────────────────────────┐
stop=N # 在N个连接后停止填充 │
│ 数字=范围 # 指定连接类型的填充范围 │
│ c # 继续填充标记 │
└────────────────────────────────────────────────────────────────┘

5.2.2 自定义填充方案示例

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"padding_scheme": [
"stop=10",
"0=50-50",
"1=200-500",
"2=500-800,c,800-1500",
"3=10-10,500-1000",
"4=500-1500",
"5=500-1500",
"6=500-1500",
"7=500-1500"
]
}

5.3 会话参数调优

5.3.1 客户端会话参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"outbounds": [
{
"type": "anytls",
"tag": "anytls-out",
"server": "your-domain.com",
"server_port": 443,
"password": "your_password",
"idle_session_check_interval": "60s",
"idle_session_timeout": "120s",
"min_idle_session": 3,
"tls": {
"enabled": true,
"server_name": "your-domain.com"
}
}
]
}

参数说明

参数建议值说明
idle_session_check_interval30s-60s检查间隔不宜过短
idle_session_timeout60s-120s超时时间根据网络调整
min_idle_session3-10保持适量的空闲会话

5.4 与其他协议混用

5.4.1 多协议服务端配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{
"inbounds": [
{
"type": "anytls",
"tag": "anytls-in",
"listen": "::",
"listen_port": 443,
"users": [
{
"name": "user1",
"password": "password1"
}
],
"tls": {
"enabled": true,
"server_name": "your-domain.com",
"key_path": "/etc/sing-box/privkey.pem",
"certificate_path": "/etc/sing-box/fullchain.pem"
}
},
{
"type": "vless",
"tag": "vless-in",
"listen": "::",
"listen_port": 8443,
"users": [
{
"uuid": "uuid-here",
"flow": "xtls-rprx-vision"
}
],
"tls": {
"enabled": true,
"server_name": "your-domain.com",
"key_path": "/etc/sing-box/privkey.pem",
"certificate_path": "/etc/sing-box/fullchain.pem"
}
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
}
]
}

六、安全性注意事项

6.1 TLS配置安全

6.1.1 证书安全

1
2
3
4
1. 使用有效的TLS证书(推荐Let's Encrypt)
2. 定期更新证书(建议自动续签)
3. 保护私钥安全,设置正确的文件权限
4. 使用强加密套件

6.1.2 证书权限设置

1
2
3
4
5
# 设置证书目录权限
sudo chmod 700 /etc/sing-box
sudo chmod 600 /etc/sing-box/privkey.pem
sudo chmod 644 /etc/sing-box/fullchain.pem
sudo chown root:root /etc/sing-box/*.pem

6.2 用户认证安全

6.2.1 密码安全

1
2
3
4
1. 使用强密码(建议16位以上,包含大小写字母、数字、特殊字符)
2. 不同用户使用不同密码
3. 定期更换密码
4. 不要使用常见密码或字典词汇

6.2.2 生成强密码

1
2
3
4
5
6
# 使用openssl生成密码
openssl rand -base64 24

# 或使用pwgen
sudo apt install pwgen
pwgen -s 32 1

6.3 网络安全

6.3.1 防火墙配置

1
2
3
4
5
6
7
8
9
# 只开放必要端口
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 443/tcp
sudo ufw allow ssh
sudo ufw enable

# 查看规则
sudo ufw status numbered

6.3.2 限制访问来源(可选)

1
2
3
4
5
6
# 只允许特定IP访问
sudo ufw allow from 192.168.1.0/24 to any port 443 proto tcp

# 或使用iptables
sudo iptables -A INPUT -p tcp --dport 443 -s 192.168.1.0/24 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j DROP

6.4 日志与监控

6.4.1 日志配置

1
2
3
4
5
6
7
{
"log": {
"level": "warn",
"output": "/var/log/sing-box/access.log",
"timestamp": true
}
}

6.4.2 日志轮转

创建 /etc/logrotate.d/sing-box

1
2
3
4
5
6
7
8
9
10
11
12
/var/log/sing-box/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0644 root root
postrotate
systemctl reload sing-box > /dev/null 2>&1 || true
endscript
}

6.5 安全最佳实践

1
2
3
4
5
6
7
8
1. 保持sing-box版本更新
2. 定期检查日志,关注异常连接
3. 使用强密码,定期更换
4. 配置防火墙,限制访问来源
5. 使用有效TLS证书,开启自动续签
6. 定期备份配置文件
7. 监控服务器资源使用情况
8. 最小权限原则运行服务

七、常见问题排查

7.1 连接失败排查

症状:无法建立AnyTLS连接

排查步骤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 1. 检查服务状态
sudo systemctl status sing-box

# 2. 检查端口是否监听
sudo ss -tlnp | grep 443

# 3. 检查防火墙
sudo ufw status
sudo iptables -L -n | grep 443

# 4. 测试端口连通性
nc -zv your-domain.com 443

# 5. 检查证书
sudo ls -la /etc/sing-box/*.pem

# 6. 查看日志
sudo journalctl -u sing-box -n 50
tail -f /var/log/sing-box/access.log

# 7. 验证配置
sing-box check -c /etc/sing-box/config.json

常见原因及解决方案

原因解决方案
防火墙阻止开放TCP端口
服务未启动启动sing-box服务
证书无效检查证书路径和有效期
密码错误核对用户名密码
TLS配置错误检查server_name和证书

7.2 连接建立但无法通信

症状:连接成功,但无法访问网络

排查步骤

1
2
3
4
5
6
7
8
9
10
11
# 1. 检查出站配置
# 确保outbounds正确配置direct或自定义路由

# 2. 检查路由配置
# 确保route.final指向正确的出站

# 3. 测试DNS解析
nslookup google.com

# 4. 检查系统代理设置
# 确保浏览器或应用使用正确的代理地址

解决方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 确保基础路由配置正确
{
"route": {
"final": "proxy"
},
"outbounds": [
{
"type": "anytls",
"tag": "proxy",
...
},
{
"type": "direct",
"tag": "direct"
}
]
}

7.3 性能问题排查

症状:连接正常但速度慢或延迟高

排查步骤

1
2
3
4
5
6
7
8
9
10
11
12
# 1. 检查服务器负载
top
htop

# 2. 检查网络延迟
mtr your-domain.com

# 3. 检查带宽
iperf3 -c your-domain.com

# 4. 检查sing-box日志
sudo journalctl -u sing-box -f

优化方案

问题解决方案
服务器负载高升级服务器配置
网络质量差更换服务器或线路
会话参数不当调整idle_session参数
填充方案复杂简化填充方案

7.4 证书问题

症状:TLS证书验证失败

排查步骤

1
2
3
4
5
6
7
8
9
10
11
# 1. 检查证书有效期
openssl x509 -in /etc/sing-box/fullchain.pem -noout -dates

# 2. 验证证书链
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt /etc/sing-box/fullchain.pem

# 3. 检查证书域名
openssl x509 -in /etc/sing-box/fullchain.pem -noout -text | grep -A1 "Subject Alternative Name"

# 4. 测试TLS连接
openssl s_client -connect your-domain.com:443 -servername your-domain.com

解决方案

1
2
3
4
5
# 重新获取证书
sudo certbot renew --force-renewal

# 或使用acme.sh
~/.acme.sh/acme.sh --renew -d your-domain.com --force

7.5 常见错误信息

错误信息可能原因解决方案
certificate verify failed证书无效或过期更新证书
connection refused服务未启动或端口被占检查服务状态
authentication failed密码错误核对用户名密码
timeout网络问题或防火墙检查网络和防火墙
TLS handshake failedTLS配置错误检查TLS配置
user not found用户名不存在检查用户配置

八、总结

8.1 协议优势

  1. TLS兼容:基于标准TLS协议,广泛兼容
  2. 灵活填充:支持自定义填充方案,增强隐蔽性
  3. 简单配置:配置简洁,易于部署
  4. 会话管理:完善的空闲会话管理机制
  5. sing-box集成:与sing-box深度集成,统一管理

8.2 协议局限

  1. 新兴协议:生态系统尚在完善中
  2. 客户端支持有限:主要依赖sing-box客户端
  3. 文档缺乏:官方文档相对简单
  4. 稳定性验证:需要更多实际使用验证
  5. 社区规模小:社区支持和讨论较少

8.3 适用场景建议

场景推荐方案说明
流量隐蔽需求AnyTLS填充方案增强隐蔽
实验性部署AnyTLS适合技术尝鲜
生产环境VLESS/Hysteria2成熟稳定
高性能需求Hysteria2UDP协议更优
广泛兼容Trojan/VLESS客户端支持广

8.4 推荐配置

最佳实践配置(服务端)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{
"log": {
"level": "warn",
"timestamp": true
},
"inbounds": [
{
"type": "anytls",
"tag": "anytls-in",
"listen": "::",
"listen_port": 443,
"users": [
{
"name": "user1",
"password": "strong_password_here"
}
],
"padding_scheme": [
"stop=8",
"0=30-30",
"1=100-400",
"2=400-500,c,500-1000,c,500-1000,c,500-1000,c,500-1000",
"3=9-9,500-1000",
"4=500-1000",
"5=500-1000",
"6=500-1000",
"7=500-1000"
],
"tls": {
"enabled": true,
"server_name": "your-domain.com",
"key_path": "/etc/sing-box/privkey.pem",
"certificate_path": "/etc/sing-box/fullchain.pem"
}
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
}
]
}

最佳实践配置(客户端)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
"log": {
"level": "info",
"timestamp": true
},
"inbounds": [
{
"type": "socks",
"tag": "socks-in",
"listen": "127.0.0.1",
"listen_port": 1080
},
{
"type": "http",
"tag": "http-in",
"listen": "127.0.0.1",
"listen_port": 8080
}
],
"outbounds": [
{
"type": "anytls",
"tag": "proxy",
"server": "your-domain.com",
"server_port": 443,
"password": "your_password",
"idle_session_check_interval": "30s",
"idle_session_timeout": "30s",
"min_idle_session": 5,
"tls": {
"enabled": true,
"server_name": "your-domain.com"
}
}
],
"route": {
"final": "proxy"
}
}

参考资料