版本一:共用 443 端口 - Podman 部署 Sing-box(自管证书) + Caddy

本方案中,Sing-box 自己管理证书,通过 ACME 协议自动获取。Caddy 作为四层代理,将 443 端口的流量转发至 Sing-box 的 8443 端口。

1. 清理旧环境

podman pod stop gateway 2>/dev/null || true
podman pod rm gateway 2>/dev/null || true

2. 创建 Gateway Pod

创建名为 gateway 的 Pod,暴露 80 和 443 端口。

podman pod create \
  --name gateway \
  --memory 512m \
  --cpus 0.6 \
  --publish 80:80 \
  --publish 443:443

3. 启动容器

3.1 启动 Caddy(使用支持 layer4 的镜像)

podman run -d \
  --name caddy \
  --pod gateway \
  --restart always \
  -v /data/caddy/config.json:/etc/caddy/config.json:Z \
  registry.cn-beijing.aliyuncs.com/k7scn/caddy2:latest \
  caddy run --config /etc/caddy/config.json

3.2 启动 Sing-box

podman run -d \
  --name singbox \
  --pod gateway \
  --restart always \
  -v /data/singbox/config.json:/etc/sing-box/config.json:Z \
  -v /data/singbox/certs:/etc/sing-box/certs:Z \
  ghcr.io/sagernet/sing-box:latest \
  run -c /etc/sing-box/config.json

4. 生成 UUID

podman run --rm ghcr.io/sagernet/sing-box:latest generate uuid

5. 配置文件示例

5.1 Caddy 配置 (/data/caddy/config.json)

Caddy 仅作为四层代理,将 443 端口的流量转发至本地的 8443 端口。

{
  "admin": {
    "disabled": true
  },
  "logging": {
    "logs": {
      "default": {
        "level": "ERROR",
        "writer": {
          "output": "discard"
        }
      }
    }
  },
  "apps": {
    "layer4": {
      "servers": {
        "public": {
          "listen": [":443"],
          "routes": [
            {
              "handle": [
                {
                  "handler": "proxy",
                  "upstreams": [
                    {"dial": ["127.0.0.1:8443"]}
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  }
}

5.2 Sing-box 配置 (/data/singbox/config.json)

Sing-box 监听 8443 端口,启用 TLS 并通过 ACME 自动获取证书。

{
  "log": {
    "level": "error",
    "timestamp": true,
    "output": "/dev/stdout"
  },
  "inbounds": [
    {
      "type": "vless",
      "tag": "vless-in",
      "listen": "0.0.0.0",
      "listen_port": 8443,
      "users": [
        {
          "uuid": "0c980228d60a"
        }
      ],
      "tls": {
        "enabled": true,
        "server_name": "zing.baidaoya.qzz.io",
        "acme": {
          "domain": ["zing.baidaoya.qzz.io"],
          "data_directory": "/etc/sing-box/certs/acme",
          "email": "347399012@qq.com",
          "provider": "letsencrypt"
        }
      }
    }
  ],
  "outbounds": [
    {
      "type": "direct",
      "tag": "direct"
    }
  ]
}

6. 部署说明

  • 端口使用:Pod 对外暴露 80 和 443,Caddy 监听 443 并转发到 Sing-box 的 8443 端口。
  • 证书管理:由 Sing-box 通过 ACME 自动申请和更新 Let's Encrypt 证书。
  • 数据持久化

    • Caddy 配置文件挂载自 /data/caddy/config.json
    • Sing-box 配置和证书目录分别挂载自 /data/singbox/config.json/data/singbox/certs
  • 安全性:容器内文件使用 :Z 标记以适应 SELinux 上下文。

版本二:共用 443 端口 - Podman 部署 Reality 协议的 Sing-box + Caddy 服务

本版本中,Caddy 作为 L4 代理不再处理 HTTPS,仅做流量转发。Sing-box 使用 Reality 协议,无需维护域名和证书,抗检测能力更强。

Reality 密钥说明:Reality 密钥是 Reality 协议中用于加密通信的一对公私钥,由 private_key(私钥)和 public_key(公钥)组成。

1. 安全性对比

Reality 在抗检测方面比传统 TLS 更安全:

安全特性传统 TLSReality
服务端指纹有明显特征完全消除
证书依赖需要购买/申请域名证书借用他人网站证书,无需自己维护
证书链攻击可能被伪造证书攻击攻击无效
深度包检测(DPI)容易被识别特征握手模拟真实网站,难以区分
前向保密性支持完整保持
抗量子安全不支持已支持 ML-DSA-65 抗量子签名验证

2. 协议选择建议

场景推荐协议原因
高审查网络环境(如校园网、企业网)Reality抗检测能力强,流量特征难以识别
追求极致性能、低配设备XTLSCPU 占用更低,速度略快
不想维护域名和证书Reality无需购买域名,无需申请证书
已有现成域名和证书XTLS部署更简单,配置更熟悉
普通家庭使用两者均可差异不明显,按需选择

3. 清理旧环境

podman pod stop gateway 2>/dev/null || true
podman pod rm gateway 2>/dev/null || true

4. 创建 Gateway Pod

podman pod create \
  --name gateway \
  --memory 512m \
  --cpus 0.6 \
  --publish 80:80 \
  --publish 443:443

5. 生成 Reality 密钥

podman run --rm ghcr.io/sagernet/sing-box:latest generate reality-keypair > /tmp/reality-key.txt
PRIVATE_KEY=$(grep PrivateKey /tmp/reality-key.txt | awk '{print $2}')
PUBLIC_KEY=$(grep PublicKey /tmp/reality-key.txt | awk '{print $2}')
echo "PrivateKey: $PRIVATE_KEY"
echo "PublicKey: $PUBLIC_KEY"

6. 生成 Short ID

如果 sing-box 旧版本不支持该命令,可使用 openssl rand -hex 8python3 -c "import secrets; print(secrets.token_hex(8))" 替代。
podman run --rm ghcr.io/sagernet/sing-box:latest generate short-id

7. 启动容器

7.1 启动 Sing-box

podman run -d \
  --name singbox \
  --pod gateway \
  --restart always \
  -v /data/singbox/config.json:/etc/sing-box/config.json:Z \
  -v /data/singbox/certs:/etc/sing-box/certs:Z \
  ghcr.io/sagernet/sing-box:latest \
  run -c /etc/sing-box/config.json

7.2 启动 Caddy(使用支持 layer4 的镜像)

podman run -d \
  --name caddy \
  --pod gateway \
  --restart always \
  -v /data/caddy/config.json:/etc/caddy/config.json:Z \
  registry.cn-beijing.aliyuncs.com/k7scn/caddy2:latest \
  caddy run --config /etc/caddy/config.json

8. 配置文件示例

8.1 Caddy 配置 (/data/caddy/config.json)

Caddy 作为四层代理,将 443 端口流量转发至 Sing-box 的 8443 端口。

{
  "admin": {
    "disabled": true
  },
  "logging": {
    "logs": {
      "default": {
        "level": "ERROR",
        "writer": {
          "output": "discard"
        }
      }
    }
  },
  "apps": {
    "layer4": {
      "servers": {
        "public": {
          "listen": [":443"],
          "routes": [
            {
              "handle": [
                {
                  "handler": "proxy",
                  "upstreams": [
                    {"dial": ["127.0.0.1:8443"]}
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  }
}

8.2 Sing-box 配置 (/data/singbox/config.json)

Sing-box 监听 8443 端口,启用 Reality 协议。

{
  "log": {
    "level": "info",
    "timestamp": true,
    "output": "/dev/stdout"
  },
  "inbounds": [
    {
      "type": "vless",
      "tag": "vless-reality-in",
      "listen": "0.0.0.0",
      "listen_port": 8443,
      "users": [
        {
          "uuid": "0c980228d60a"
        }
      ],
      "tls": {
        "enabled": true,
        "server_name": "www.bing.com",
        "reality": {
          "enabled": true,
          "handshake": {
            "server": "www.bing.com",
            "server_port": 443
          },
          "private_key": "4KTghpSRoClFpXnI",
          "short_id": ["c69ff710"]
        }
      }
    }
  ],
  "outbounds": [
    {
      "type": "direct",
      "tag": "direct"
    }
  ]
}

9. 部署说明

  • 端口使用:Pod 对外暴露 80 和 443,Caddy 监听 443 并转发到 Sing-box 的 8443 端口。
  • 协议选择:使用 Reality 协议,无需维护域名和证书,抗检测能力更强。
  • 目标网站:配置中借用 www.bing.com 的 TLS 会话特征,实际使用时可替换为其他高信誉网站。
  • 数据持久化

    • Caddy 配置文件挂载自 /data/caddy/config.json
    • Sing-box 配置和证书目录分别挂载自 /data/singbox/config.json/data/singbox/certs
  • 安全性:容器内文件使用 :Z 标记以适应 SELinux 上下文。

10. 注意事项

  1. 替换密钥:配置中的 private_keyshort_id 为示例值,请使用实际生成的密钥替换。
  2. 替换 UUID:配置中的 uuid 为示例值,请使用 sing-box generate uuid 生成新的 UUID。
  3. 目标网站选择server_namehandshake.server 应保持一致,建议选择延迟低、稳定的高信誉网站(如 www.bing.comcloudflare.com 等)。
  4. Caddy 镜像:确保使用支持 layer4 模块的 Caddy 镜像,否则无法进行四层代理转发。

手机小飞机配置

1. Shadowrocket 配置步骤

在手机上打开 Shadowrocket,点击右上角 “+” 添加节点,选择类型为 VLESS,然后按以下字段填写:

字段填写内容说明
类型VLESSReality 基于 VLESS 协议
地址你的服务器 IP 或域名
端口443因为使用了 Caddy 做 L4 透传,客户端连接 443 端口
UUID你生成的 UUID
传输方式 (Network)tcpReality 目前主要支持 TCP
TLS开启(√)必须开启
TLS 版本1.3建议选择
SNI / 服务器名www.bing.com关键:必须填写服务端 server_name 字段的域名
Reality 开关开启(√)开启后会显示下方字段
公钥 (Public Key)你的 Reality 公钥关键:服务端生成的公钥
Short ID你的短 ID关键:服务端配置的 Short ID
指纹 (Fingerprint)chromerandom建议选择 chrome,模拟浏览器指纹

2. 关键注意事项

Reality 与普通 VLESS 的区别

在 Shadowrocket 中,Reality 本质上是 VLESS 协议下的一种特殊 TLS 配置。你在填写时务必确认:

  • 必须手动将 “Reality” 开关拨到开启状态,否则软件会把它当成普通的 VLESS + TLS 连接,导致无法连接。
  • SNI 必须填服务端 server_name 的域名(如 www.bing.com),不要填你自己的域名。

标签: none

添加新评论