本文作者:icy

go-探索 Sing-Box:下一代代理工具的核心引擎

icy 今天 12 抢沙发
go-探索 Sing-Box:下一代代理工具的核心引擎摘要: 探索 Sing-Box:下一代代理工具的核心引擎 什么是 Sing-Box? Sing-Box 是一个用 Go 语言编写的通用代理平台,旨在为各种代理工具提供核心功能支持。它采用模...

go-探索 Sing-Box:下一代代理工具的核心引擎

探索 Sing-Box:下一代代理工具的核心引擎

什么是 Sing-Box?

Sing-Box 是一个用 Go 语言编写的通用代理平台,旨在为各种代理工具提供核心功能支持。它采用模块化设计,支持多种代理协议,包括但不限于 Shadowsocks、VMess、Trojan、VLESS 等,同时提供了灵活的配置方式和强大的扩展能力。

核心特性

1. 多协议支持

Sing-Box 支持当前主流的代理协议: - Shadowsocks (包括 2022 版) - VMess - VLESS - Trojan - Hysteria - TUIC - 以及多种传输层协议(WebSocket、gRPC、QUIC 等)

2. 模块化架构

采用插件化设计,各功能模块相互独立,便于维护和扩展: - 协议实现模块化 - 路由规则可定制 - DNS 解析可配置 - 流量控制灵活

3. 高性能

得益于 Go 语言的并发特性和精心优化的网络栈: - 高并发连接处理 - 内存使用效率高 - 低延迟传输

安装与配置

安装方式

使用预编译二进制文件:

text
# 下载最新版本
wget https://github.com/SagerNet/sing-box/releases/latest/download/sing-box-linux-amd64.tar.gz
tar -xzf sing-box-linux-amd64.tar.gz
cd sing-box-*
sudo install -m 755 sing-box /usr/local/bin/

使用 Docker:

text
docker pull ghcr.io/sagernet/sing-box:latest

基础配置示例

以下是一个简单的 Shadowsocks 服务器配置:

text
{
  "log": {
    "level": "info",
    "timestamp": true
  },
  "inbounds": [
    {
      "type": "shadowsocks",
      "tag": "shadowsocks-in",
      "listen": "0.0.0.0",
      "listen_port": 8388,
      "method": "2022-blake3-aes-128-gcm",
      "password": "your-password-here"
    }
  ],
  "outbounds": [
    {
      "type": "direct",
      "tag": "direct"
    },
    {
      "type": "block",
      "tag": "block"
    }
  ],
  "route": {
    "rules": [
      {
        "geoip": ["private", "cn"],
        "outbound": "direct"
      }
    ]
  }
}

高级功能示例

1. 多入站配置

支持同时监听多个端口和协议:

text
{
  "inbounds": [
    {
      "type": "shadowsocks",
      "tag": "ss-inbound",
      "listen": "0.0.0.0",
      "listen_port": 8388,
      "method": "chacha20-ietf-poly1305",
      "password": "password1"
    },
    {
      "type": "vmess",
      "tag": "vmess-inbound",
      "listen": "0.0.0.0",
      "listen_port": 443,
      "users": [
        {
          "uuid": "your-uuid-here",
          "alterId": 0
        }
      ],
      "transport": {
        "type": "ws",
        "path": "/path"
      }
    }
  ]
}

2. 复杂路由规则

实现智能分流:

text
{
  "route": {
    "rules": [
      {
        "domain_suffix": [".google.com", ".github.com"],
        "outbound": "proxy"
      },
      {
        "ip_cidr": ["10.0.0.0/8", "172.16.0.0/12"],
        "outbound": "direct"
      },
      {
        "domain_keyword": ["netflix", "disney"],
        "outbound": "streaming"
      }
    ],
    "final": "proxy"
  },
  "outbounds": [
    {
      "type": "shadowsocks",
      "tag": "proxy",
      "server": "your-server.com",
      "server_port": 8388,
      "method": "aes-256-gcm",
      "password": "your-password"
    },
    {
      "type": "vless",
      "tag": "streaming",
      "server": "streaming-server.com",
      "server_port": 443,
      "uuid": "your-uuid",
      "flow": "xtls-rprx-vision"
    }
  ]
}

3. DNS 配置

自定义 DNS 解析:

text
{
  "dns": {
    "servers": [
      {
        "tag": "local",
        "address": "223.5.5.5",
        "detour": "direct"
      },
      {
        "tag": "remote",
        "address": "1.1.1.1",
        "detour": "proxy"
      }
    ],
    "rules": [
      {
        "outbound": "direct",
        "server": "local"
      },
      {
        "geosite": "cn",
        "server": "local"
      }
    ],
    "final": "remote"
  }
}

实际应用场景

场景 1:个人代理服务器

text
# 启动服务
sing-box run -c config.json

# 使用 systemd 管理
sudo systemctl enable sing-box
sudo systemctl start sing-box

场景 2:透明代理

配置 TProxy 实现透明代理:

text
{
  "inbounds": [
    {
      "type": "tun",
      "tag": "tun-in",
      "interface_name": "tun0",
      "mtu": 9000,
      "stack": "mixed",
      "endpoint_independent_nat": true,
      "auto_route": true
    }
  ]
}

场景 3:多用户管理

使用 API 进行动态配置管理:

text
# 启用管理 API
{
  "experimental": {
    "clash_api": {
      "external_controller": "127.0.0.1:9090",
      "secret": "your-secret"
    }
  }
}

# 使用 curl 管理
curl -X GET http://127.0.0.1:9090/proxies

性能优化建议

  1. 启用多线程
text
{
  "experimental": {
    "cache_file": {
      "enabled": true,
      "path": "cache.db"
    }
  }
}
  1. 调整系统参数
text
# 增加文件描述符限制
ulimit -n 65535

# 优化 TCP 参数
sysctl -w net.core.rmem_max=67108864
sysctl -w net.core.wmem_max=67108864

监控与日志

日志配置

text
{
  "log": {
    "disabled": false,
    "level": "info",
    "output": "/var/log/sing-box.log",
    "timestamp": true
  }
}

状态监控

text
# 查看运行状态
sing-box check -c config.json

# 测试连接
sing-box test -c config.json --url https://www.google.com

安全建议

  1. 定期更新:保持 Sing-Box 版本最新
  2. 使用强密码:避免使用弱密码或默认密码
  3. 限制访问:配置防火墙规则,只允许必要端口
  4. 启用 TLS:对于 WebSocket 等传输,建议启用 TLS 加密

社区与支持

  • GitHub Issues:报告问题和功能请求
  • Telegram 群组:获取实时帮助
  • 文档:详细的配置说明和 API 文档

总结

Sing-Box 作为一个现代化的代理工具核心引擎,以其高性能、模块化设计和丰富的功能集,为开发者和用户提供了强大的代理解决方案。无论是个人使用还是企业部署,Sing-Box 都能提供稳定、安全、高效的代理服务。

通过合理的配置和优化,Sing-Box 可以满足各种复杂的网络代理需求,同时保持代码的简洁性和可维护性。随着社区的不断发展和功能的持续完善,Sing-Box 有望成为未来代理工具领域的重要基础设施。

sing-box_20260204154825.zip
类型:压缩文件|已下载:1|下载方式:免费下载
立即下载
文章版权及转载声明

作者:icy本文地址:https://zelig.cn/2026/04/495.html发布于 今天
文章转载或复制请以超链接形式并注明出处软角落-SoftNook

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

阅读
分享

发表评论

快捷回复:

验证码

评论列表 (暂无评论,12人围观)参与讨论

还没有评论,来说两句吧...