Niko's blog

Install sing-box on Openwrt

2023-12-24

前段时间因为clash 删库的事情,就在网上查了一下有什么替代品之类的,发现了一个sing-box 的工具。它支持现在机场上提供的大部分的协议,发现它也是很早前自己曾经使用的sagernet(一个Android 的代理工具)的那群开发者开发的,于是就简单的了解了一下,也在手机使用了一段时间,发现整体的体验还不错。所以就折腾了一下把全平台都迁移一下过来,这样就用一个通用的配置就全平台通用。Android 、iOS 和 Mac OS 上都有客户端可以直接安装就使用了,家里的路由器之前是装的是immortalwrt,需要做一些配置才能安装好使用。

安装

在openwrt 的packages 中已经有了sing-box 的安装源,目前的版本是1.74。因此整个安装会比较简单用opkg 就能完成。

更新一下packages 然后把依赖也一并安装

1
2
3
opkg update
opkg install kmod-inet-diag kmod-netlink-diag kmod-tun iptables-nft
opkg install sing-box

安装完后,/etc/init.d/sing-box/etc/config/sing-box 会被创建,主要是用来开机启动和配置管理,后面会用到.

为了验证安装是否成功以及配置文件是否有问题,用checkrun 命令分别测试一下.

1
2
sing-box check -c {your_config_path}
sing-box run -c {your_config_path} -D {your_work_dir} # 这个会在你运行的地方下载geoip ,geosite 以及cash dashboard 的UI

如果上面的检测没有问题说明安装就正常了. 下面是要配置开机启动和防火墙.

配置防火墙

sing-box 服务启动后不能自己代理流量的,即便你开了了auto_route,需要简单配置一个防火墙.

在/etc/config/firewall中添加规则:

1
2
3
4
5
6
7
8
9
10
11
12
13
config zone
option name 'proxy'
option forward 'REJECT'
option output 'ACCEPT'
option input 'ACCEPT'
option mtu_fix '1'
option device 'tun0'
list network 'proxy'

config forwarding
option name 'lan-proxy'
option dest 'proxy'
option src 'lan'

添加网络接口/etc/config/network

1
2
3
config interface 'proxy'
option proto 'none'
option device 'tun0'

配置开机启动

1
2
3
4
5
config sing-box 'main'
option enabled '1'
option user 'root' # 如果你是用tun 模式的话,一定要改成root
option conffile '/etc/sing-box/config.json'
option workdir '/usr/share/sing-box'

配置订阅

我是自己fork 了一个clash2singbox,在原来的基础上加了地区分组.主要有:

  • HongKong
  • USA
  • Japan
  • Singapore
  • Taiwan
  • Others

通过 Github Action 的方式定期更新订阅并把转换好的配置文件上传到Cloudflare 的免费R2 上.

References

Tags: 工具