# 给NAS用上Sing Box


<!--more-->

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


## config.json
>[!NOTE]
>我使用版本是1.12.16，因为sing-box每个版本的配置文件都会变，其他版本会不适用。

创建配置文件`/etc/sing-box/config.json`
```jsonc
{
  "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
```shell
cp sing-box /usr/local/bin/
chmod +x /usr/local/bin/sing-box
sudo vim /etc/systemd/system/sing-box.service
```

```
[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
```

启动服务
```shell
sudo systemctl daemon-reload
sudo systemctl enable sing-box
sudo systemctl start sing-box
```


## 使用
### docker 容器
`docker-compose.yml` 里面添加环境变量
```yml
environment:
  - HTTP_PROXY=http://192.168.200.74:2333
  - HTTPS_PROXY=http://192.168.200.74:2333
```

### 终端使用
```shell
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](https://metacubexd.pages.dev/)
[yacd](https://yacd.haishan.me/)
建议使用第一个面板，第二个刷新有点问题。
如果不想使用在线面板可以下载一个 UI放在 `/etc/sing-box/` 目录下。
## 参考
1. [强迫症专属 sing-box 配置模板（更新至 1.12 版本） \| Blog Rewired 🍀](https://blog.rewired.moe/post/sing-box-config/)
2. [sing-box 基础教程：sing-box 的配置方法和使用教程 · 云原生实验室](https://icloudnative.io/posts/sing-box-tutorial/)
3. [sing-box配置详解 |技术艺术](https://blog.agfw.top/archives/314)

---
{.awesome-hr}

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

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


---

> 作者: Chifan  
> URL: https://eo.chifan.de/posts/sing-box/  

