部署sing-box

sing-box 是一款新兴的通用代理平台,提供了丰富的协议支持和灵活的配置选项。

使用的 OpenWrt 版本:23.05

文章内容参考了以下来源:

安装sing-box

更新软件包列表

1
opkg update

安装内核模块和依赖包

1
opkg install kmod-inet-diag kmod-netlink-diag kmod-tun iptables-nft

安装sing-box

1
opkg install sing-box

常用命令:

  • /etc/init.d/sing-box enable 启用 sing-box 服务,作用等效于开启 sing-box 开机自启。固件为了避免和其它代理插件冲突默认禁用 sing-box 服务,使用 sing-box 前请先用此命令启用 sing-box 服务。
  • /etc/init.d/sing-box disable 禁用 sing-box 服务,作用为关闭 sing-box 开机自启。在你尝试过 sing-box 后要切换到其它代理插件时应使用此命令禁用 sing-box 服务,避免机器重启后 sing-box 自动启动造成两个代理插件同时运行发生冲突。
  • /etc/init.d/sing-box start 启动 sing-box,配置文件准备好后使用此命令启动 sing-box。
  • /etc/init.d/sing-box stop 关闭 sing-box,停止 sing-box 运行。
  • /etc/init.d/sing-box reload 重新读取配置文件,当 sing-box 正在运行过程中配置文件发生变化时使用此命令重新读取配置文件。

修改配置 /etc/init.d/sing-box 添加以下内容:

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
#!/bin/sh /etc/rc.common

START=99
USE_PROCD=1

##### ONLY CHANGE THIS BLOCK ######
PROG=/usr/bin/sing-box
RES_DIR=/etc/sing-box/ # resource dir / working dir / the dir where you store ip/domain lists
CONF=./config.json # where is the config file, it can be a relative path to $RES_DIR
##### ONLY CHANGE THIS BLOCK ######

start_service() {
sleep 10
procd_open_instance
procd_set_param command $PROG run -D $RES_DIR -c $CONF

procd_set_param user root
procd_set_param limits core="unlimited"
procd_set_param limits nofile="1000000 1000000"
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
procd_close_instance
echo "sing-box is started!"
}

stop_service() {
service_stop $PROG
echo "sing-box is stopped!"
}

reload_service() {
stop
sleep 5s
echo "sing-box is restarted!"
start
}

添加权限

1
chmod +x /etc/init.d/sing-box

配置

准备配置文件

如果你有订阅链接直接将配置文件下载到 /etc/sing-box 目录。

1
2
mkdir /etc/sing-box
wget -U "sing-box" "订阅地址" -O /etc/sing-box/config.json

可以保存多个订阅的配置文件,注意从文件名进行区分。sing-box 运行时只会读取 config.json ,所以要使用的配置文件修改好后须重命名或者复制一份为 config.json 。

对配置文件 /etc/sing-box/config.json 进行一些修改,添加clash-ui。

1
2
3
4
5
6
7
8
9
"experimental": {
"clash_api":{
"external_controller": "0.0.0.0:9090",
"external_ui": "ui",
"secret": "passwd",
"external_ui_download_url": "https://mirror.ghproxy.com/https://github.com/MetaCubeX/metacubexd/archive/gh-pages.zip",
"external_ui_download_detour": "direct"
}
},
  • external_controller 影响 clash 面板的访问地址。作为 网关/路由 这里的地址必须修改为 0.0.0.0,端口可随意设置只要不与系统本身及其它插件冲突即可。面板访问地址为 http://路由IP:自定义端口/ui
  • external_ui clash 面板源码目录,可随意设置。
  • external_ui_download_url clash 面板静态网页资源的 ZIP 下载地址,当 external_ui 设置的目录不存在或是空目录会按这里设置的地址下载面板文件。实例为 metacubexd 面板,要使用 yacd 面板修改为 “https://mirror.ghproxy.com/https://github.com/MetaCubeX/Yacd-meta/archive/gh-pages.zip
  • external_ui_download_detour 用于下载静态网页资源的出站的标签。如果为空,将使用默认出站。此项可省略。
  • secret clash 面板的登录密码,推荐始终设置一个密码。

检查配置文件:

1
sing-box check -c /etc/sing-box/config.json

使用Tproxy模式

默认情况下 sing-box 是 Tun 模式,Tproxy 模式性能会更好。

找到配置文件 /etc/sing-box/config.json 中的 inbounds 部分对照示例然后将其中包含 "type": "tun" 的整个 {} 中的内容替换为示例代码。这步的作用是将代理模式由 tun 换为 tproxy。

1
2
3
4
5
6
7
8
9
{
"type": "tproxy",
"tag": "tproxy-in",
"listen": "::",
"listen_port": 10105,
"tcp_fast_open": true,
"udp_fragment": true,
"sniff": true
},

检查配置文件:

1
sing-box check -c /etc/sing-box/config.json

创建转发规则,注意替换文件中的内网网段 192.168.8.0/24 改为你的内网网段,例如 192.168.1.0/24

创建文件 /etc/sing-box/nftables.rules 添加以下内容:

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
#!/usr/sbin/nft -f

flush ruleset

define RESERVED_IP = {
100.64.0.0/10,
127.0.0.0/8,
169.254.0.0/16,
172.16.0.0/12,
192.0.0.0/24,
224.0.0.0/4,
240.0.0.0/4,
255.255.255.255/32
}

table inet xray {
chain prerouting {
type filter hook prerouting priority filter; policy accept;
ip daddr $RESERVED_IP return
# 修改为你的内网网段
meta l4proto tcp ip daddr 192.168.8.0/24 return
ip daddr 192.168.8.0/24 udp dport != 53 return
meta mark 0x000000ff return
# 修改为你的透明代理程序的端口
meta l4proto { tcp, udp } meta mark set 0x00000001 tproxy ip to 127.0.0.1:10105 accept
}

chain output {
type route hook output priority filter; policy accept;
ip daddr $RESERVED_IP return
# 修改为你的内网网段
meta l4proto tcp ip daddr 192.168.8.0/24 return
ip daddr 192.168.8.0/24 udp dport != 53 return
meta mark 0x000000ff return
meta l4proto { tcp, udp } meta mark set 0x00000001 accept
}

chain divert {
type filter hook prerouting priority mangle; policy accept;
meta l4proto tcp socket transparent 1 meta mark set 0x00000001 accept
}
}

修改启动配置 /etc/init.d/sing-box 修改为以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  ...
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
procd_close_instance
ip rule add fwmark 1 table 100
ip route add local 0.0.0.0/0 dev lo table 100
nft -f /etc/sing-box/nftables.rules
echo "sing-box is started!"
}

stop_service() {
service_stop $PROG
ip rule del fwmark 1 table 100
ip route del local 0.0.0.0/0 dev lo table 100
nft flush ruleset
echo "sing-box is stopped!"
}

启动 sing-box :

1
2
/etc/init.d/sing-box enable
/etc/init.d/sing-box start

遇到的问题

  • 目前与 Dnsmasq 不兼容,会导致相关功能失效。

部署sing-box
https://frely.github.io/2024/03/16/部署sing-box/
作者
frely
发布于
2024年3月16日
许可协议