有时为了方便本地调试和测试,需要搭建一个全局科学代理的环境来测试网络请求的效果,以下是基于 sing-box 来实现一个全局代理的容器方法
首先在服务器上安装 sing-box 1.11.14 版本,下载:
wget https://github.com/SagerNet/sing-box/releases/download/v1.11.14/sing-box-1.11.14-linux-amd64.tar.gz
配置 vless + reality 协议,编辑 config.json
:
{
"log": {
"level": "info"
},
"inbounds": [
{
"type": "vless",
"listen": "::",
"listen_port": 443,
"users": [
{
"uuid": "808288ba-197c-4368-95c2-5c8844e311d9", // 确保服务端和客户端 UUID 一致
"flow": "xtls-rprx-vision"
}
],
"tls": {
"enabled": true,
"server_name": "www.microsoft.com",
"reality": {
"enabled": true,
"handshake": {
"server": "www.microsoft.com",
"server_port": 443
},
"private_key": "", // 使用 ./sing-box generate reality-keypair 生成公钥和密钥,公钥将在客户端中使用
"short_id": ["12345678"]
}
}
}
],
"outbounds": [
{
"type": "direct"
}
]
}
运行:
./sing-box run -c config.json
检查输出无误后,服务器应该已经在 443 端口监听了 vless 协议的连接
接下来在本地机器上安装 sing-box 的容器,Docker 环境请自行安装,新建一个 sing-box 的 docker 目录,里面包含:
$ tree
.
├── compose.yaml
├── Dockerfile
└── sing-box
├── config.json
└── sing-box
Dockerfile 内容如下:
FROM debian:12
# ARG http_proxy
# ARG https_proxy
# ENV http_proxy=http://192.168.1.1:8080
# ENV https_proxy=http://192.168.1.1:8080
# 更改apt源为清华源(可选)
RUN sed -i.bak 's|http://deb.debian.org|http://mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.sources && \
sed -i.bak 's|http://security.debian.org|http://mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.sources
RUN apt-get update && \
apt-get install -y iproute2 iptables curl iperf3 iputils-ping && \
apt-get clean
COPY ./sing-box /app
CMD ["/app/sing-box", "run", "-c", "/app/config.json"]
compose.yaml 内容如下:
services:
sing-box:
build:
context: ./
container_name: sing-box
environment:
- TZ=Asia/Shanghai
devices:
- /dev/net/tun:/dev/net/tun
cap_add:
- NET_ADMIN
restart: unless-stopped
config.json 内容如下:
"log": {
"level": "info",
"timestamp": true
},
"inbounds": [
{
"type": "tun",
"interface_name": "tun0",
"address": [
"172.19.0.1/30"
],
"mtu": 9000,
"auto_route": true,
"strict_route": true,
"sniff": true,
"sniff_override_destination": true
}
],
"outbounds": [
{
"type": "vless",
"tag": "proxy",
"server": "", // 这里填写服务器地址
"server_port": 443,
"uuid": "808288ba-197c-4368-95c2-5c8844e311d9",
"flow": "xtls-rprx-vision",
"tls": {
"enabled": true,
"server_name": "www.microsoft.com",
"utls": {
"enabled": true,
"fingerprint": "chrome"
},
"reality": {
"enabled": true,
"public_key": "", // 这里填写服务器生成的 public_key
"short_id": "12345678"
}
}
},
{
"type": "direct",
"tag": "direct"
}
],
"route": {
"final": "proxy",
"auto_detect_interface": true
}
}
随后运行 sing-box 容器:
docker compose up -d
进入容器内:
docker exec -it sing-box /bin/sh
检测网络连通性:
curl cip.cc