首页
/ sing-box规则集编写:使用GeoIP与GeoSite实现精准路由

sing-box规则集编写:使用GeoIP与GeoSite实现精准路由

2026-02-05 04:01:22作者:平淮齐Percy

什么是规则集

规则集(Rule Set)是sing-box 1.8.0版本引入的功能,用于集中管理和复用路由规则。通过规则集,用户可以将复杂的路由策略模块化,实现更灵活和高效的流量控制。规则集支持内联、本地文件和远程文件三种形式,可以根据实际需求灵活选择。

规则集基本结构

sing-box的规则集主要有以下三种结构形式:

内联规则集

内联规则集是在配置文件中直接定义的规则集合,适用于简单的规则定义。自sing-box 1.10.0版本开始支持。

{
  "type": "inline", // 可选
  "tag": "my_inline_rules",
  "rules": [
    // 规则定义
  ]
}

本地文件规则集

本地文件规则集是存储在本地文件系统中的规则集合,适用于复杂或需要复用的规则。

{
  "type": "local",
  "tag": "my_local_rules",
  "format": "source", // 或 binary
  "path": "/path/to/rules.json"
}

远程文件规则集

远程文件规则集是从远程服务器下载的规则集合,适用于需要定期更新的规则。

{
  "type": "remote",
  "tag": "my_remote_rules",
  "format": "source", // 或 binary
  "url": "https://example.com/rules.json",
  "download_detour": "proxy", // 可选,指定下载规则集使用的出站
  "update_interval": "1d" // 可选,默认1天
}

路由配置中的规则集应用

在sing-box的路由配置中,可以通过rule_set字段引用定义好的规则集。

{
  "route": {
    "rules": [],
    "rule_set": [
      {
        "type": "local",
        "tag": "geoip_rules",
        "format": "source",
        "path": "rules/geoip.json"
      },
      {
        "type": "local",
        "tag": "geosite_rules",
        "format": "source",
        "path": "rules/geosite.json"
      }
    ],
    "final": "direct"
  }
}

GeoSite规则集编写

GeoSite是一种基于域名的规则集合,用于根据域名后缀、关键词或正则表达式进行路由决策。sing-box提供了专门的GeoSite处理模块,可以解析和应用GeoSite规则。

GeoSite规则结构

GeoSite规则主要包含以下几种类型:

  • 精确域名(Domain):完全匹配的域名
  • 域名后缀(Domain Suffix):匹配域名后缀
  • 域名关键词(Domain Keyword):匹配域名中的关键词
  • 域名正则(Domain Regex):使用正则表达式匹配域名

这些规则类型在代码中定义如下:

const (
    RuleTypeDomain ItemType = iota
    RuleTypeDomainSuffix
    RuleTypeDomainKeyword
    RuleTypeDomainRegex
)

编写GeoSite规则集

下面是一个典型的GeoSite规则集示例,用于将中国域名和Google域名分别路由到不同的出站:

{
  "version": 1,
  "rules": [
    {
      "tag": "geosite:cn",
      "domain_suffix": [
        ".cn",
        ".com.cn",
        ".net.cn",
        ".org.cn",
        ".gov.cn"
      ],
      "domain": [
        "baidu.com",
        "qq.com",
        "aliyun.com"
      ],
      "action": "direct"
    },
    {
      "tag": "geosite:google",
      "domain_suffix": [
        ".google.com",
        ".google.co.uk",
        ".google.cn"
      ],
      "domain_keyword": [
        "google",
        "gstatic",
        "ggpht"
      ],
      "action": "proxy"
    }
  ]
}

GeoSite规则解析实现

sing-box的GeoSite规则解析主要在common/geosite/rule.go文件中实现。核心功能是将GeoSite规则转换为sing-box可识别的路由规则:

func Compile(code []Item) option.DefaultRule {
    var domainLength int
    var domainSuffixLength int
    var domainKeywordLength int
    var domainRegexLength int
    for _, item := range code {
        switch item.Type {
        case RuleTypeDomain:
            domainLength++
        case RuleTypeDomainSuffix:
            domainSuffixLength++
        case RuleTypeDomainKeyword:
            domainKeywordLength++
        case RuleTypeDomainRegex:
            domainRegexLength++
        }
    }
    // ... 省略部分代码 ...
    for _, item := range code {
        switch item.Type {
        case RuleTypeDomain:
            codeRule.Domain = append(codeRule.Domain, item.Value)
        case RuleTypeDomainSuffix:
            codeRule.DomainSuffix = append(codeRule.DomainSuffix, item.Value)
        case RuleTypeDomainKeyword:
            codeRule.DomainKeyword = append(codeRule.DomainKeyword, item.Value)
        case RuleTypeDomainRegex:
            codeRule.DomainRegex = append(codeRule.DomainRegex, item.Value)
        }
    }
    return codeRule
}

IP规则集编写

虽然sing-box 1.12.0版本移除了直接的geoip配置,但我们仍然可以通过IP-CIDR规则集实现类似功能。IP规则集基于IP地址范围进行路由决策,适用于根据目标IP地址进行流量控制的场景。

IP-CIDR规则结构

IP-CIDR规则使用CIDR(无类别域间路由)表示法来指定IP地址范围。例如,192.168.1.0/24表示从192.168.1.0到192.168.1.255的所有IP地址。

编写IP规则集

下面是一个典型的IP规则集示例,用于将中国IP地址段和私有IP地址段的流量直接连接,其他流量通过代理:

{
  "version": 1,
  "rules": [
    {
      "tag": "ip:cn",
      "ip_cidr": [
        "1.0.0.0/8",
        "2.0.0.0/8",
        "3.0.0.0/8",
        "42.0.0.0/8",
        "101.0.0.0/8",
        "103.0.0.0/8",
        "110.0.0.0/8",
        "111.0.0.0/8",
        "112.0.0.0/8",
        "113.0.0.0/8",
        "114.0.0.0/8",
        "115.0.0.0/8",
        "116.0.0.0/8",
        "117.0.0.0/8",
        "118.0.0.0/8",
        "119.0.0.0/8",
        "120.0.0.0/8",
        "121.0.0.0/8",
        "122.0.0.0/8",
        "123.0.0.0/8",
        "124.0.0.0/8",
        "125.0.0.0/8",
        "126.0.0.0/8",
        "162.0.0.0/8",
        "175.0.0.0/8",
        "180.0.0.0/8",
        "192.0.0.0/8",
        "198.0.0.0/8",
        "202.0.0.0/8",
        "210.0.0.0/8",
        "221.0.0.0/8",
        "222.0.0.0/8"
      ],
      "action": "direct"
    },
    {
      "tag": "ip:private",
      "ip_cidr": [
        "10.0.0.0/8",
        "172.16.0.0/12",
        "192.168.0.0/16",
        "127.0.0.0/8"
      ],
      "action": "direct"
    }
  ]
}

IP-CIDR规则处理实现

sing-box的IP-CIDR规则处理主要在common/srs/ip_cidr.go文件中实现,核心功能是读取和解析CIDR格式的IP地址范围:

func readPrefix(reader varbin.Reader) (netip.Prefix, error) {
    addrSlice, err := varbin.ReadValue[[]byte](reader, binary.BigEndian)
    if err != nil {
        return netip.Prefix{}, err
    }
    prefixBits, err := varbin.ReadValueuint8
    if err != nil {
        return netip.Prefix{}, err
    }
    return netip.PrefixFrom(M.AddrFromIP(addrSlice), int(prefixBits)), nil
}

完整路由配置示例

下面是一个结合GeoSite和IP-CIDR规则集的完整路由配置示例:

{
  "route": {
    "rule_set": [
      {
        "type": "local",
        "tag": "geosite-cn",
        "format": "source",
        "path": "rules/geosite_cn.json"
      },
      {
        "type": "local",
        "tag": "geosite-google",
        "format": "source",
        "path": "rules/geosite_google.json"
      },
      {
        "type": "local",
        "tag": "ip-cn",
        "format": "source",
        "path": "rules/ip_cn.json"
      },
      {
        "type": "local",
        "tag": "ip-private",
        "format": "source",
        "path": "rules/ip_private.json"
      }
    ],
    "rules": [
      {
        "rule_set": "geosite-cn",
        "action": "direct"
      },
      {
        "rule_set": "geosite-google",
        "action": "proxy"
      },
      {
        "rule_set": "ip-cn",
        "action": "direct"
      },
      {
        "rule_set": "ip-private",
        "action": "direct"
      }
    ],
    "final": "proxy"
  }
}

规则集的更新与维护

为了保证规则集的有效性,需要定期更新。对于本地规则集,可以通过脚本或工具定期更新;对于远程规则集,可以设置自动更新间隔:

{
  "type": "remote",
  "tag": "geosite-auto-update",
  "format": "source",
  "url": "https://example.com/geosite_rules.json",
  "update_interval": "12h"
}

总结

通过规则集和GeoSite/GeoIP功能,sing-box提供了强大而灵活的路由控制能力。合理使用这些功能,可以实现精准的流量管理,提高网络访问效率和安全性。

官方文档中提供了更多关于规则集的详细信息,可以参考docs/configuration/rule-set/index.md了解更多内容。同时,规则解析的源代码也可以在以下文件中找到:

掌握这些知识后,你可以根据自己的需求编写更复杂和精确的路由规则,充分发挥sing-box的强大功能。

登录后查看全文
热门项目推荐
相关项目推荐