feat: automatically adjust Caddyfile and restart Caddy when enabling REALITY fallback on 443
Main / build (push) Waiting to run

This commit is contained in:
2026-07-08 00:56:35 +08:00
parent b0aa99f394
commit 88f9422f01
+31 -5
View File
@@ -150,6 +150,24 @@ get_port() {
done
}
update_caddyfile_fallback() {
[[ ! -f $is_caddyfile ]] && return
# Backup Caddyfile
cp -f $is_caddyfile ${is_caddyfile}.bak
# 1. Replace servers :443 with servers :4443
sed -i 's/servers :443/servers :4443/g' $is_caddyfile
# 2. Replace any site block headers explicit :443 with :4443
sed -i 's/:443[[:space:]]*{/:4443 {/g' $is_caddyfile
# 3. Add https_port 4443 to global block if not present
if [[ ! $(grep "https_port 4443" $is_caddyfile) ]]; then
# Find first { and insert https_port 4443 on the next line
sed -i '0,/^[[:space:]]*{[[:space:]]*$/s//{\n https_port 4443/' $is_caddyfile
fi
}
get_pbk() {
is_tmp_pbk=($($is_core_bin generate reality-keypair | sed 's/.*://'))
is_public_key=${is_tmp_pbk[1]}
@@ -360,6 +378,15 @@ create() {
# config.json
create config.json
fi
# If reality fallback is enabled, automatically update Caddyfile!
if [[ $is_reality_fallback ]]; then
msg "\n自动调整 Caddy 配置文件以支持 4443 端口回落..."
update_caddyfile_fallback
msg "Caddy 配置文件更新成功,备份已保存至 ${is_caddyfile}.bak"
msg "正在重启 Caddy 以释放 443 端口..."
manage restart caddy &>/dev/null
sleep 1
fi
# caddy auto tls
[[ $is_caddy && $host && ! $is_no_auto_tls ]] && {
create caddy $net
@@ -1519,11 +1546,10 @@ info() {
fi
if [[ $is_reality_fallback ]]; then
msg "------------- REALITY Fallback NOTICE -------------"
warn "由于启用了 Caddy 回落分流,你必须手动配置 Caddy 才能使整体网络生效"
msg "1. 将你的 ${yellow}/etc/caddy/Caddyfile${none} 中的默认 https 端口修改为 ${green}4443${none}"
msg " 例如,在全局配置块中添加: ${cyan}https_port 4443${none},或者将 site 域名改为 ${cyan}yourdomain.com:4443${none}"
msg "2. 修改后,请运行命令重启 Caddy ➔ ${green}systemctl restart caddy${none}"
msg "3. 如果 sing-box 启动失败,请检查 Caddy 是否已成功释放了 443 端口。"
_green "检测到启用了 Caddy 回落分流,脚本已自动为你调整了 Caddy 配置文件"
msg "1. 已自动将你的 ${yellow}/etc/caddy/Caddyfile${none} 中的默认 https 端口修改为 ${green}4443${none}"
msg "2. 原配置文件备份已保存至 ${yellow}${is_caddyfile}.bak${none}"
msg "3. Caddy 和 sing-box 已自动重启,现在 443 端口由 sing-box 监听,普通流量会自动分流回落至 Caddy。"
msg "---------------------------------------------------"
fi
footer_msg