跳过正文
  1. Sing-Boxes/

新时代之sing-box

x
作者
x
熟练掌握Spring Boot、Spring Cloud等Java技术栈,专注于分布式系统设计与微服务架构。热爱技术分享,探索编程之美。
目录

新时代之sing-box:下一代通用代理平台
#

sing-box 是一个通用的代理平台,支持多种协议,具有强大的路由功能和高性能表现。它是新一代的网络代理工具,相比传统的代理软件,sing-box 在功能性、性能和可配置性方面都有显著提升。

一、sing-box 简介
#

1.1 什么是 sing-box
#

sing-box 是一个用 Go 语言编写的通用代理平台,旨在提供一个统一的解决方案来处理各种网络代理需求。它继承了 v2ray、clash 等工具的优点,同时在架构设计、性能优化和用户体验上做了大量改进。

核心特性:

  • 多协议支持:支持 Shadowsocks、VMess、VLESS、Trojan、Hysteria、TUIC 等主流协议
  • 强大的路由系统:灵活的分流规则,支持域名、IP、GeoIP、进程等多种匹配方式
  • 高性能:Go 语言编写,内存占用低,并发性能强
  • 跨平台:支持 Windows、macOS、Linux、Android、iOS 等主流平台
  • 现代化设计:配置文件采用 JSON 格式,结构清晰,易于维护
  • DNS 处理:内置 DNS 服务器,支持 DNS 分流和防污染

1.2 为什么选择 sing-box
#

相比其他代理工具,sing-box 具有以下优势:

vs V2Ray:

  • 更简洁的配置格式
  • 更好的性能表现
  • 更完善的文档

vs Clash:

  • 更丰富的协议支持
  • 更灵活的路由规则
  • 更低的资源占用

vs Shadowsocks:

  • 支持更多现代化协议
  • 强大的分流能力
  • 完整的规则引擎

二、主流代理协议详解
#

sing-box 支持多种代理协议,每种协议都有其特点和适用场景。

2.1 Shadowsocks 协议
#

简介: Shadowsocks(SS)是最早期也是最成熟的代理协议之一,以其简单、高效著称。

技术特点:

  • 基于 SOCKS5 协议
  • 使用对称加密算法(如 AES、ChaCha20)
  • 轻量级设计,资源占用少
  • 易于部署和维护

加密方法:

{
  "type": "shadowsocks",
  "tag": "ss-out",
  "server": "example.com",
  "server_port": 8388,
  "method": "aes-256-gcm",  // 推荐使用 AEAD 加密
  "password": "your-password"
}

常用加密算法:

  • aes-128-gcm:平衡性能和安全
  • aes-256-gcm:更高的安全性
  • chacha20-ietf-poly1305:移动设备上性能更好

适用场景:

  • 日常科学上网
  • 移动设备
  • 低延迟需求场景

2.2 VMess 协议
#

简介: VMess 是 V2Ray 项目原创的加密通信协议,提供了更强的混淆和安全特性。

技术特点:

  • 基于 TCP 或 mKCP 传输
  • 动态端口、动态加密
  • 支持时间戳验证,防止重放攻击
  • 可配置 alterID 实现额外混淆

配置示例:

{
  "type": "vmess",
  "tag": "vmess-out",
  "server": "example.com",
  "server_port": 443,
  "uuid": "b831381d-6324-4d53-ad4f-8cda48b30811",
  "security": "auto",
  "alter_id": 0,
  "transport": {
    "type": "ws",
    "path": "/path",
    "headers": {
      "Host": "example.com"
    }
  },
  "tls": {
    "enabled": true,
    "server_name": "example.com"
  }
}

传输方式:

  • TCP:最基础的传输方式
  • WebSocket:可以伪装成正常 HTTPS 流量
  • HTTP/2:利用 HTTP/2 的多路复用特性
  • gRPC:基于 gRPC 框架,抗审查能力强

适用场景:

  • 需要较强混淆能力的场景
  • 配合 CDN 使用
  • WebSocket + TLS 伪装

2.3 VLESS 协议
#

简介: VLESS 是 V2Ray 的轻量级协议,相比 VMess 去除了冗余的加密,性能更优。

技术特点:

  • 无状态协议,不依赖系统时间
  • 去除了内部加密层(依赖外层 TLS)
  • 更低的资源占用
  • 支持 XTLS,进一步提升性能

配置示例:

{
  "type": "vless",
  "tag": "vless-out",
  "server": "example.com",
  "server_port": 443,
  "uuid": "b831381d-6324-4d53-ad4f-8cda48b30811",
  "flow": "xtls-rprx-vision",
  "transport": {
    "type": "tcp",
    "headers": {
      "type": "http",
      "request": {
        "path": ["/"]
      }
    }
  },
  "tls": {
    "enabled": true,
    "server_name": "example.com",
    "reality": {
      "enabled": true,
      "public_key": "public_key_here",
      "short_id": "short_id_here"
    }
  }
}

XTLS 技术:

  • 通过识别内层 TLS 握手,避免双重加密
  • 显著降低 CPU 占用
  • 提升传输速度

适用场景:

  • 追求极致性能
  • 配合 REALITY 实现更好的抗审查
  • 高流量场景(如流媒体)

2.4 Trojan 协议
#

简介: Trojan 通过模仿正常的 HTTPS 流量来绕过防火墙检测。

技术特点:

  • 流量完全伪装成 HTTPS
  • 使用标准 TLS 加密
  • 需要真实的域名和证书
  • 抗检测能力强

配置示例:

{
  "type": "trojan",
  "tag": "trojan-out",
  "server": "example.com",
  "server_port": 443,
  "password": "your-password",
  "tls": {
    "enabled": true,
    "server_name": "example.com",
    "alpn": ["h2", "http/1.1"]
  }
}

工作原理:

  1. 客户端与服务器建立 TLS 连接
  2. 发送验证信息(password hash)
  3. 服务器验证后建立代理通道
  4. 所有流量都在 TLS 加密下传输

适用场景:

  • 网络审查严格的环境
  • 需要高度伪装的场景
  • 配合 CDN 和正常网站共存

2.5 Hysteria 协议
#

简介: Hysteria 是基于 QUIC 协议的新型代理,针对高丢包、不稳定网络环境优化。

技术特点:

  • 基于 UDP 的 QUIC 协议
  • 内置拥塞控制算法
  • 支持多路复用
  • 优秀的弱网表现

配置示例:

{
  "type": "hysteria",
  "tag": "hysteria-out",
  "server": "example.com",
  "server_port": 36712,
  "up_mbps": 100,
  "down_mbps": 100,
  "auth_str": "your-auth-string",
  "tls": {
    "enabled": true,
    "server_name": "example.com",
    "alpn": ["h3"]
  }
}

性能优化:

  • BBR 拥塞控制:智能调整发送速率
  • 0-RTT 握手:减少连接延迟
  • 多路复用:单连接支持多个数据流

适用场景:

  • 跨国长距离连接
  • 高丢包率网络环境
  • 需要高带宽的场景(如流媒体)

2.6 TUIC 协议
#

简介: TUIC 是另一个基于 QUIC 的现代化代理协议,兼顾性能和安全。

技术特点:

  • 基于 QUIC v1
  • 原生支持 0-RTT
  • 内置流量控制
  • 轻量级设计

配置示例:

{
  "type": "tuic",
  "tag": "tuic-out",
  "server": "example.com",
  "server_port": 8443,
  "uuid": "b831381d-6324-4d53-ad4f-8cda48b30811",
  "password": "your-password",
  "congestion_control": "bbr",
  "udp_relay_mode": "native",
  "tls": {
    "enabled": true,
    "server_name": "example.com",
    "alpn": ["h3"]
  }
}

适用场景:

  • 追求极致性能
  • UDP 密集型应用(如游戏、语音通话)
  • 弱网环境

2.7 协议对比总结
#

协议传输协议加密方式性能混淆能力适用场景
ShadowsocksTCP/UDP对称加密日常使用
VMessTCP动态加密通用场景
VLESSTCPTLS中-高高性能需求
TrojanTCPTLS强审查环境
HysteriaUDP/QUICQUIC+TLS极高弱网/高带宽
TUICUDP/QUICQUIC+TLS极高游戏/实时应用

三、sing-box Router 分流机制详解
#

Router(路由/分流)是 sing-box 最核心、最强大的功能之一。通过灵活的路由规则,可以实现精准的流量控制,让不同的网络请求走不同的代理通道。

3.1 Router 工作原理
#

基本流程:

客户端请求 
DNS 解析(可选)
Router 规则匹配
选择对应的 Outbound
建立连接并传输数据

匹配逻辑:

  1. sing-box 接收到网络请求
  2. 按照配置文件中规则的顺序依次匹配
  3. 第一个匹配成功的规则生效
  4. 将流量导向规则指定的 outbound
  5. 如果没有规则匹配,使用默认 outbound

3.2 Router 配置结构
#

基本的 router 配置包含以下部分:

{
  "route": {
    "rules": [
      // 规则列表,按顺序匹配
    ],
    "final": "direct",  // 默认出站
    "auto_detect_interface": true,
    "override_android_vpn": false,
    "default_interface": "eth0",
    "default_mark": 233
  }
}

重要参数说明:

  • rules:规则数组,按顺序匹配
  • final:当所有规则都不匹配时使用的 outbound
  • auto_detect_interface:自动检测出站网卡
  • override_android_vpn:Android 平台覆盖 VPN 设置
  • default_mark:默认的 socket mark(Linux)

3.3 规则类型详解
#

3.3.1 域名匹配(Domain)
#

完整域名匹配:

{
  "domain": ["google.com", "facebook.com"],
  "outbound": "proxy"
}

域名后缀匹配:

{
  "domain_suffix": [".google.com", ".youtube.com"],
  "outbound": "proxy"
}
// 匹配 www.google.com, mail.google.com 等所有子域名

域名关键词匹配:

{
  "domain_keyword": ["google", "youtube", "facebook"],
  "outbound": "proxy"
}
// 匹配包含关键词的所有域名

域名正则匹配:

{
  "domain_regex": ["^.*\\.google\\..*$", "^.*\\.googleapis\\..*$"],
  "outbound": "proxy"
}
// 使用正则表达式进行复杂匹配

3.3.2 IP 地址匹配
#

IP CIDR 匹配:

{
  "ip_cidr": [
    "8.8.8.8/32",
    "1.1.1.1/32",
    "192.168.0.0/16",
    "10.0.0.0/8"
  ],
  "outbound": "direct"
}

IP 版本匹配:

{
  "ip_version": 6,  // 4 或 6
  "outbound": "proxy-ipv6"
}

3.3.3 GeoIP 和 GeoSite
#

sing-box 支持使用 GeoIP 和 GeoSite 数据库进行地理位置和站点分类匹配。

GeoIP 匹配:

{
  "geoip": ["cn", "private"],
  "outbound": "direct"
}
// cn: 中国大陆 IP
// private: 私有 IP(192.168.x.x, 10.x.x.x 等)

GeoSite 匹配:

{
  "geosite": ["cn", "geolocation-!cn"],
  "outbound": "direct"
}
// cn: 中国大陆网站
// geolocation-!cn: 非中国大陆网站

常用 GeoSite 分类:

  • cn:中国大陆网站
  • geolocation-!cn:非中国大陆网站
  • google:Google 服务
  • netflix:Netflix 流媒体
  • youtube:YouTube
  • telegram:Telegram
  • twitter:Twitter
  • facebook:Facebook
  • category-ads-all:广告域名
  • apple-cn:Apple 中国服务

3.3.4 端口匹配
#

单个端口:

{
  "port": [80, 443],
  "outbound": "proxy"
}

端口范围:

{
  "port_range": ["1000:2000", "8000:9000"],
  "outbound": "proxy"
}

3.3.5 进程匹配
#

进程名称匹配(支持 Windows、Linux、macOS):

{
  "process_name": ["chrome.exe", "firefox.exe"],
  "outbound": "proxy"
}

进程路径匹配:

{
  "process_path": [
    "/usr/bin/curl",
    "/Applications/Telegram.app/Contents/MacOS/Telegram"
  ],
  "outbound": "proxy"
}

3.3.6 协议匹配
#

{
  "protocol": ["http", "tls", "quic", "dns"],
  "outbound": "proxy"
}

3.3.7 网络类型匹配
#

{
  "network": "tcp",  // tcp 或 udp
  "outbound": "proxy"
}

3.3.8 源 IP 匹配
#

{
  "source_ip_cidr": ["192.168.1.0/24"],
  "outbound": "proxy"
}
// 根据请求来源 IP 进行分流

3.3.9 入站标签匹配
#

{
  "inbound": ["tun-in", "mixed-in"],
  "outbound": "proxy"
}
// 根据流量来源的 inbound 进行分流

3.4 规则组合与逻辑
#

sing-box 支持在单个规则中组合多个条件:

AND 逻辑(同时满足):

{
  "domain": ["example.com"],
  "port": [443],
  "network": "tcp",
  "outbound": "proxy"
}
// 同时满足域名、端口、网络类型才匹配

取反逻辑:

{
  "invert": true,
  "geoip": ["cn"],
  "outbound": "proxy"
}
// 匹配非中国 IP

规则集(Rule Set):

{
  "rule_set": ["geosite-google", "geosite-youtube"],
  "outbound": "proxy"
}

3.5 完整分流配置示例
#

下面是一个实际的、功能完善的 sing-box 配置示例,展示了如何使用 router 进行精细化分流:

{
  "log": {
    "level": "info",
    "timestamp": true
  },
  "dns": {
    "servers": [
      {
        "tag": "google",
        "address": "tls://8.8.8.8",
        "detour": "proxy"
      },
      {
        "tag": "local",
        "address": "223.5.5.5",
        "detour": "direct"
      },
      {
        "tag": "block",
        "address": "rcode://success"
      }
    ],
    "rules": [
      {
        "geosite": "cn",
        "server": "local"
      },
      {
        "geosite": "category-ads-all",
        "server": "block"
      }
    ],
    "final": "google",
    "strategy": "prefer_ipv4"
  },
  "inbounds": [
    {
      "type": "mixed",
      "tag": "mixed-in",
      "listen": "127.0.0.1",
      "listen_port": 7890,
      "sniff": true,
      "sniff_override_destination": true
    }
  ],
  "outbounds": [
    {
      "type": "vless",
      "tag": "proxy",
      "server": "example.com",
      "server_port": 443,
      "uuid": "b831381d-6324-4d53-ad4f-8cda48b30811",
      "flow": "xtls-rprx-vision",
      "tls": {
        "enabled": true,
        "server_name": "example.com"
      }
    },
    {
      "type": "direct",
      "tag": "direct"
    },
    {
      "type": "block",
      "tag": "block"
    },
    {
      "type": "dns",
      "tag": "dns-out"
    }
  ],
  "route": {
    "rules": [
      {
        "protocol": "dns",
        "outbound": "dns-out"
      },
      {
        "geosite": "category-ads-all",
        "outbound": "block"
      },
      {
        "geoip": ["private"],
        "outbound": "direct"
      },
      {
        "geosite": ["cn"],
        "geoip": ["cn"],
        "outbound": "direct"
      },
      {
        "geosite": ["google", "youtube", "netflix", "telegram"],
        "outbound": "proxy"
      },
      {
        "domain_suffix": [".google.com", ".youtube.com"],
        "outbound": "proxy"
      },
      {
        "ip_cidr": ["8.8.8.8/32", "1.1.1.1/32"],
        "outbound": "proxy"
      }
    ],
    "final": "proxy",
    "auto_detect_interface": true
  }
}

3.6 高级分流场景
#

3.6.1 按流媒体平台分流
#

{
  "route": {
    "rules": [
      {
        "geosite": ["netflix"],
        "outbound": "netflix-proxy"
      },
      {
        "geosite": ["youtube"],
        "outbound": "youtube-proxy"
      },
      {
        "domain_suffix": [".hulu.com", ".hulustream.com"],
        "outbound": "hulu-proxy"
      },
      {
        "geosite": ["disney-plus"],
        "outbound": "disney-proxy"
      }
    ]
  },
  "outbounds": [
    {
      "type": "vless",
      "tag": "netflix-proxy",
      "server": "us-netflix.example.com",
      // ... 美国节点配置
    },
    {
      "type": "vless",
      "tag": "youtube-proxy",
      "server": "hk.example.com",
      // ... 香港节点配置
    }
  ]
}

3.6.2 广告拦截
#

{
  "route": {
    "rules": [
      {
        "geosite": ["category-ads-all"],
        "outbound": "block"
      },
      {
        "domain_suffix": [
          ".doubleclick.net",
          ".googlesyndication.com",
          ".googleadservices.com"
        ],
        "outbound": "block"
      }
    ]
  }
}

3.6.3 分应用代理
#

{
  "route": {
    "rules": [
      {
        "process_name": ["Telegram.exe", "telegram-desktop"],
        "outbound": "telegram-proxy"
      },
      {
        "process_name": ["chrome.exe", "firefox.exe"],
        "domain_suffix": [".google.com"],
        "outbound": "proxy"
      },
      {
        "process_path": ["/usr/bin/wget", "/usr/bin/curl"],
        "outbound": "direct"
      }
    ]
  }
}

3.6.4 国内外智能分流
#

{
  "route": {
    "rules": [
      {
        "protocol": "dns",
        "outbound": "dns-out"
      },
      {
        "geoip": ["private"],
        "outbound": "direct"
      },
      {
        "geosite": ["cn"],
        "outbound": "direct"
      },
      {
        "geoip": ["cn"],
        "outbound": "direct"
      },
      {
        "ip_cidr": [
          "119.29.29.29/32",
          "223.5.5.5/32",
          "223.6.6.6/32"
        ],
        "outbound": "direct"
      }
    ],
    "final": "proxy"
  }
}

3.7 DNS 分流
#

DNS 分流是实现精准路由的关键,可以防止 DNS 污染和 DNS 泄漏。

基本 DNS 配置:

{
  "dns": {
    "servers": [
      {
        "tag": "cloudflare",
        "address": "https://1.1.1.1/dns-query",
        "address_resolver": "local",
        "detour": "proxy"
      },
      {
        "tag": "local",
        "address": "223.5.5.5",
        "detour": "direct"
      },
      {
        "tag": "block",
        "address": "rcode://success"
      }
    ],
    "rules": [
      {
        "geosite": "category-ads-all",
        "server": "block",
        "disable_cache": true
      },
      {
        "geosite": "cn",
        "server": "local"
      },
      {
        "clash_mode": "Direct",
        "server": "local"
      },
      {
        "clash_mode": "Global",
        "server": "cloudflare"
      }
    ],
    "final": "cloudflare",
    "strategy": "prefer_ipv4",
    "disable_cache": false,
    "disable_expire": false
  }
}

DNS 服务器类型:

  • UDPudp://8.8.8.8
  • TCPtcp://8.8.8.8
  • TLStls://8.8.8.8
  • HTTPS (DoH)https://1.1.1.1/dns-query
  • QUIC (DoQ)quic://dns.adguard.com

3.8 路由策略优化
#

规则顺序优化原则:

  1. DNS 请求优先:最先处理 DNS 协议
  2. 广告拦截:其次拦截广告域名
  3. 局域网直连:私有 IP 直连
  4. 特殊应用:特定应用的规则
  5. 国内网站直连:GeoSite/GeoIP 中国规则
  6. 国外服务代理:特定国外服务
  7. 默认策略:最后设置 final

示例顺序:

{
  "route": {
    "rules": [
      // 1. DNS 请求
      {"protocol": "dns", "outbound": "dns-out"},
      
      // 2. 广告拦截
      {"geosite": "category-ads-all", "outbound": "block"},
      
      // 3. 局域网
      {"geoip": ["private"], "outbound": "direct"},
      
      // 4. 特殊应用
      {"process_name": ["Telegram.exe"], "outbound": "proxy"},
      
      // 5. 国内直连
      {"geosite": ["cn"], "outbound": "direct"},
      {"geoip": ["cn"], "outbound": "direct"},
      
      // 6. 国外服务
      {"geosite": ["google", "youtube"], "outbound": "proxy"}
    ],
    "final": "proxy"
  }
}

四、实战配置案例
#

4.1 基础科学上网配置
#

适合日常使用的简单配置:

{
  "log": {
    "level": "info"
  },
  "dns": {
    "servers": [
      {
        "tag": "google",
        "address": "tls://8.8.8.8"
      },
      {
        "tag": "local",
        "address": "223.5.5.5"
      }
    ],
    "rules": [
      {
        "geosite": "cn",
        "server": "local"
      }
    ],
    "final": "google"
  },
  "inbounds": [
    {
      "type": "mixed",
      "tag": "mixed-in",
      "listen": "127.0.0.1",
      "listen_port": 7890
    }
  ],
  "outbounds": [
    {
      "type": "shadowsocks",
      "tag": "proxy",
      "server": "example.com",
      "server_port": 8388,
      "method": "aes-256-gcm",
      "password": "your-password"
    },
    {
      "type": "direct",
      "tag": "direct"
    }
  ],
  "route": {
    "rules": [
      {
        "geoip": ["private"],
        "outbound": "direct"
      },
      {
        "geosite": ["cn"],
        "geoip": ["cn"],
        "outbound": "direct"
      }
    ],
    "final": "proxy"
  }
}

4.2 多节点负载均衡配置
#

通过 URL Test 或 Selector 实现节点选择和故障转移:

{
  "outbounds": [
    {
      "type": "selector",
      "tag": "proxy",
      "outbounds": ["auto", "hk-node", "us-node", "jp-node"],
      "default": "auto"
    },
    {
      "type": "urltest",
      "tag": "auto",
      "outbounds": ["hk-node", "us-node", "jp-node"],
      "url": "https://www.gstatic.com/generate_204",
      "interval": "5m",
      "tolerance": 50
    },
    {
      "type": "vless",
      "tag": "hk-node",
      "server": "hk.example.com",
      "server_port": 443,
      "uuid": "uuid-here"
    },
    {
      "type": "vless",
      "tag": "us-node",
      "server": "us.example.com",
      "server_port": 443,
      "uuid": "uuid-here"
    },
    {
      "type": "vless",
      "tag": "jp-node",
      "server": "jp.example.com",
      "server_port": 443,
      "uuid": "uuid-here"
    },
    {
      "type": "direct",
      "tag": "direct"
    }
  ],
  "route": {
    "rules": [
      {
        "geosite": ["cn"],
        "geoip": ["cn"],
        "outbound": "direct"
      }
    ],
    "final": "proxy"
  }
}

4.3 游戏加速配置
#

针对游戏优化的低延迟配置:

{
  "outbounds": [
    {
      "type": "hysteria",
      "tag": "game-proxy",
      "server": "game.example.com",
      "server_port": 36712,
      "up_mbps": 100,
      "down_mbps": 100,
      "auth_str": "auth-string",
      "tls": {
        "enabled": true,
        "server_name": "game.example.com",
        "alpn": ["h3"]
      },
      "recv_window_conn": 67108864,
      "recv_window": 268435456
    },
    {
      "type": "direct",
      "tag": "direct"
    }
  ],
  "route": {
    "rules": [
      {
        "process_name": [
          "League of Legends.exe",
          "VALORANT.exe",
          "steam.exe"
        ],
        "outbound": "game-proxy"
      },
      {
        "geoip": ["cn"],
        "outbound": "direct"
      }
    ],
    "final": "game-proxy"
  }
}

4.4 流媒体解锁配置
#

针对不同流媒体平台使用不同节点:

{
  "outbounds": [
    {
      "type": "selector",
      "tag": "netflix",
      "outbounds": ["us-node", "uk-node"]
    },
    {
      "type": "selector",
      "tag": "youtube",
      "outbounds": ["hk-node", "tw-node"]
    },
    {
      "type": "vless",
      "tag": "us-node",
      "server": "us.example.com",
      "server_port": 443,
      "uuid": "uuid-here"
    },
    {
      "type": "direct",
      "tag": "direct"
    }
  ],
  "route": {
    "rules": [
      {
        "geosite": ["netflix"],
        "outbound": "netflix"
      },
      {
        "geosite": ["youtube"],
        "outbound": "youtube"
      },
      {
        "geosite": ["cn"],
        "geoip": ["cn"],
        "outbound": "direct"
      }
    ],
    "final": "proxy"
  }
}

五、性能优化与最佳实践
#

5.1 性能优化建议
#

1. 选择合适的协议

  • 日常使用:Shadowsocks(轻量高效)
  • 高性能需求:VLESS + XTLS
  • 弱网环境:Hysteria/TUIC

2. 启用连接复用

{
  "outbounds": [
    {
      "type": "vless",
      "multiplex": {
        "enabled": true,
        "protocol": "h2mux",
        "max_connections": 4,
        "min_streams": 4,
        "max_streams": 0
      }
    }
  ]
}

3. DNS 缓存优化

{
  "dns": {
    "disable_cache": false,
    "disable_expire": false,
    "independent_cache": true
  }
}

4. 减少规则数量

  • 使用 GeoSite/GeoIP 代替大量单个规则
  • 合并相同 outbound 的规则
  • 删除不必要的规则

5. 启用 Sniff

{
  "inbounds": [
    {
      "type": "mixed",
      "sniff": true,
      "sniff_override_destination": true
    }
  ]
}

5.2 安全建议
#

1. 使用 TLS 加密

  • 所有协议都应该启用 TLS
  • 使用真实域名和有效证书
  • 启用 ALPN 协议协商

2. 定期更换密码和 UUID

# 生成新的 UUID
uuidgen

3. 限制监听地址

{
  "inbounds": [
    {
      "listen": "127.0.0.1",  // 仅本地访问
      "listen_port": 7890
    }
  ]
}

4. 启用访问控制

{
  "inbounds": [
    {
      "type": "mixed",
      "users": [
        {
          "username": "user",
          "password": "password"
        }
      ]
    }
  ]
}

5.3 常见问题排查
#

1. 连接失败

  • 检查服务器地址和端口
  • 验证 UUID/密码
  • 确认防火墙规则
  • 查看日志:sing-box run -c config.json

2. DNS 解析错误

  • 检查 DNS 服务器可达性
  • 尝试更换 DNS 服务器
  • 检查 DNS 分流规则

3. 规则不生效

  • 确认规则顺序
  • 检查 GeoSite/GeoIP 数据库
  • 使用日志查看匹配过程

4. 性能问题

  • 减少规则数量
  • 优化 DNS 设置
  • 选择更快的协议
  • 检查网络带宽

5.4 调试技巧
#

启用详细日志:

{
  "log": {
    "level": "debug",
    "output": "box.log",
    "timestamp": true
  }
}

测试规则匹配:

# 使用 curl 测试
curl -x socks5://127.0.0.1:7890 https://www.google.com
curl -x socks5://127.0.0.1:7890 https://www.baidu.com

查看连接统计:

# 使用 API 查看统计(如果启用)
curl http://127.0.0.1:9090/stats

六、总结
#

sing-box 作为新一代通用代理平台,凭借其强大的功能、优秀的性能和灵活的配置,正在成为越来越多用户的首选。

核心优势回顾:

  1. 协议支持广泛:从传统的 Shadowsocks 到现代化的 VLESS、Hysteria,满足各种场景需求
  2. 路由功能强大:灵活的分流规则,精准的流量控制
  3. 性能表现优秀:Go 语言实现,资源占用低,并发能力强
  4. 配置清晰易懂:JSON 格式配置,结构清晰,易于维护
  5. 跨平台支持:支持所有主流操作系统

使用建议:

  • 新手:从简单的 Shadowsocks + 基础分流开始
  • 进阶:尝试 VLESS/Trojan + 多节点负载均衡
  • 高级:深度定制路由规则,针对不同场景优化

未来展望:

随着网络环境的不断变化,sing-box 也在持续演进:

  • 更多协议支持(如 REALITY)
  • 更智能的路由算法
  • 更好的性能优化
  • 更完善的生态系统

无论是日常科学上网、流媒体解锁,还是游戏加速、企业专线,sing-box 都能提供可靠的解决方案。掌握 sing-box 的配置和使用,将为你的网络体验带来质的提升。

参考资料
#


相关文章推荐:

通过邮件回复

相关文章

Spring事务传播机制

spring事务传播机制 # 在Spring框架中,事务传播机制(Transaction Propagation)定义了当一个事务方法被另一个事务方法调用时,事务应该如何传播和处理。Spring提供了多种传播行为,以满足不同的业务需求。以下是Spring中常用的事务传播机制: