Understanding Rule Providers
Relevant source files
The following files were used as context for generating this wiki page:
This project, a self-use collection of proxy分流 (shunting) rules based on ACL4SSR, heavily relies on the concept of "Rule Providers" for the Clash proxy client. Rule providers are external files that contain a list of domain names, IP addresses, or keywords. Clash uses these providers to dynamically update its routing rules, determining whether a network connection should be sent directly, through a proxy, or be rejected. This system allows for modular, maintainable, and easily updatable rule management without altering the main configuration file.
The repository contains automation scripts to generate these provider files from simpler .list formats, catering to both Clash (.yaml) and sing-box (.srs) clients. The rules are categorized into logical groups such as services to be proxied, direct connections, and specific application rules like those for AI, academic, or developer tools.
Sources: README.md, Clash/GeneralClashConfig.yml, update_providers.py, update_sing_box.py
Rule Generation and Automation
The repository employs Python scripts to automate the creation of provider files from master .list files. This ensures consistency and simplifies maintenance. Two primary scripts handle the generation for Clash and sing-box.
Clash Provider Generation (update_providers.py)
The update_providers.py script scans the Clash/ directory for .list files (excluding certain folders) and converts them into Clash-compatible .yaml provider files.
The following diagram illustrates the workflow of this script.
This diagram shows how update_providers.py converts .list files into .yaml provider files for Clash.
Key Logic:
- It prepends
payload:to the output file. - It converts standard rule lines into YAML list items (e.g.,
DOMAIN-SUFFIX,google.combecomes- DOMAIN-SUFFIX,google.com). - Lines starting with
#are preserved as comments. - Specific rule types like
USER-AGENT,URL-REGEX, andPROCESS-NAMEare commented out, as they may not be standard in all provider contexts.
Sources: update_providers.py:10-40
sing-box Rule Generation (update_sing_box.py)
A parallel script, update_sing_box.py, generates rule files for the sing-box client. It follows a more structured approach, parsing .list files into Python dataclasses before generating a final compiled binary.
The diagram below shows the workflow for generating sing-box rules.
This diagram illustrates the process of converting .list files into compiled .srs rule files for sing-box.
The script uses two dataclasses, HeadlessRule and Rule, to structure the data before serialization.
This class diagram shows the data structures used in update_sing_box.py to represent rule sets.
Sources: update_sing_box.py:16-52, 70-107
Provider File Structure and Types
The generated .yaml files for Clash follow a simple structure. They contain a single top-level key, payload, which holds a list of rule definitions.
Example Provider File:
# Clash/Providers/Ruleset/AI.yaml
payload:
# 内容:内容:AI平台-国外
# 数量:39条
- DOMAIN-KEYWORD,anthropic
- DOMAIN-KEYWORD,claude
- DOMAIN-KEYWORD,openai
- DOMAIN,ai.google.dev
- DOMAIN-SUFFIX,chatgpt.com
- DOMAIN-SUFFIX,claude.ai
Sources: Clash/Providers/Ruleset/AI.yaml:1-10
The repository uses several types of rules across its provider files.
| Rule Type | Description | Example | Source File |
|---|---|---|---|
DOMAIN-SUFFIX | Matches a domain and all its subdomains. | DOMAIN-SUFFIX,google.com | Clash/Providers/ProxyLite.yaml:454 |
DOMAIN-KEYWORD | Matches if a keyword is found anywhere in the domain name. | DOMAIN-KEYWORD,google | Clash/Providers/ProxyLite.yaml:50 |
DOMAIN | Matches an exact domain name. | DOMAIN,ai.google.dev | Clash/Providers/Ruleset/AI.yaml:7 |
IP-CIDR | Matches an IP address or a range of IP addresses. | IP-CIDR,74.125.0.0/16,no-resolve | Clash/Providers/ProxyLite.yaml:313 |
Rule Categorization
Rules are organized into different provider files based on their purpose. This modular approach allows users to selectively apply rule sets.
Proxy Providers
These files contain rules for traffic that should be routed through a proxy.
ProxyGFWlist.yaml: A comprehensive list containing thousands of rules derived from the GFWList project, intended to bypass internet censorship.ProxyLite.yaml: A smaller, more curated list of domains and keywords that should be proxied. This includes common international services like Google, Facebook, and Twitter.
Sources: Clash/Providers/ProxyGFWlist.yaml, Clash/Providers/ProxyLite.yaml
Thematic Rulesets
Located in Clash/Providers/Ruleset/, these providers group domains for specific use cases.
AI.yaml: For AI services like OpenAI, Gemini, and Claude.Scholar.yaml: For academic websites and research databases like IEEE, Nature, and ACM.Developer.yaml: For developer tools and platforms like Docker, GitLab, and Stack Overflow.Crypto.yaml: For cryptocurrency exchanges and services like Binance, Coinbase, and Ethereum.
Sources: Clash/Providers/Ruleset/AI.yaml, Clash/Providers/Ruleset/Scholar.yaml, Clash/Providers/Ruleset/Developer.yaml, Clash/Providers/Ruleset/Crypto.yaml
UnBan Provider
The UnBan.yaml file serves as a whitelist. It contains rules for domains that might be accidentally blocked by broader ad-blocking or proxy rules but are necessary for certain applications to function correctly. For example, it includes tracking.miui.com and app.adjust.com.
# Clash/Providers/UnBan.yaml
payload:
# Unbreak 白名单 不应该被广告链接的网站
# Miui 小米
- DOMAIN,tracking.miui.com
# General
- DOMAIN,app.adjust.com
Sources: Clash/Providers/UnBan.yaml:1-19
System-Level Bypass Configuration
Beyond rule providers, the GeneralClashConfig.yml file defines a system-level bypass list for Clash for Windows under the cfw-bypass key. This list contains domains and IP ranges that should always bypass the proxy, regardless of the current mode or other rules. It is primarily used for local network addresses and domains of Chinese domestic services to ensure they are accessed directly for optimal performance.
# Clash/GeneralClashConfig.yml
# 系统代理跳过列表 cfw-bypass
# System proxy will ignore these domains(or ipnet)
cfw-bypass:
# 直连列表
# MyList
- "*.13th.tech"
- "*.423down.com"
# CN域名直连(中国|公司|网络)
- "*.cn"
- "*.xn--fiqs8s"
# Alibaba
- "*.1688.com"
- "*.alipay.com"
# Local Network
- localhost
- 127.*
- 192.168.*
- <local>
Sources: Clash/GeneralClashConfig.yml:259-272, 300-301, 715-718
Tooling for Rule Conversion
The repository includes tools in Tool/Surge转acl工具/ that suggest a workflow for converting rules from other formats. The file shortcuts.xml contains Notepad++ macros designed to transform rule formats using regular expressions. For instance, one macro converts Surge's DOMAIN-SUFFIX rules into a format compatible with the .list files used by the generation scripts. This indicates that rules may be sourced from various community projects and standardized before being processed.