https://github.com/RayWangQvQ/sing-box-installer
基于sing-box
和docker
容器化搭建naiveproxy
和hysteria
的保姆级教程。
开源地址:https://github.com/SagerNet/sing-box
sing-box
是一个开源的通用代理部署平台,目的是在当今繁杂的各种代理协议之上,抽象出一个通用接口(interface),来统一各种协议的定义和配置。
有了它,我可以使用同一套配置规则,部署多个不同的协议。
开源地址:
naiveproxy
据说是当前最安全的协议之一,了解到它还是去年(2022年)10月份那次大规模封禁,据说除了naiveproxy
幸免,其他协议均有死伤(包括trojan
,Xray
,V2Ray TLS+Websocket
,VLESS
和gRPC
),详细可查看issue:net4people/bbs#129
为了解决墙的主动探测,它在服务端它使用自己优化过Caddy
(forwardproxy
),利用反代,将没有认证的流量转到一个正常的站点(伪装站点)。也就是,你用你的proxy客户端去访问,认证(用户名+密码)能通过,它就给你做代理;你不用客户端用正常浏览器(或用户名密码错误),只要认证不通过,它就给你反代到正常站点,瞒天过海。
关于TLS指纹问题的讨论,可以看下这个issus:v2ray/v2ray-core#2098
在issue里顺便也了解到,naiveproxy
的作者原来也是trojan
最初的几个作者之一,后来trojan
有些设计上的争议,包括一些优化的想法,由于主程没时间,无法得到实施,于是naiveproxy
的作者就自己开了个项目来实现这些想法,这个项目就是现在的naiveproxy
。
开源地址:https://github.com/apernet/hysteria
hysteria
的优势是快,真的快。同一台机器,我的测试结果是,比我之前的xray
快了2到3倍(网上有人测出快了10倍)。
它是基于quic
协议,走udp,跟它名字一样(歇斯底里),并发去请求扔包,所以快。
已知问题是qos,来自服务商的限制,当请求流量过大时,会被限速、断流。以前看有图比是嫌清晰度不够,用了hysteria
可能要反过来主动去自己调低清晰度了。
关于安全性,目前墙对UDP的管控技术还没有TCP那么成熟,所以相对来说算比较安全。
文档:https://sing-box.sagernet.org/zh/configuration/
部署sing-box
的关键,就是编写它的配置文件。
sing-box
抽象出一套配置规则,类似v2ray
,有DNS,有路由(router),有入站(inbound)和出站(outbound)。
如果之前使用过v2ray
,对这些概念很熟悉,那么你可以很轻松切换到sing-box
;
如果你是个新手,完全不了解这些概念,那么我建议你先去读读v2ray的文档(https://www.v2ray.com)。
因为当前sing-box的文档还处于待完善阶段,只有对各配置字段的解释,并不会告诉你它是什么以及为什么要这么配。
我个人推荐使用docker
容器化部署,容器化有很多好处,这里就不多说了。
下面会基于sing-box
的官方docker
镜像,使用docker-compose
进行容器构建。
官方镜像地址:https://github.com/orgs/SagerNet/packages?repo_name=sing-box
如果你的机器没有装过docker,请先安装docker,安装指令:
curl -sSL https://get.docker.com/ | sh
systemctl start docker
systemctl enable docker
然后基于docker容器run sing-box的官方镜像,我们只需要配置好配置文件config.json即可。
下面有两种模式:一键脚本部署和手动部署,任选其一即可。
# create a dir
mkdir -p ./sing-box && cd ./sing-box
# install
bash <(curl -sSL https://raw.githubusercontent.com/RayWangQvQ/sing-box-installer/main/install.sh)
运行后会让输入参数:
- 域名:需要自己DNS解析好到自己的服务器ip
- 邮箱:用来申请证书的(会自动申请并更新)
- proxy用户名:自己设置
- proxy密码:自己设置
- obfs:hysteria用来混淆的,相当于密码,自己设置
需要在服务器构建如下目录结构:
sing-box
├── data
├── config.json
├── entry.sh
└── tls
└── docker-compose.yml
其中,data/config.json
是sing-box
的配置文件,所有节点配置信息都在里面。
data/entry.sh
是容器启动脚本。
tls文件夹用于存储tls证书,sing-box
可以自动颁发证书,你也可以使用自己现有的证书。如果自动颁发,就空文件夹就行,运行后该目录下会生成证书文件;如果要使用现有证书,可以将证书拷贝到当前文件夹下。
docker-compose.yml
参考内容如下:
version: '3'
services:
sing-box:
image: ghcr.io/sagernet/sing-box
container_name: sing-box
restart: unless-stopped
network_mode: "host"
# ports:
# - 80:80
# - 443:443
# - 8090:8090
# - 10080-10099:10080-10099/udp
volumes:
- ./data:/data
- ./tls:/tls
cap_add:
- NET_ADMIN
devices:
- /dev/net/tun
entrypoint: ["/bin/bash", "/data/entry.sh"]
其中,网络模式使用了network_mode: "host"
,直接使用了宿主机的网络环境,需要关闭宿主机的防火墙,命令如下:
# CentOS:
systemctl disable firewalld
# Debian/Ubuntu:
sudo ufw disable
如果host模式
有问题,也可以切换到指定ports模式(注释掉network_mode
,然后删掉下方prots的注释)
参考内容如下:
#!/bin/bash
set -e
configFilePath="/data/config.json"
logFilePath="/data/sing-box.json"
echo "entry"
sing-box version
# https://sing-box.sagernet.org/configuration/
echo -e "\nconfig:"
sing-box check -c $configFilePath || cat $configFilePath
sing-box format -c /data/config.json -w
cat $configFilePath
echo -e "\nstarting"
sing-box run -c $configFilePath
tail -f $logFilePath
会输出sing-box
版本,检查并格式化配置文件,启动sing-box
,并追踪日志。
最关键的配置文件,参考内容如下:
{
"log": {
"level": "trace",
"output": "/data/sing-box.log",
"timestamp": true
},
"inbounds": [
{
"type": "hysteria",
"tag": "hysteria-in",
"listen": "0.0.0.0",
"listen_port": 10080,
"domain_strategy": "ipv4_only",
"up_mbps": 100,
"down_mbps": 100,
"obfs": "nicetofuckyou",
"users": [
{
"name": "<proxy_name>",
"auth_str": "<proxy_pwd>"
}
],
"tls": {
"enabled": true,
"server_name": "<domain>",
"acme": {
"domain": "<domain>",
"data_directory": "/tls",
"default_server_name": "<domain>",
"email": "<email>"
}
}
},
{
"type": "naive",
"tag": "naive-in",
"listen": "0.0.0.0",
"listen_port": 8090,
"domain_strategy": "ipv4_only",
"users": [
{
"username": "<proxy_name>",
"password": "<proxy_pwd>"
}
],
"network": "tcp",
"tls": {
"enabled": true,
"server_name": "<domain>",
"acme": {
"domain": "<domain>",
"data_directory": "/tls",
"default_server_name": "<domain>",
"email": "<email>"
}
}
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
},
{
"type": "dns",
"tag": "dns-out"
}
],
"route": {
"geoip": {
"path": "/data/geoip.db",
"download_url": "https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db",
"download_detour": "direct"
},
"geosite": {
"path": "/data/geosite.db",
"download_url": "https://github.com/SagerNet/sing-geosite/releases/latest/download/geosite.db",
"download_detour": "direct"
},
"rules": [
{
"protocol": "dns",
"outbound": "dns-out"
}
],
"final": "direct",
"auto_detect_interface": true
}
}
其中,有几处需要替换的地方:
<proxy_name>
替换为代理的用户名,自己取,如Ray
<proxy_pwd>
替换为代理的密码,自己取,如1234@qwer
<domain>
替换为域名<email>
替换为邮箱obfs
是hysteria
混淆字符串,可以自定义
如上就配置了两个节点,一个基于udp的10080端口的hysteria
节点,一个基于tcp的8090端口的naive
节点。
如果你的云上有安全策略,请确保这两个端口都开放了。
证书的话,如果tls目录下没有现有证书,会自动颁发。
其他配置可以查阅官方文档了解。
在docker-compose.yml
同级目录下,执行:
docker compose up -d
等待容器启动。
如果一切正常,就是启动成功,可以去使用自己的客户端连接了。(就是这么简单)
其他参考指令:
# 查看当前运行中的容器
docker ps
# 查看容器启动日志
docker logs sing-box
# 追踪容器运行日志(使用Ctrl C退出追踪)
docker logs -f sing-box
# 进入容器
docker exec -it sing-box bash
其中:
混淆密码
是配置中的obfs
认证荷载
是配置中的auth_str
其中,密码
是配置中的password
todo
配置文件内容如下:
{
"server": "127.0.0.1:%mapping_port%",
"server_name": "sample.zai7lou.ml",
"obfs": "Ray",
"auth_str": "1234@qwer",
"alpn": "h2",
"up_mbps": 100,
"down_mbps": 100,
"socks5": {
"listen": "127.0.0.1:%socks_port%"
}
}
里面的占位符(%mapping_port%
和%socks_port%
)不要动,127.0.0.1
也不要动,这个是本地的代理链,不是服务器的ip和端口。
配置文件内容如下:
{
"server": "sample.zai7lou.ml:10080",
"server_name": "sample.zai7lou.ml",
"obfs": "Ray",
"auth_str": "1234@qwer",
"alpn": "h2",
"up_mbps": 100,
"down_mbps": 100,
"socks5": {
"listen": "127.0.0.1:10808"
},
"http": {
"listen": "127.0.0.1:10809"
}
}
配置文件内容如下:
{
"listen": "socks://127.0.0.1:10808",
"proxy": "https://Ray:1234@[email protected]:8090"
}
同上,跟windows下的Nekoray一样。
sing-box暂不支持,目前只能多加几个hy节点,然后在客户端做负载均衡。