sing-box,功能强大且灵活的代理平台

简介

Sing-box 是一个功能强大且灵活的代理平台,旨在提供安全、快速和可定制的网络连接。 它可以被看作是一个现代化的、高度可配置的 VPN 和代理工具,但其能力远不止于此。

官网:https://sing-box.sagernet.org/zh/

源码:https://github.com/SagerNet/sing-box

特性和功能

  • 多协议支持: Sing-box 支持广泛的代理协议,包括但不限于:
    • VMess
    • VLESS
    • Trojan
    • Shadowsocks
    • Socks
    • HTTP(S)
    • gRPC
    • Hysteria
    • TUIC
    • Reality (TLS 握手伪装)
    • XTLS (一种增强的 TLS)
    • WireGuard (VPN 协议)
    • OpenVPN (VPN 协议)
    • NaiveProxy
  • 灵活的路由规则: Sing-box 允许你根据不同的条件(例如域名、IP 地址、地理位置、应用程序等)定义流量的路由规则。 这意味着你可以精确地控制哪些流量通过代理,哪些流量直连。
  • 强大的配置选项: Sing-box 的配置非常灵活,可以使用 JSON 或 YAML 格式进行配置。 这允许你根据自己的需求定制代理行为。
  • 插件支持: Sing-box 支持插件,可以扩展其功能。 例如,可以使用插件来添加新的协议支持、修改流量或执行其他自定义操作。
  • 跨平台: Sing-box 可以在多种平台上运行,包括:
    • Linux
    • macOS
    • Windows
    • Android
    • iOS (需要越狱)
  • 性能优化: Sing-box 经过优化,可以提供高性能的代理服务。
  • 内置 DNS 服务器: Sing-box 包含一个内置的 DNS 服务器,可以用于解析域名。
  • 流量统计: Sing-box 可以提供流量统计信息,帮助你了解代理的使用情况。
  • 规则集支持: Sing-box 支持使用规则集,例如来自 GitHub 的规则集,方便用户快速配置代理规则。

安装

包管理器

仓库安装

Debian/Ubuntu

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
sudo mkdir -p /etc/apt/keyrings &&
   sudo curl -fsSL https://sing-box.app/gpg.key -o /etc/apt/keyrings/sagernet.asc &&
   sudo chmod a+r /etc/apt/keyrings/sagernet.asc &&
   echo '
Types: deb
URIs: https://deb.sagernet.org/
Suites: *
Components: *
Enabled: yes
Signed-By: /etc/apt/keyrings/sagernet.asc
' | sudo tee /etc/apt/sources.list.d/sagernet.sources &&
   sudo apt-get update &&
   sudo apt-get install sing-box # or sing-box-beta

Redhat/DNF5

1
2
sudo dnf config-manager addrepo --from-repofile=https://sing-box.app/sing-box.repo &&
sudo dnf install sing-box # or sing-box-beta

Redhat/DNF4

1
2
3
sudo dnf config-manager --add-repo https://sing-box.app/sing-box.repo &&
sudo dnf -y install dnf-plugins-core &&
sudo dnf install sing-box # or sing-box-beta

手动安装

Debian

1
bash <(curl -fsSL https://sing-box.app/deb-install.sh)

Redhat

1
bash <(curl -fsSL https://sing-box.app/rpm-install.sh)

Archlinux

1
bash <(curl -fsSL https://sing-box.app/rpm-install.sh)

托管安装

Archlinux

1
paru -S sing-box

Debian

1
apt install sing-box

Windows

1
winget install sing-box

手动安装

Debian

1
2
3
mkdir -p ~/.sing-box
cd ~/.sing-box
待续。。。

服务管理

对于带有 systemd 的 Linux 系统,通常安装已经包含 sing-box 服务, 您可以使用以下命令管理服务:

行动 命令
启用 sudo systemctl enable sing-box
禁用 sudo systemctl disable sing-box
启动 sudo systemctl start sing-box
停止 sudo systemctl stop sing-box
强行停止 sudo systemctl kill sing-box
重新启动 sudo systemctl restart sing-box
查看日志 sudo journalctl -u sing-box --output cat -e
实时日志 sudo journalctl -u sing-box --output cat -f

示例模板

服务端

  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
{
    "log": {
        "disabled": false,
        "level": "info",
        "output": "",
        "timestamp": true
    },
    "dns": {
        "servers": [
            {
                "tag": "google",
                "address": "https://8.8.8.8/dns-query",
                "address_resolver": "google-resolver",
                "detour": "direct"
            },
            {
                "tag": "google-resolver",
                "address": "udp://8.8.8.8:53",
                "detour": "direct"
            }
        ],
        "rules": [
            {
                "action": "route",
                "outbound": "any",
                "server": "google"
            }
        ],
        "final": "google"
    },
    "inbounds": [
        {
            "type": "vless",
            "tag": "vless-in",
            "listen": "::",
            "listen_port": 8081,
            "users": [
                {
                    "uuid": "7e92454",
                    "flow": "xtls-rprx-vision"
                }
            ],
            "tls": {
                "enabled": true,
                "server_name": "reality借用证书的域名",
                "reality": {
                    "enabled": true,
                    "handshake": {
                        "server": "reality借用证书的域名",
                        "server_port": 443
                    },
                    "private_key": "reality私有key",
                    "short_id": [
                        "随机id"
                    ]
                }
            }
        },
        {
            "type": "hysteria2",
            "tag": "hy2-in",
            "listen": "::",
            "listen_port": 8082,
            "users": [
                {
                    "name": "hysteria2用户名",
                    "password": "hysteria2密码"
                }
            ],
            "masquerade": "hysteria2认证访问失败回退的域名",
            "tls": {
                "enabled": true,
                "alpn": [
                    "h3"
                ],
                "certificate_path": "/etc/cert/cert.pem",
                "key_path": "/etc/cert/key.pem"
            }
        },
        {
            "type": "tuic",
            "tag": "tuic-in",
            "listen": "::",
            "listen_port": 8083,
            "users": [
                {
                    "name": "tuic用户名",
                    "password": "tuic密码",
                    "uuid": "20865d24-d6ba-4ecf-9c91-93adcf4e31dc"
                }
            ],
            "congestion_control": "bbr",
            "tls": {
                "enabled": true,
                "alpn": [
                    "h3"
                ],
                "certificate_path": "/etc/cert/cert.pem",
                "key_path": "/etc/cert/key.pem"
            }
        }
    ],
    "outbounds": [
        {
            "type": "direct",
            "tag": "direct"
        }
    ],
    "route": {
        "rules": [
            {
                "action": "sniff"
            },
            {
                "protocol": "dns",
                "action": "hijack-dns"
            },
            {
                "ip_is_private": true,
                "action": "reject"
            },
            {
                "rule_set": [
                    "geoip-cn",
                    "geosite-category-ads-all"
                ],
                "action": "route",
                "outbound": "block"
            }
        ],
        "rule_set": [
            {
                "type": "remote",
                "tag": "geoip-cn",
                "format": "binary",
                "url": "https://raw.githubusercontent.com/SagerNet/sing-geoip/rule-set/geoip-cn.srs",
                "download_detour": "direct"
            },
            {
                "type": "remote",
                "tag": "geosite-category-ads-all",
                "format": "binary",
                "url": "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-category-ads-all.srs",
                "download_detour": "direct"
            }
        ],
        "final": "direct"
    }
}

vless-reality-vision

目前服务端使用最多的伪装方案

准备

生成 uuid

1
sing-box generate uuid
1
2
# 生成内容类似如下
# 2203cb2f-fcae-4dc1-ae5a-679b1e4c54fd

生成 reality-keypair

1
sing-box generate reality-keypair
1
2
3
# 生成内容类似如下
# PrivateKey: WEb_WJ47QeAOUlNSAMrrMd19n5xabHhMVg4Te_Ndqlg
# PublicKey: NvQanM4cV2rDGz7ZeSgSDQGpnhMhoTu8Yi4NdyFCThc

生成 short_id

1
openssl rand -hex 8
1
2
# 生成内容类似如下
# e857a701eca0971a

服务端

修改:

  • listen_port:监听端口
  • uuid
  • server_name:套用的证书域名或者 ip
  • server:套用的证书域名或者 ip
  • server_port:套用的证书域名或者 ip 的端口,一般为 443
  • private_key
  • short_id
 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
{
  "inbounds": [
    {
      "type": "vless",
      "tag": "reality-in",
      "listen": "::",
      "listen_port": 443,
      "users": [
        {
          "uuid": "",
          "flow": "xtls-rprx-vision"
        }
      ],
      "tls": {
        "enabled": true,
        "server_name": "",
        "reality": {
          "enabled": true,
          "handshake": {
            "server": "",
            "server_port": 443
          },
          "private_key": "",
          "short_id": [
            ""
          ]
        }
      }
    }
  ]
}

客户端

修改为对应服务端的信息

  • server:服务器的域名或者 ip
  • server_port:服务器的监听端口
  • uuid
  • server_name:套用的证书域名或者 ip
  • public_key
  • short_id
 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
{
  "outbounds": [
    {
      "type": "vless",
      "tag": "reality-out",
      "server": "",
      "server_port": 443,
      "uuid": "",
      "flow": "xtls-rprx-vision",
      "packet_encoding": "xudp",
      "tls": {
        "enabled": true,
        "server_name": "",
        "utls": {
          "enabled": true,
          "fingerprint": "chrome"
        },
        "reality": {
          "enabled": false,
          "public_key": "",
          "short_id": ""
        }
      }
    }
  ]
}

tuic

准备

查看本机 Linux 的网络拥塞控制算法

1
sysctl net.ipv4.tcp_congestion_control

安装 acme.sh,需要修改 mail@example.com

1
2
curl https://get.acme.sh | sh -s email=mail@example.com
source ~/.bashrc
1
acme.sh --set-default-ca --server letsencrypt

申请证书,修改 example.com 为自己要使用的域名

1
acme.sh --issue -d example.com --standalone

安装证书,修改 example.com为自己要使用的域名

1
mkdir -p /etc/cert
1
2
3
acme.sh --install-cert -d example.com \
--key-file       /etc/cert/key.pem  \
--fullchain-file /etc/cert/cert.pem

服务端

修改:

  • listen_port:监听端口
  • name:用户名
  • uuid
  • password:用户密码
  • congestion_control:拥塞控制算法
  • certificate_path:TSL 证书公钥路径
  • key_path:TSL 证书私钥路径
 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
{
  "inbounds": [
    {
      "type": "tuic",
      "tag": "tuic-in",
      "listen": "::",
      "listen_port": 23532,
      "users": [
        {
          "name": "",
          "uuid": "",
          "password": ""
        }
      ],
      "congestion_control": "cubic",
      "tls": {
        "enabled": true,
        "alpn": [
          "h3"
        ],
        "certificate_path": "",
        "key_path": ""
      }
    }
  ]
}

客户端

修改为对应服务端的信息

  • server:服务器的域名或者 ip
  • server_port:服务器的监听端口
  • uuid
  • password:服务器设置的密码
  • congestion_control:拥塞控制算法
  • server_name:服务器域名
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
{
  "outbounds": [
    {
      "type": "tuic",
      "tag": "tuic-out",
      "server": "",
      "server_port": 23532,
      "uuid": "",
      "password": "",
      "congestion_control": "cubic",
      "tls": {
        "enabled": true,
        "server_name": "",
        "insecure": false,
        "alpn": [
          "h3"
        ]
      }
    }
  ]
}

hysteria2

速度很快的协议,基于 UDP,可能会被运营商 QoS

准备

生成 uuid

1
sing-box generate uuid
1
2
# 生成内容类似如下
# 2203cb2f-fcae-4dc1-ae5a-679b1e4c54fd

生成证书

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
curl https://get.acme.sh | sh -s email=mail@example.com

source ~/.bashrc
acme.sh --set-default-ca --server letsencrypt

acme.sh --issue -d example.com --standalone

mkdir -p /etc/cert

acme.sh --install-cert -d example.com \
--key-file       /etc/cert/key.pem  \
--fullchain-file /etc/cert/cert.pem

服务端

替换

  • tag:标签
  • listen:监听地址
  • listen_port:监听端口
  • name:用户名
  • password:用户密码
  • certificate_path:TSL 证书公钥路径
  • key_path:TSL 证书私钥路径
  • masquerade:HTTP3 服务器认证失败时的行为
 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
{
  "inbounds": [
    {
      "type": "hysteria2",
      "tag": "hy2-in",
      "listen": "::",
      "listen_port": 34622,
      "users": [
        {
          "name": "",
          "password": ""
        }
      ],
      "tls": {
        "enabled": true,
        "alpn": [
          "h3"
        ],
        "certificate_path": "",
        "key_path": ""
      },
      "masquerade": "https://apple.com"
    }
  ]
}

客户端

outbound

替换对应服务端的信息

  • server:服务器的域名或者 ip
  • server_port:服务器的端口
  • up_mbps:上传最大带宽
  • down_mbps:下载最大带宽
  • password:服务器设置的密码
  • server_name:服务器域名
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
{
  "outbounds": [
    {
      "type": "hysteria2",
      "tag": "hy2-out",
      "server": "",
      "server_port": 34622,
      "up_mbps": 30,
      "down_mbps": 150,
      "password": "",
      "tls": {
        "enabled": true,
        "server_name": "",
        "insecure": false,
        "alpn": [
          "h3"
        ]
      }
    }
  ]
}

vless-ws-cdn-tls

作用有三:

  • 隐藏服务器 IP
  • 提速垃圾线路
  • 拯救被墙的 IP

准备

生成 uuid

1
sing-box generate uuid
1
2
# 生成内容类似如下
# 2203cb2f-fcae-4dc1-ae5a-679b1e4c54fd

登录 cloudflare,将域名解析到服务器 IP,开启小黄云

进入 SSL/TLS 设置

  • 进入概览,将连接类型改为完全或者完全严格(使用 cloudflare 提供的 15 年源服务器证书)
  • 进入源服务器证书,生成一个通配符域名源服务器证书,记得保存下来,后面会用到

服务端

修改如配置所示:

 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
{
  "log": {
    "level": "info",
    "timestamp": true
  },
  "inbounds": [
    {
      "type": "vless",
      "tag": "vless-in",
      "listen": "::",
      "listen_port": 443,
      "users": [
        {
          "name": "user1",
          "uuid": "请替换为你生成的UUID"
        }
      ],
      "tls": {
        "enabled": true,
        "server_name": "你的域名 (例如 vless.example.com)",
        "certificate_path": "/etc/sing-box/cert/server.crt",
        "key_path": "/etc/sing-box/cert/server.key"
      },
      "transport": {
        "type": "ws",
        "path": "/你的自定义路径 (例如 /chat)",
        "headers": {
          "Host": "你的域名 (例如 vless.example.com)"
        }
      }
    }
  ],
  "outbounds": [
    {
      "type": "direct",
      "tag": "direct"
    }
  ]
}
  1. uuid: 必须替换。在终端输入 sing-box generate uuid 可生成一个新的。
  2. server_name: 填写你在 Cloudflare 解析的域名。
  3. path: WebSocket 的路径,必须以 / 开头,可以是任意字符串,客户端需保持一致。

客户端

修改为对应服务端的信息

 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
{
  "outbounds": [
    {
      "type": "vless",
      "tag": "vless-ws-tls",
      "server": "你的域名 (例如 vless.example.com)",
      "server_port": 443,
      "uuid": "请替换为你生成的UUID",
      "flow": "",
      "tls": {
        "enabled": true,
        "server_name": "你的域名 (例如 vless.example.com)",
        "insecure": false,
        "utls": {
          "enabled": true,
          "fingerprint": "chrome"
        }
      },
      "transport": {
        "type": "ws",
        "path": "/你的自定义路径 (例如 /chat)",
        "headers": {
          "Host": "你的域名 (例如 vless.example.com)"
        }
      }
    }
  ]
}
网站总访客数:Loading
网站总访问量:Loading

-->