V-PS VLESS-REALITY 代理部署与客户端配置指南

First Post:

Last Update:

Word Count:
5.2k

Read Time:
24 min

Page View: loading...

远程服务器 VLESS-REALITY 代理部署与客户端配置指南

本文档将指导您完成以下全部流程:

  1. 在 AWS Lightsail VPS 上进行基础设置和安全加固。
  2. 部署 Xray-core 并配置 VLESS-REALITY 服务。
  3. 在本地生成客户端所需的 YAML 配置文件。
  4. 通过 GitHub Gist 发布配置文件,生成可供订阅的 HTTP 链接。
  5. 提供一个一键生成本地 YAML 文件的脚本。

1. 简介与准备工作

1.1 VPS 推荐:AWS Lightsail

对于需要稳定、高质量国际网络连接的学术和研究用途,AWS Lightsail 是一个优秀的选择。

关键步骤:

  1. 前往 AWS Lightsail官网 创建一个实例。推荐选择位于东京或新加坡等低延迟地区的实例。
  2. 在实例的管理面板中,进入“网络”选项卡,务必创建一个静态 IP 地址并将其附加到您的实例。这是确保您的服务地址不会因重启而改变的关键。

1.2 VLESS 与 REALITY 协议简述

VLESS 是一个性能卓越、配置灵活的轻量级传输协议。它本身不包含加密,将加密的责任交给了底层传输层(如 TLS),实现了“权责分离”,从而获得了极高的性能和可扩展性。

REALITY (REgular-expression based Application Layer proxy for Inbound TLSY) 是一种革命性的 TLS 代理技术。它通过“借用”一个真实、有效的目标网站的 TLS 证书来伪装流量,解决了传统代理需要购买域名和申请证书的痛点。当流量被审查时,它看起来就像是用户在直接访问一个普通的、知名的大型网站(例如 www.bing.com),从而为学术数据传输和网络研究提供了极佳的隐蔽性和稳定性。

1.3 为什么不推荐使用 Cloudflare 代理?

对于 VLESS-REALITY 这种旨在模仿真实用户直接访问目标网站的协议,不应该再套一层 Cloudflare 代理。原因如下:

  • 特征冲突:Cloudflare 的代理是为隐藏服务器真实 IP、抗 DDoS 等场景设计的,其流量特征(如特定的 TLS 指纹、IP段)非常明显。这与 REALITY 试图模仿直接连接的初衷相悖。
  • 显著增加延迟:所有流量都需要先经过 Cloudflare 的全球网络再到达您的服务器,相当于进行了一次不必要的中转。这会显著增加网络延迟(通常增加 30%-50% 或更高),降低连接速度和响应性。
  • 功能性问题
    • 过多重定向:可能导致某些服务(如 cn.bing.com 的搜索)失败。
    • 人机验证失败:许多网站使用 Cloudflare 的服务来防御机器人,当您的代理流量也通过 Cloudflare 时,可能会触发更严格的人机验证,甚至无法通过。

2. 服务器准备与安全加固

2.1 更新系统

通过 SSH 连接到您的服务器,并执行以下命令,确保所有软件包都是最新的。

1
sudo apt update && sudo apt upgrade -y

2.2 防火墙加固 (UFW)

我们将采用“默认拒绝”策略,只放行必要的端口。

  1. 设置默认策略

    1
    2
    sudo ufw default deny incoming
    sudo ufw default allow outgoing
  2. 允许 SSH 和 Xray 端口

    • 22 是默认 SSH 端口,443 是我们将要使用的 Xray 服务端口。
    • 重要:如果您计划修改 SSH 端口(强烈推荐),请务必先允许新端口,再禁用旧端口。例如,将 SSH 端口改为 7777
    1
    2
    3
    4
    5
    6
    7
    8
    # 允许你自定义的SSH端口
    sudo ufw allow 7777/tcp

    # 允许 Xray 服务端口
    sudo ufw allow 443/tcp

    # 如果您确认新的SSH端口可以登录,可以禁用默认的22端口
    # sudo ufw deny 22/tcp

    注意:请务必检查云服务商(如 AWS Lightsail)自带的网络防火墙规则,确保上述端口(7777443)也已对公网开放。为了省心,您可以选择允许所有 TCP/UDP 端口(0-65535)的流量,但这会降低安全性。

  3. 启用并检查状态

    1
    2
    sudo ufw enable
    sudo ufw status verbose

    当看到 Status: active 和您添加的规则列表时,表示防火墙已成功启动。

2.3 (可选但强烈推荐) SSH 安全强化

  1. 创建新用户并授予 sudo 权限

    1
    2
    3
    # 将 your_username 替换为您想用的用户名
    adduser your_username
    usermod -aG sudo your_username
  2. 配置 SSH 密钥认证

    • 您自己的电脑上生成 SSH 密钥对。
    • 将公钥 (~/.ssh/id_rsa.pub 的内容) 复制到服务器上新用户的 ~/.ssh/authorized_keys 文件中。
    • 确保您能通过密钥免密登录新用户后,再进行下一步。
  3. 修改 SSH 配置文件

    1
    sudo nano /etc/ssh/sshd_config

    找到并修改以下参数:

    1
    2
    3
    4
    5
    6
    7
    8
    # 将端口从 22 改为 1024-65535 之间的任意端口
    Port 7777

    # 禁止 root 用户远程登录
    PermitRootLogin no

    # 禁用密码认证,强制使用密钥登录
    PasswordAuthentication no
  4. 检查云服务商的覆盖配置
    检查 /etc/ssh/sshd_config.d/ 目录下是否有云服务商的配置文件(如 50-cloud-init.conf)。如果该文件强制 PasswordAuthentication yes,请将其修改为 no 或删除该文件。

  5. 重启 SSH 服务

    1
    sudo systemctl restart ssh

    现在,您需要使用新的端口和密钥才能登录服务器。

2.4 网络性能优化:启用 TCP BBR

BBR 算法能显著改善长距离、高延迟网络下的连接速度和吞吐量。

  1. 修改系统控制参数

    1
    sudo nano /etc/sysctl.conf

    在文件末尾添加以下两行:

    1
    2
    net.core.default_qdisc=fq
    net.ipv4.tcp_congestion_control=bbr
  2. 应用并验证配置

    1
    2
    3
    4
    5
    # 使配置立即生效
    sudo sysctl -p

    # 验证 BBR 是否已启用
    sysctl net.ipv4.tcp_congestion_control

    如果输出结果为 net.ipv4.tcp_congestion_control = bbr,则表示配置成功。

    注意:一些云服务商(如 AWS Lightsail)默认就允许了,也是好事。


3. 核心服务部署:Xray-core

3.1 安装 Xray-core

使用官方一键安装脚本来安装最新版本。

1
bash -c "$(curl -L [https://github.com/XTLS/Xray-install/raw/main/install-release.sh](https://github.com/XTLS/Xray-install/raw/main/install-release.sh))" @ install

3.2 生成必要凭证

在配置前,我们需要生成一个 UUID 和一对 REALITY 密钥。

  1. 生成 UUID

    1
    /usr/local/bin/xray uuid

    记录下这串唯一的字符串,例如 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

  2. 生成 REALITY 密钥对

    1
    /usr/local/bin/xray x25519

    这会同时输出 私钥 (PrivateKey)公钥 (PublicKey)。请务必妥善保存这两个密钥。私钥用于服务器配置,公钥用于客户端配置。

3.3 编写 config.json 配置文件

使用以下内容完全替换 /usr/local/etc/xray/config.json 文件。

1
sudo nano /usr/local/etc/xray/config.json

完整配置示例 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{
"log": {
"loglevel": "warning"
},
"routing": {
"domainStrategy": "IPIfNonMatch",
"rules": [
{
"type": "field",
"ip": ["geoip:private"],
"outboundTag": "block"
},
{
"type": "field",
"protocol": ["bittorrent"],
"outboundTag": "block"
}
]
},
"inbounds": [
{
"port": 443,
"protocol": "vless",
"settings": {
"clients": [
{
"id": "your_uuid_here", // <--- 替换为您生成的 UUID
"level": 0,
"flow": "xtls-rprx-vision"
}
],
"decryption": "none"
},
"streamSettings": {
"network": "tcp",
"security": "reality",
"realitySettings": {
"show": false,
"dest": "[www.bing.com:443](https://www.bing.com:443)", // <--- 伪装的目标网站,可自行更换
"xver": 0,
"serverNames": [
"[www.bing.com](https://www.bing.com)", // <--- 必须与 dest 网站证书上的域名匹配
"bing.com"
],
"privateKey": "your_private_key_here", // <--- 粘贴您生成的 PrivateKey
"maxTimeDiff": 60000,
"shortIds": [
"",
"abcdef0123456789" // <--- 可自定义的 shortId,客户端需要使用
]
}
},
"sniffing": {
"enabled": true,
"destOverride": ["http", "tls"]
}
}
],
"outbounds": [
{
"protocol": "freedom",
"tag": "direct"
},
{
"protocol": "blackhole",
"tag": "block"
}
]
}

配置说明

  • id: 替换为您自己生成的 UUID。
  • privateKey: 替换为您自己生成的私钥。
  • shortIds: 这是一个 shortId 列表,客户端可以任选其一使用。你可以自定义一个,比如 abcdef0123456789
  • destserverNames: 这是 REALITY 的核心。dest 必须是一个墙外、支持 TLS 1.3 的稳定大网站。serverNames 必须包含 dest 网站证书上的域名。www.bing.com 是一个不错的选择。

3.4 服务管理 (systemd)

  1. 测试配置

    1
    /usr/local/bin/xray run -test -config /usr/local/etc/xray/config.json

    如果显示 Configuration OK,则表示配置无误。

  2. 启动并设置开机自启

    1
    2
    sudo systemctl restart xray
    sudo systemctl enable xray
  3. 检查服务状态

    1
    sudo systemctl status xray

    确保服务状态是 active (running)

  4. 实时查看日志(用于排错):

    1
    sudo journalctl -u xray -f

4. 客户端配置

4.1 生成 YAML 代理片段

您需要根据服务器上的配置,填写以下客户端 YAML 片段。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
proxies:
- name: Catpaw-Tokyo-Reality # <--- 节点名称,可自定义
type: vless
server: your_server_ip_here # <--- 替换为您的服务器静态IP地址
port: 443
uuid: your_uuid_here # <--- 替换为您的 UUID
network: tcp
tls: true
udp: true
flow: xtls-rprx-vision
servername: [www.bing.com](https://www.bing.com) # <--- 必须与服务器 config.json 中的 serverNames 之一完全匹配
reality-opts:
public-key: your_public_key_here # <--- 替换为您生成的 PublicKey
short-id: abcdef0123456789 # <--- 替换为您在服务器上设置的 shortId
client-fingerprint: chrome

4.2 使用 GitHub Gist 发布订阅文件

  1. 登录 GitHub 并访问 gist.github.com
  2. 在文件名处输入 my_subscription.yaml
  3. 将一份完整的 Clash YAML 配置(例如文末脚本生成的内容)粘贴到代码框中。
  4. 点击右下角的 Create secret gist。这会创建一个无法被搜索到的私密 Gist。
  5. 创建成功后,点击页面右上角的 Raw 按钮。浏览器地址栏中的 URL 就是可以直接用于 Clash 客户端的订阅链接。

5. 一键生成完整 YAML 配置文件脚本

这个 shell 脚本会提示您输入必要信息,然后自动在当前目录下生成一个名为 config.yaml 的完整 Clash 配置文件。

您可以将以下代码保存为一个名为 create_clash_config.sh 的文件。

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash

# ANSI Color Codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${GREEN}--- Clash VLESS-REALITY YAML 动态配置生成脚本 ---${NC}"
echo "本脚本将引导您生成一份完整的 Clash 配置文件。"
echo ""

# --- 获取用户输入 ---
read -p "$(echo -e ${YELLOW}'[1/6] 请为您的代理节点命名 (例如: AWS-Tokyo-01): '${NC})" PROXY_NAME
read -p "$(echo -e ${YELLOW}'[2/6] 请输入您的服务器 IP 地址: '${NC})" SERVER_IP
read -p "$(echo -e ${YELLOW}'[3/6] 请输入您在服务器config.json中设置的 servername (例如: www.bing.com): '${NC})" SERVER_NAME
read -p "$(echo -e ${YELLOW}'[4/6] 请输入您的 UUID: '${NC})" UUID
read -p "$(echo -e ${YELLOW}'[5/6] 请输入您的 REALITY PublicKey: '${NC})" PUBLIC_KEY
read -p "$(echo -e ${YELLOW}'[6/6] 请输入您在服务器上设置的 short-id: '${NC})" SHORT_ID


# --- 检查输入 ---
if [[ -z "$PROXY_NAME" || -z "$SERVER_IP" || -z "$SERVER_NAME" || -z "$UUID" || -z "$PUBLIC_KEY" || -z "$SHORT_ID" ]]; then
echo -e "\n${RED}错误:所有字段均为必填项。脚本已中止,请重新运行。${NC}"
exit 1
fi

# --- 定义配置文件名 ---
CONFIG_FILE="config.yaml"

# --- 使用 cat 和 EOF 创建 YAML 文件 ---
# 使用用户输入的变量来填充模板
cat <<EOF > ${CONFIG_FILE}
mixed-port: 7890
mode: rule
ipv6: false
geodata-mode: true
geox-url:
geoip: https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.dat
geosite: https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geosite.dat
mmdb: https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.metadb
geo-auto-update: false
geo-update-interval: 24
log-level: info
global-client-fingerprint: chrome
tcp-concurrent: true
find-process-mode: strict
unified-delay: true
profile:
store-selected: true
store-fake-ip: true
tun:
enable: false
stack: mixed
auto-route: true
auto-detect-interface: true
dns-hijack:
- any:53
- tcp://any:53
sniffer:
enable: true
force-dns-mapping: true
parse-pure-ip: true
override-destination: true
sniff:
QUIC:
ports:
- 443
- 8443
TLS:
ports:
- 443
- 8443
HTTP:
ports:
- 80
- 8080-8880
force-domain:
- +.v2ex.com
skip-domain:
- Mijia Cloud
dns:
enable: true
respect-rules: false
listen: 0.0.0.0:1053
ipv6: false
default-nameserver:
- 223.5.5.5
- 119.29.29.29
enhanced-mode: fake-ip
fake-ip-range: 198.18.0.1/16
fake-ip-filter-mode: blacklist
fake-ip-filter:
- '*'
- +.lan
- +.local
- +.stun.*.*
- +.stun.*.*.*
- +.stun.*.*.*.*
- +.stun.*.*.*.*.*
nameserver:
- 223.5.5.5
- 119.29.29.29
fallback:
- tls://8.8.4.4
- tls://1.1.1.1
proxy-server-nameserver:
- https://223.5.5.5/dns-query
- https://1.12.12.12/dns-query
direct-nameserver:
- system
direct-nameserver-follow-policy: false
fallback-filter:
geoip: true
geoip-code: CN
nameserver-policy:
gfw
ipcidr:
- 240.0.0.0/4
domain:
- +.google.com
- +.facebook.com
- +.youtube.com
proxies:
- name: ${PROXY_NAME}
type: vless
server: ${SERVER_IP}
port: 443
uuid: ${UUID}
network: tcp
tls: true
udp: true
flow: xtls-rprx-vision
servername: ${SERVER_NAME}
reality-opts:
public-key: ${PUBLIC_KEY}
short-id: ${SHORT_ID}
client-fingerprint: chrome
proxy-groups:
- name: Proxy
type: select
proxies:
- ${PROXY_NAME}
- name: AdBlock
type: select
proxies:
- REJECT
- DIRECT
rule-providers:
reject:
type: http
behavior: domain
url: https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/reject.txt
path: ./ruleset/reject.yaml
interval: 86400
proxy:
type: http
behavior: domain
url: https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/proxy.txt
path: ./ruleset/proxy.yaml
interval: 86400
gfw:
type: http
behavior: domain
url: https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/gfw.txt
path: ./ruleset/gfw.yaml
interval: 86400
steamCN:
type: http
behavior: classical
url: https://cdn.jsdelivr.net/gh/blackmatrix7/ios_rule_script@master/rule/Clash/SteamCN/SteamCN.yaml
path: ./ruleset/steamCN.yaml
interval: 86400
GameDownload:
type: http
behavior: classical
url: https://cdn.jsdelivr.net/gh/blackmatrix7/ios_rule_script@master/rule/Clash/Game/GameDownload/GameDownload.yaml
path: ./ruleset/GameDownload.yaml
interval: 86400
Bing:
type: http
behavior: classical
url: https://cdn.jsdelivr.net/gh/blackmatrix7/ios_rule_script@master/rule/Clash/Bing/Bing.yaml
path: ./ruleset/Bing.yaml
interval: 86400
rules:
- IP-CIDR,10.10.9.9/32,DIRECT
- DOMAIN-SUFFIX,playerinfinite.com,Proxy
- DOMAIN-SUFFIX,levelinfinite.com,Proxy
- DOMAIN-SUFFIX,nodeseek.com,Proxy
- RULE-SET,GameDownload,DIRECT
- RULE-SET,steamCN,DIRECT
- RULE-SET,reject,AdBlock
- GEOSITE,category-ads,AdBlock
- RULE-SET,gfw,Proxy
- GEOSITE,cn,DIRECT
- GEOIP,cn,DIRECT
- MATCH,Proxy
EOF

echo ""
echo -e "${GREEN}成功!配置文件 '${CONFIG_FILE}' 已在当前目录生成。${NC}"
echo "它现在包含您自定义的节点名称和服务器信息。"
echo "您可以直接在 Clash 客户端中导入此文件。"

如何使用此脚本

  1. 将上面的代码复制并粘贴到一个新文件中,例如 create_clash_config.sh
  2. 在您的终端中,给这个文件添加可执行权限:chmod +x create_clash_config.sh
  3. 运行脚本:./create_clash_config.sh
  4. 按照提示输入您的服务器IP、UUID、PublicKey 和 short-id。
  5. 脚本执行完毕后,一个名为 config.yaml 的文件就会出现在当前目录下,您可以直接使用了。

懒人包

从一个新创建的、拥有 sudo 权限的用户开始,一键完成服务器环境配置、Xray 安装与配置,直到最后启动并验证服务。

使用方法

  1. 以您新创建的、拥有 sudo 权限的用户身份登录到您的服务器。
  2. 将下面的代码完整复制,并粘贴到一个新文件中。例如,命名为 setup_xray.sh
    1
    2
    nano setup_xray.sh
    # 粘贴代码,然后按 Ctrl+X, Y, Enter 保存退出
  3. 给该脚本文件添加可执行权限:
    1
    chmod +x setup_xray.sh
  4. 运行脚本:
    1
    ./setup_xray.sh
  5. 根据脚本的提示,输入所需信息并按回车键继续。

一键部署脚本 (setup_xray.sh)

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash

# ==============================================================================
# Xray VLESS-REALITY 一键部署脚本 (适用于 Debian/Ubuntu)
#
# 本脚本将执行以下操作:
# 1. 更新系统软件包
# 2. 配置 UFW 防火墙
# 3. 启用 TCP BBR 网络优化
# 4. 安装最新的 Xray-core
# 5. 自动生成 REALITY 凭证并创建配置文件
# 6. 启动并设置 Xray 开机自启
# ==============================================================================

# --- 全局变量和函数 ---

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 使脚本在遇到错误时退出
set -e
set -o pipefail

# 打印彩色消息
print_color() {
COLOR=$1
MESSAGE=$2
echo -e "${COLOR}${MESSAGE}${NC}"
}

# 暂停并等待用户按键
press_any_key() {
echo ""
print_color "$YELLOW" "请按任意键继续..."
read -n 1 -s -r
echo ""
}

# 检查是否以 root 身份运行
if [[ $(id -u) -eq 0 ]]; then
print_color "$RED" "错误:请不要以 root 用户身份运行此脚本。请使用具有 sudo 权限的普通用户执行。"
exit 1
fi

# --- 脚本主流程 ---

clear
print_color "$GREEN" "======================================================="
print_color "$GREEN" " Xray VLESS-REALITY 服务器端一键部署脚本"
print_color "$GREEN" "======================================================="
echo ""
print_color "$YELLOW" "本脚本将引导您完成所有必要的服务器配置。"
press_any_key

# --- 步骤 1: 系统更新 ---
print_color "$GREEN" "[1/5] 正在更新系统软件包..."
sudo apt update && sudo apt upgrade -y
print_color "$GREEN" "✅ 系统更新完成。"
press_any_key

# --- 步骤 2: 配置防火墙 (UFW) ---
print_color "$GREEN" "[2/5] 正在配置 UFW 防火墙..."
read -p "$(echo -e ${YELLOW}'请输入您当前使用的 SSH 端口 (默认为 22): '${NC})" SSH_PORT
SSH_PORT=${SSH_PORT:-22}

sudo ufw default deny incoming >/dev/null
sudo ufw default allow outgoing >/dev/null
sudo ufw allow ${SSH_PORT}/tcp
sudo ufw allow 443/tcp
sudo ufw --force enable
print_color "$GREEN" "✅ 防火墙已启用并配置完成。当前规则:"
sudo ufw status verbose
press_any_key

# --- 步骤 3: 启用 TCP BBR ---
print_color "$GREEN" "[3/5] 正在启用 TCP BBR 网络优化..."
{
echo "net.core.default_qdisc=fq"
echo "net.ipv4.tcp_congestion_control=bbr"
} | sudo tee /etc/sysctl.d/99-bbr.conf >/dev/null
sudo sysctl -p /etc/sysctl.d/99-bbr.conf >/dev/null

if sysctl net.ipv4.tcp_congestion_control | grep -q "bbr"; then
print_color "$GREEN" "✅ TCP BBR 已成功启用。"
else
print_color "$RED" "❌ TCP BBR 启用失败,请检查内核版本(需 >= 4.9)。"
fi
press_any_key

# --- 步骤 4: 安装 Xray-core ---
print_color "$GREEN" "[4/5] 正在安装最新的 Xray-core..."
sudo bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install >/dev/null 2>&1

if [ -f "/usr/local/bin/xray" ]; then
print_color "$GREEN" "✅ Xray-core 安装成功。"
else
print_color "$RED" "❌ Xray-core 安装失败。"
exit 1
fi
press_any_key

# --- 步骤 5: 生成配置并启动服务 ---
print_color "$GREEN" "[5/5] 正在生成 Xray 配置文件并启动服务..."

# 交互式获取伪装域名
print_color "$YELLOW" "REALITY 需要一个真实存在的、支持 TLS 1.3 的目标网站进行伪装。"
read -p "$(echo -e ${YELLOW}'请输入伪装域名 (默认为 www.bing.com): '${NC})" FAKE_DEST_DOMAIN
FAKE_DEST_DOMAIN=${FAKE_DEST_DOMAIN:-"www.bing.com"}
# 自动处理 www 前缀,生成 serverNames 列表
if [[ $FAKE_DEST_DOMAIN == www.* ]]; then
SERVER_NAMES="\"${FAKE_DEST_DOMAIN}\", \"$(echo ${FAKE_DEST_DOMAIN} | sed 's/www.//')\""
else
SERVER_NAMES="\"${FAKE_DEST_DOMAIN}\""
fi

# 生成凭证
print_color "$YELLOW" "正在生成 UUID 和 REALITY 密钥对..."
USER_UUID=$(/usr/local/bin/xray uuid)
KEY_PAIR_OUTPUT=$(/usr/local/bin/xray x25519)
PRIVATE_KEY=$(echo "$KEY_PAIR_OUTPUT" | grep "Private key" | awk '{print $3}')
PUBLIC_KEY=$(echo "$KEY_PAIR_OUTPUT" | grep "Public key" | awk '{print $3}')
SHORT_ID=$(head /dev/urandom | tr -dc 'a-f0-9' | head -c 16)

# 创建 config.json 文件
CONFIG_JSON_CONTENT=$(cat <<EOF
{
"log": {
"loglevel": "warning"
},
"routing": {
"domainStrategy": "IPIfNonMatch",
"rules": [
{ "type": "field", "ip": ["geoip:private"], "outboundTag": "block" },
{ "type": "field", "protocol": ["bittorrent"], "outboundTag": "block" }
]
},
"inbounds": [
{
"port": 443,
"protocol": "vless",
"settings": {
"clients": [
{
"id": "${USER_UUID}",
"level": 0,
"flow": "xtls-rprx-vision"
}
],
"decryption": "none"
},
"streamSettings": {
"network": "tcp",
"security": "reality",
"realitySettings": {
"show": false,
"dest": "${FAKE_DEST_DOMAIN}:443",
"xver": 0,
"serverNames": [${SERVER_NAMES}],
"privateKey": "${PRIVATE_KEY}",
"shortIds": ["${SHORT_ID}"]
}
},
"sniffing": { "enabled": true, "destOverride": ["http", "tls"] }
}
],
"outbounds": [
{ "protocol": "freedom", "tag": "direct" },
{ "protocol": "blackhole", "tag": "block" }
]
}
EOF
)

echo "${CONFIG_JSON_CONTENT}" | sudo tee /usr/local/etc/xray/config.json >/dev/null

# 测试、重启并设置开机自启
print_color "$YELLOW" "正在验证配置并启动 Xray..."
if sudo /usr/local/bin/xray run -test -config /usr/local/etc/xray/config.json; then
sudo systemctl restart xray
sudo systemctl enable xray
print_color "$GREEN" "✅ Xray 启动成功!"
sleep 2
sudo systemctl status xray --no-pager -l
else
print_color "$RED" "❌ Xray 配置文件无效!请检查 /usr/local/etc/xray/config.json"
exit 1
fi

# --- 最终信息汇总 ---
echo ""
print_color "$GREEN" "======================================================================="
print_color "$GREEN" " 🎉 恭喜!服务器端部署已全部完成! 🎉"
print_color "$GREEN" "======================================================================="
echo ""
print_color "$YELLOW" "请妥善保管以下客户端连接参数:"
echo "-----------------------------------------------------------------------"
echo -e " 服务器地址 (IP): $(curl -s ip.sb)"
echo -e " 端口 (Port): 443"
echo -e " UUID: ${GREEN}${USER_UUID}${NC}"
echo -e " 流控 (Flow): xtls-rprx-vision"
echo -e " 加密 (Security): reality"
echo -e " 域名 (ServerName): ${GREEN}${FAKE_DEST_DOMAIN}${NC}"
echo -e " 公钥 (PublicKey): ${GREEN}${PUBLIC_KEY}${NC}"
echo -e " 短ID (ShortId): ${GREEN}${SHORT_ID}${NC}"
echo -e " 指纹 (Fingerprint): chrome"
echo "-----------------------------------------------------------------------"
echo ""
print_color "$YELLOW" "您现在可以使用以上参数配置您的客户端了。"
echo ""