侧边栏壁纸
博主头像
liveJQ博主等级

沒有乐趣,何来开始

  • 累计撰写 182 篇文章
  • 累计创建 68 个标签
  • 累计收到 2 条评论

站群服务器使用 sing-box 部署 SOCKS5 源进源出代理

liveJQ
2025-04-01 / 0 评论 / 0 点赞 / 20 阅读 / 3,287 字

安装 sing-box

环境:Ubuntu 20.04

我有一台站群服务器,单口网卡eno1,配置了多个 HK IP,我需要部署 SOCKS5 服务端,使客户端连接到对应的IP和端口时,从对应的IP出去访问,即达到源进源出的效果。

cd /opt && wget https://github.com/SagerNet/sing-box/releases/download/v1.6.1/sing-box-1.6.1-linux-amd64.tar.gz && tar -xzf sing-box-1.6.1-linux-amd64.tar.gz

创建文件 socks.json 文件

{
  "log": {
      "disabled": false,
      "level": "warn",
      "output": "/var/log/sing-box.log",
      "timestamp": true
    },
  "inbounds": [
    {
      "type": "socks",
      "tag": "socks69",
      "listen": "0.0.0.0",
      "listen_port": 51069,
        "users": [
	{
	"username": "user69",
	"password": "pass69"
  	}
	]
    },
    {
      "type": "socks",
      "tag": "socks70",
      "listen": "0.0.0.0",
      "listen_port": 51070,
        "users": [
	{
	"username": "user70",
	"password": "pass70"
  	}
	]
    }
  ],
  "outbounds": [
    {
      "type": "direct",
      "tag": "out69",
      "inet4_bind_address": "my.hkip.69"
    },
    {
      "type": "direct",
      "tag": "out70",
      "inet4_bind_address": "my.hkip.70"
    }
  ],
  "route": {
    "rules": [
      {
        "inbound": ["socks69"],
        "outbound": "out69"
      },
      {
        "inbound": ["socks70"],
        "outbound": "out70"
      }
    ]
  }
}

测试运行状态

./sing-box run -c socks.json

没有输出就表示运行正常了。

配置为系统服务

先将配置文件移动到 /etc 目录,然后将二进制文件移动到 /usr/local/bin

mkdir /etc/sing-box
cp /opt/sing-box-1.6.1-linux-amd64/socks.json /etc/sing-box
cp /opt/sing-box-1.6.1-linux-amd64/sing-box /usr/local/bin

创建文件 /etc/systemd/system/sing-box@socks.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_SYS_PTRACE CAP_DAC_READ_SEARCH
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
ExecStart=/usr/local/bin/sing-box -D /var/lib/sing-box-%i -c /etc/sing-box/%i.json run
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=10s
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

重载配置

systemctl daemon-reload

运行服务

systemctl start sing-box@socks.service

配置 sing-box 日志自动回滚

创建文件 /etc/logrotate.d/sing-box

/var/log/sing-box.log {
    daily
    rotate 7
    compress
    missingok
    notifempty
    size 100M
    create 640 root adm
    postrotate
        systemctl reload sing-box > /dev/null 2>&1 || true
    endscript
}
  • daily 每天检查
  • rotate 7 最大保留7份,旧日志保存为 xxx.log.1
  • compress 自动 gzip 压缩封存
  • size 100M 达到 100M 才轮换
  • create 640 root adm 新建日志文件默认640权限,root用户和用户组。
  • missingok 有时候日志文件还没自动创建,即缺失也不报错。
  • notifempty 空文件就不轮换了,避免生成无意义的空日志文件。
  • postrotate 里面执行重新加载 sing-box 服务使其将日志写入新的日志文件。

新增或修改配置

可使用如下命令重新加载配置,正常情况下不会影响已存在的配置,即现有服务不会因此中断,例如监听端口。

systemctl reload sing-box@socks.service

为了防止重载配置失败,可先检查配置文件是否存在语法等错误。

sing-box check -c /etc/sing-box/socks.json

新增 inbound 的时候需要确保新添加的端口并未使用,可使用如下脚本批量检查。

#!/bin/bash

start=$1
end=$2

ss -tnl | awk '{print $4}' | grep -Eo '[0-9]+$' | while read port; do
  if [[ $port -ge $start && $port -le $end ]]; then
    echo "Port $port is in use"
  fi
done

保存为 check_port.sh,添加执行权限后,例如我需要检查端口范围为51100-51120是否存在已使用的端口,执行:

./check_port.sh 51100 51120

重载配置完成后,如果想要检查服务是否生效了,可利用 curl 测试 socks 代理服务

curl -x socks5://username:password@your_ip:your_port https://ifconfig.co

参考资料

  1. sing-box 透明代理笔记
  2. sing-box 基础教程
0

评论区