Home
Relevant source files
The following files were used as context for generating this wiki page:The Self-Rule repository is a collection of personalized network proxy rules and configurations, primarily designed for use with Clash and sing-box. It provides a structured and automated way to manage extensive rule sets for routing network traffic. The system is built around source .list files which are processed by Python scripts to generate configuration files (.yaml for Clash, .json/.srs for sing-box) for different proxy tools.
The repository includes a general configuration for Clash, a variety of modular rule sets for specific services (like AI, Developer tools, and Microsoft services), and comprehensive proxy lists such as the GFWlist. It also contains tooling, including Python automation scripts and Notepad++ macros, to maintain and convert these rule sets.
Core Configuration (Clash)
The main configuration for Clash is defined in Clash/GeneralClashConfig.yml. This file sets up the fundamental behavior of the Clash client, including ports, LAN access, and logging levels.
Sources: Clash/GeneralClashConfig.yml
Key Parameters
| Parameter | Default Value | Description |
|---|---|---|
mixed-port | 7890 | A single port for both HTTP and SOCKS5 proxy services. |
socks-port | 7891 | A dedicated SOCKS5 proxy port. |
allow-lan | true | Allows connections from other devices on the local network. |
bind-address | "*" | Binds to all IP addresses when allow-lan is true. |
mode | rule | Default mode is rule-based proxying. Other options are global or direct. |
log-level | info | Sets the verbosity of the log output. |
external-controller | 127.0.0.1:9090 | The address for the Clash RESTful API, used for management dashboards. |
cfw-bypass | (list) | A list of domains and IP ranges to bypass the system proxy in Clash for Windows. |
Sources: Clash/GeneralClashConfig.yml:5-28, Clash/GeneralClashConfig.yml:256-821
Clash for Windows (CFW) Specifics
The configuration includes several settings prefixed with cfw- that are specific to the Clash for Windows client.
cfw-latency-url:http://www.gstatic.com/generate_204is used to test proxy latency.cfw-conn-break-strategy: Defines how connections are handled when switching proxies, profiles, or modes.cfw-proxies-order: Sets the default sorting order for proxies in the UI.cfw-bypass: An extensive list of domains and IP networks that should not be routed through the system proxy. This is primarily for ensuring domestic Chinese services connect directly.
Sources: Clash/GeneralClashConfig.yml:148-150, Clash/GeneralClashConfig.yml:181-184, Clash/GeneralClashConfig.yml:195-196, Clash/GeneralClashConfig.yml:256-821
Rule and Provider System
The repository's rules are organized into a provider system. The source of truth is typically a .list file containing domains, IPs, and keywords. These are then converted into .yaml provider files for Clash.
Provider File Structure
Provider files, such as Clash/Providers/ProxyGFWlist.yaml, contain a payload section. This section is a list of rules that determine which traffic should be proxied.
# Clash/Providers/Ruleset/AI.yaml
payload:
# 内容:内容:AI平台-国外
# 数量:39条
- DOMAIN-KEYWORD,anthropic
- DOMAIN-KEYWORD,claude
- DOMAIN-KEYWORD,openai
- DOMAIN,ai.google.dev
- DOMAIN-SUFFIX,anthropic.com
Sources: Clash/Providers/Ruleset/AI.yaml:1-9
Rule Types
The YAML files use several types of rules to match traffic.
| Rule Type | Example | Description |
|---|---|---|
DOMAIN-SUFFIX | github.com | Matches a domain and all its subdomains. |
DOMAIN-KEYWORD | github | Matches any domain containing the specified keyword. |
DOMAIN | ai.google.dev | Matches an exact domain name. |
IP-CIDR | 91.108.0.0/16 | Matches an IP address within the specified CIDR block. |
Sources: Clash/Providers/Ruleset/Github.yaml:5-6, Clash/Providers/Ruleset/AI.yaml:7-9, Clash/Providers/ProxyLite.yaml:473-478
Rule Categories
The rules are modularized into categories located in Clash/Providers/Ruleset/. This allows for easy inclusion or exclusion of rules for specific services.
| Category | File | Description |
|---|---|---|
| AI | AI.yaml | Rules for foreign AI platforms like OpenAI, Claude, and Gemini. |
| Developer | Developer.yaml | Common foreign websites, mirrors, and forums for developers. |
| GitHub | Github.yaml | Rules specifically for GitHub domains. |
| Scholar | Scholar.yaml | Domestic and foreign academic websites. |
| Wikipedia | Wikipedia.yaml | Domains related to Wikipedia and its sister projects. |
| Crypto | Crypto.yaml | Cryptocurrency exchanges, forums, and analysis sites. |
| Marketing | Marketing.yaml | Rules for "思杰马克丁" (Citrix Marketing) related software. |
Sources: Clash/Providers/Ruleset/AI.yaml:2-3, Clash/Providers/Ruleset/Developer.yaml:2-3, Clash/Providers/Ruleset/Github.yaml:2-3, Clash/Providers/Ruleset/Scholar.yaml:2-3, Clash/Providers/Ruleset/Wikipedia.yaml:2-3, Clash/Providers/Ruleset/Crypto.yaml, Clash/Providers/Ruleset/Marketing.yaml:2-3
Automation and Tooling
The repository relies on Python scripts to automate the generation of configuration files from source lists, ensuring consistency and ease of maintenance.
Clash Provider Generation
The update_providers.py script converts .list files into .yaml provider files for Clash.
The diagram below shows the workflow for generating Clash provider files.
This script iterates through directories, reads .list files, and writes formatted YAML files to the Clash/Providers directory. It also handles comments and ignores certain rule types like USER-AGENT by commenting them out.
Sources: update_providers.py
sing-box Rule Generation
The update_sing_box.py script is designed to convert the same .list files into a format compatible with sing-box.
The diagram below shows the workflow for generating and compiling sing-box rule files.
The script defines two main data structures, HeadlessRule and Rule, to represent the sing-box rule format before serializing to JSON.
# update_sing_box.py:14-46
@dataclass
class HeadlessRule:
# query_type: list of int or str
query_type: typing.List[typing.Union[int, str]] = None
domain: list[str] = None
domain_suffix: list[str] = None
domain_keyword: list[str] = None
domain_regex: list[str] = None
source_ip_cidr: list[str] = None
ip_cidr: list[str] = None
# ... other fields
def append(self, key: str, value: str):
if getattr(self, key) is None:
setattr(self, key, [])
getattr(self, key).append(value)
@dataclass
class Rule:
version: int = 2
rules: list[HeadlessRule] = None
Sources: update_sing_box.py
Rule Conversion Utilities
The repository contains tools for manual rule conversion in Tool/Surge转acl工具/. The shortcuts.xml file provides a set of macros for the Notepad++ editor to convert rules between different formats, such as Surge and ACL.
For example, the "网址转acl" (URL to ACL) macro performs a series of find-and-replace operations using regular expressions to transform a list of domains into the ACL format.
<!-- Tool/Surge转acl工具/shortcuts.xml:4-18 -->
<Macro name="网址转acl" Ctrl="no" Alt="no" Shift="no" Key="0">
...
<Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1601" wParam="0" lParam="0" sParam="^((DOMAIN-SUFFIX|DOMAIN)\,)|((.*)?http\w?\:\#(www\\\.)?)" />
<Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
<Action type="3" message="1602" wParam="0" lParam="0" sParam="\(^|\\\.\)" />
<Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
<Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
<Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1601" wParam="0" lParam="0" sParam="(\,\s?.{0,3}(Proxy|DIRECT|REJECT)(.*)?$)|(\/.*$)" />
<Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
<Action type="3" message="1602" wParam="0" lParam="0" sParam="$" />
...
</Macro>
Sources: Tool/Surge转acl工具/shortcuts.xml:4-29
Project History
The file Tool/New.md serves as a changelog for the repository, documenting updates to the rule sets over time. Entries are dated and describe changes such as adding new rules, removing duplicates, and enhancing filtering for specific applications or ad networks.
Example Update:
3.16
去重
合并中国IP段
完善百度、letv、金立手机
添加UC浏览器广告过滤
添加墨迹天气,增强sohu、sougou等
添加大量广告联盟,屏蔽效果更好
添加爱奇艺广告ip段
添加起点 掌阅 书旗 app广告
This history indicates a long-term effort to refine and expand the rule lists for better ad-blocking and traffic routing.
Sources: Tool/New.md:4-14