文章

在 Serv00 上源码编译 Sing-box 并部署多协议代理服务

本文记录在 Serv00 VPS 环境下,通过源码编译 Sing-box 并部署多协议代理服务的操作流程。

在 Serv00 上源码编译 Sing-box 并部署多协议代理服务

Sing-box 官方 Github 站点构建的包无法直接在 Serv00 环境下运行,需要从源码编译。在 Serv00 环境下源码编译 Sing-box 非常简单。

1.源码编译 Sing-box

1.首先,通过 SSH 登录 Serv00

1
ssh your_username@s13.serv00.com

2.使用 wget 命令下载源码包并解压

下载地址:https://github.com/SagerNet/sing-box/releases

1
2
3
wget https://github.com/SagerNet/sing-box/archive/refs/tags/v1.10.1.tar.gz

tar zxf v1.10.1.tar.gz

3.进入解压的目录下,执行 make 开始编译

1
cd sing-box-1.10.1 && make

编译完成后,当前目录下会新增一个 sing-box 可执行文件,这个就是编译后的执行程序了。

4.执行 ./sing-box -h 命令即可查看详细使用说明

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
$ ./sing-box -h
Usage:
  sing-box [command]

Available Commands:
  check       Check configuration
  completion  Generate the autocompletion script for the specified shell
  format      Format configuration
  generate    Generate things
  geoip       GeoIP tools
  geosite     Geosite tools
  help        Help about any command
  merge       Merge configurations
  rule-set    Manage rule-sets
  run         Run service
  tools       Experimental tools
  version     Print current version of sing-box

Flags:
  -c, --config stringArray             set configuration file path
  -C, --config-directory stringArray   set configuration directory path
  -D, --directory string               set working directory
      --disable-color                  disable color output
  -h, --help                           help for sing-box

Use "sing-box [command] --help" for more information about a command.

2.多协议代理配置模版

Sing-box 服务端配置文件,只需要配置 inbounds 即可,协议配置详细可参考 Sing-box 官方文档

注意:Serv00 目前仅对用户开放三个自定义端口。下面给出 socks5、trojan、hysteria2 三种协议的 server 入站配置模版:

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
{
  "inbounds": [
    {
      "type": "socks",
      "tag": "socks-in",
      "listen": "0.0.0.0",
      "listen_port": 28509,
      "users": [
        {
          "username": "your.username", // 修改成自定义用户名
          "password": "your.password" // 修改成自定义密码
        }
      ]
    },
    {
      "type": "trojan",
      "listen": "0.0.0.0",
      "listen_port": 63814,
      "users": [
        {
          "password": "your.password" // 修改成自定义密码
        }
      ],
      "tls": {
        "enabled": true,
        "server_name": "bing.com",
        "key_path": "/path/to/server.key", // 可以通过生成自签名证书的方式获取,参考文章最后【生成自签名证书文件】
        "certificate_path": "/path/to/server.crt" // 可以通过生成自签名证书的方式获取,参考文章最后【生成自签名证书文件】
      },
      "multiplex": {
        "enabled": true
      }
    },
    {
      "type": "hysteria2",
      "listen": "0.0.0.0",
      "listen_port": 52072,
      "up_mbps": 100,
      "down_mbps": 100,
      "users": [
        {
          "password": "your.password" // 修改成自定义密码
        }
      ],
      "tls": {
        "enabled": true,
        "server_name": "bing.com",
        "key_path": "/path/to/server.key", // 可以通过生成自签名证书的方式获取,参考文章最后【生成自签名证书文件】
        "certificate_path": "/path/to/server.crt" // 可以通过生成自签名证书的方式获取,参考文章最后【生成自签名证书文件】
      }
    }
  ]
}

配置中,key_path 和 certificate_path 是用于证书认证的,我们可以通过生成自签名证书的方式获取,但注意,在客户端需要设置 insecure 为 true 才能正常通过认证。

2.1 生成自签名证书文件

使用 OpenSSL 生成 RSA 私钥和自签名证书,证书的有效期为 100 年,绑定到域名 bing.com:

1
2
3
4
5
# 生成私钥
openssl genpkey -algorithm RSA -out private.key -pkeyopt rsa_keygen_bits:2048

# 生成自签名证书
openssl req -new -x509 -days 36500 -key private.key -out certificate.crt -subj "/CN=bing.com"

3.启动代理服务

1.把上面的配置保存成 config.json 文件,执行 ./sing-box run -c config.json 测试是否可以成功运行:

1
2
3
4
5
$ ./sing-box run -c config.json
INFO[0000] inbound/socks[socks-in]: tcp server started at 0.0.0.0:28509
INFO[0000] inbound/trojan[1]: tcp server started at 0.0.0.0:63814
INFO[0000] inbound/hysteria2[2]: udp server started at 0.0.0.0:52072
INFO[0000] sing-box started (0.11s)

2.上面表示启动成功,可以使用 nohup 命令,让服务在后台运行:

1
nohup ./sing-box run -c config.json > /dev/null 2>&1 &
本文由作者按照 CC BY 4.0 进行授权