给NAS用上Sing Box

目录
摘要

给 NAS 配置一下代理,因为上次使用 clashshell 透明代理出了点问题,我将所有节点重置后NAS从探针面板和 tailscale 都失联了。这次决定用下更轻量的 sing-box ,并且可以减少代理配置面。毕竟 NAS 还是稳定性最重要,我不打算拿来当旁路由使用。

config.json

注意

我使用版本是1.12.16,因为sing-box每个版本的配置文件都会变,其他版本会不适用。

创建配置文件/etc/sing-box/config.json

  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
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{
  "log": {
    "disabled": false,
    "level": "info",
    "timestamp": true
  },
  "experimental": {
    "clash_api": {
      "external_controller": "0.0.0.0:9090",
      "external_ui": "ui",
      "secret": "password",             // 控制台连接密钥,建议修改
      "default_mode": "Rule"                 // 默认分流模式
    }
  },
  "dns": {
    "servers": [
      {
        "tag": "remote",
        "type": "https",
        "server": "1.1.1.1",
        "detour": "Available"                // 远程解析走代理
      },
      {
        "tag": "local",
        "type": "https",
        "server": "223.5.5.5"                // 国内解析走直连
      }
    ],
    "rules": [
      {
        "clash_mode": "Proxy",
        "action": "route",
        "server": "remote"
      },
      {
        "clash_mode": "Direct",
        "action": "route",
        "server": "local"
      },
      {
        "rule_set": ["geosite-cn"],
        "action": "route",
        "server": "local"
      },
      {
        "rule_set": ["category-ads-all"],
        "action": "reject"                   // 广告拦截
      }
    ],
    "strategy": "ipv4_only"
  },
  "inbounds": [
    {
      "type": "mixed",
      "tag": "mixed-in",
      "listen": "0.0.0.0",                   // 允许 Docker 容器及局域网访问代理端口
      "listen_port": 2333,                   // 混合代理端口(HTTP/SOCKS)
      "sniff": true,                         // 开启流量嗅探,用于识别域名
      "sniff_override_destination": true     // 强制将嗅探到的域名作为目标,防止 DNS 污染
    },
    {
      "type": "dns",
      "tag": "dns-in",
      "listen": "127.0.0.1",
      "listen_port": 53                      // 劫持本地 53 端口流量到内置 DNS 引擎
    }
  ],
  "outbounds": [
    {
      "type": "selector",
      "tag": "Available",
      "outbounds": [
        "Reality-Out",
        "Hy2-Out"
      ],
      "default": "Reality-Out"               // 默认使用的出站节点
    },
    {
      "type": "vless",
      "tag": "Reality-Out",
      "server": "your_vps_ip",               // 填入 VPS 的 IP 地址
      "server_port": 8881,                   // 填入 VPS 的端口
      "uuid": "your_uuid",                   // 填入 UUID
      "flow": "xtls-rprx-vision",
      "tls": {
        "enabled": true,
        "server_name": "your_sni",           //  SNI
        "utls": {
          "enabled": true,
          "fingerprint": "firefox"           // 使用 Firefox 指纹,昨天看到消息,使用chrome指纹 uTLS 数据包填充缺失漏洞可被GFW利用高效识别代理流量
        },
        "reality": {
          "enabled": true,
          "public_key": "your_public_key",   // 填入 Reality 公钥
          "short_id": ""                     // 填入 Short ID (如果有)
        }
      },
      "domain_resolver": "local"             // 关键:使用本地 DNS 解析服务器 IP 避免解析回环
    },
    {
      "type": "hysteria2",
      "tag": "Hy2-Out",
      "server": "your_vps_ip",
      "server_port": 443,
      "password": "your_password",           // Hysteria2 节点连接密码
      "up_mbps": 30,                         // 根据你的上行宽带填写
      "down_mbps": 100,                      // 根据你的下行宽带填写
      "tls": {
        "enabled": true,
        "server_name": "your_sni",
        "insecure": false,                   // 正规证书填 false,自签名填 true
        "utls": {
          "enabled": true,
          "fingerprint": "firefox"
        }
      },
      "domain_resolver": "local"
    },
    {
      "type": "direct",
      "tag": "direct"
    }
  ],
  "route": {
    "auto_detect_interface": true,           // 自动检测出口网卡,防止流量回环
    "default_domain_resolver": "local",      // 默认域名解析器
    "rules": [
      {
        "protocol": "dns",
        "action": "hijack-dns"               // 劫持所有 DNS 流量
      },
      {
        "ip_is_private": true,               // 内网 IP 直接连接
        "action": "route",
        "outbound": "direct"
      },
      {
        "rule_set": ["category-ads-all"],
        "action": "reject"
      },
      {
        "rule_set": ["geosite-cn"],          // 中国站点直连
        "action": "route",
        "outbound": "direct"
      },
      {
        "clash_mode": "Proxy",               // 强制代理模式下的路由
        "action": "route",
        "outbound": "Available"
      },
      {
        "clash_mode": "Direct",
        "action": "route",
        "outbound": "direct"
      }
    ],
    "rule_set": [
      {
        "tag": "geosite-cn",
        "type": "remote",
        "format": "binary",
        "url": "https://fastly.jsdelivr.net/gh/SagerNet/sing-geosite@rule-set/geosite-cn.srs",
        "download_detour": "direct"
      },
      {
        "tag": "category-ads-all",
        "type": "remote",
        "format": "binary",
        "url": "https://fastly.jsdelivr.net/gh/SagerNet/sing-geosite@rule-set/geosite-category-ads-all.srs",
        "download_detour": "direct"
      }
    ]
  }
}

填好后,使用sing-box check -c /etc/sing-box/config.json检查JSON是否有错。

systemctl restart sing-box.service,加载配置。

systemctl status sing-box,查看服务是否存活。

journalctl -u sing-box -f,查看实时日志。

配置 systemd

1
2
3
cp sing-box /usr/local/bin/
chmod +x /usr/local/bin/sing-box
sudo vim /etc/systemd/system/sing-box.service
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[Unit]
Description=sing-box service
Documentation=https://sing-box.sagernet.org
After=network.target nss-lookup.target

[Service]
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
ExecStart=/usr/local/bin/sing-box run -c /etc/sing-box/config.json
Restart=on-failure
RestartSec=18s
LimitNOFILE= infinity

[Install]
WantedBy=multi-user.target

启动服务

1
2
3
sudo systemctl daemon-reload
sudo systemctl enable sing-box
sudo systemctl start sing-box

使用

docker 容器

docker-compose.yml 里面添加环境变量

1
2
3
environment:
  - HTTP_PROXY=http://192.168.200.74:2333
  - HTTPS_PROXY=http://192.168.200.74:2333

终端使用

1
2
alias proxyon='export http_proxy="http://127.0.0.1:2333"; export https_proxy="http://127.0.0.1:2333"; export all_proxy="socks5://127.0.0.1:2333"; echo "Proxy ON:127.0.0.1:2333"'
alias proxyoff='unset http_proxy; unset https_proxy; unset all_proxy; echo "Proxy OFF"'

source ~/.bashrc 加载。

后面就可以通过 proxyon使用代理,proxyoff关闭代理了。

切换节点

MetaCubeXD yacd 建议使用第一个面板,第二个刷新有点问题。 如果不想使用在线面板可以下载一个 UI放在 /etc/sing-box/ 目录下。

参考

  1. 强迫症专属 sing-box 配置模板(更新至 1.12 版本) | Blog Rewired 🍀
  2. sing-box 基础教程:sing-box 的配置方法和使用教程 · 云原生实验室
  3. sing-box配置详解 |技术艺术

后面发现 shell crash 已经支持 sing-box 内核了,还是我懂得太少了。

电脑使用 GUI.For.SingBox 可以在GUI快速编辑配置文件,然后导出。 果然觉得麻烦的问题大概已经有人给出了方案,只是我不知道而已。。。