Core Concepts: Traffic Splitting and Policies
Relevant source files
The following files were used as context for generating this wiki page:This project provides a set of self-use traffic splitting rules for network proxy clients like Clash. It is based on the rules from ACL4SSR. The primary purpose is to intelligently route network traffic based on predefined rules, sending specific requests through a proxy, directly to the destination, or blocking them entirely. This is achieved through a collection of YAML configuration files that define rule sets and policies.
The system uses a rule-based mode where each outgoing connection is matched against a list of rules. These rules can be based on domain names, IP addresses, or other connection attributes. The repository also includes Python scripts to automate the conversion of simple .list files into the more structured .yaml format required by Clash providers and the JSON format for sing-box.
Core Clash Configuration
The main behavior of the Clash client is controlled by Clash/GeneralClashConfig.yml. This file sets up listening ports, logging, and the core routing mode.
Sources: [Clash/GeneralClashConfig.yml](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/GeneralClashConfig.yml)
Key Parameters
| Parameter | Value | Description | Source |
|---|---|---|---|
mixed-port | 7890 | A single port for both HTTP and SOCKS5 proxy connections. | [Clash/GeneralClashConfig.yml:5](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/GeneralClashConfig.yml#L5) |
allow-lan | true | Allows other devices on the local network to connect to the proxy. | [Clash/GeneralClashConfig.yml:14](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/GeneralClashConfig.yml#L14) |
mode | rule | Sets the proxy mode to rule, which means traffic is routed based on the defined rules. Other options are global and direct. | [Clash/GeneralClashConfig.yml:22](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/GeneralClashConfig.yml#L22) |
log-level | info | Sets the verbosity of the log output. | [Clash/GeneralClashConfig.yml:26](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/GeneralClashConfig.yml#L26) |
external-controller | 127.0.0.1:9090 | Exposes a RESTful API for controlling the Clash client, which can be used by external UIs like clash-dashboard. | [Clash/GeneralClashConfig.yml:29](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/GeneralClashConfig.yml#L29) |
cfw-bypass | [...] | A list of domains and IP ranges that should bypass the system proxy when using Clash for Windows. This is a client-specific feature. | [Clash/GeneralClashConfig.yml:281-1051](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/GeneralClashConfig.yml#L281-L1051) |
System Proxy Bypass (cfw-bypass)
A significant part of the configuration is dedicated to the cfw-bypass list. This feature is specific to Clash for Windows and defines which domains and IP networks should not be routed through the proxy when "System Proxy" is enabled. The list is extensive and primarily includes domains for Chinese domestic services, ensuring they are accessed directly for optimal performance and compatibility.
This diagram illustrates the bypass logic for Clash for Windows.
Sources: [Clash/GeneralClashConfig.yml:281-1051](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/GeneralClashConfig.yml#L281-L1051)
Example of cfw-bypass entries:
# Clash/GeneralClashConfig.yml:327-333
cfw-bypass:
# ...
# Alibaba
- "*.1688.com"
- "*.aliapp.org"
- "*.alibaba.com"
# ...
This ensures that requests to Alibaba's services, for example, will bypass the system proxy and connect directly.
Rule Providers and Syntax
The traffic splitting logic is primarily defined in rule provider files located under Clash/Providers/. These are YAML files containing a payload of rules. Each rule specifies a condition and, implicitly, the action to be taken by the proxy group that uses it (e.g., route through PROXY).
Rule Structure
The provider files use a simple list format under the payload key.
# Clash/Providers/Ruleset/Wikipedia.yaml:1-5
payload:
# 内容:Wikipedia 维基相关域名
# 数量:12条
- DOMAIN-SUFFIX,mediawiki.org
- DOMAIN-SUFFIX,wikibooks.org
Sources: [Clash/Providers/Ruleset/Wikipedia.yaml:1-5](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/Providers/Ruleset/Wikipedia.yaml#L1-L5)
Common Rule Types
The rules are categorized into different types to match various traffic patterns.
| Rule Type | Example | Description | Source - |
| DOMAIN-SUFFIX | google.com | Matches google.com and all its subdomains (e.g., www.google.com). | [Clash/Providers/ProxyGFWlist.yaml:10](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/Providers/ProxyGFWlist.yaml#L10) - |
| DOMAIN-KEYWORD | google | Matches any domain containing the keyword "google". | [Clash/Providers/ProxyGFWlist.yaml:38](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/Providers/ProxyGFWlist.yaml#L38) - |
| IP-CIDR | 74.125.0.0/16,no-resolve | Matches any IP address within the specified CIDR block. The no-resolve option prevents DNS lookups for domains that resolve to this range. | [Clash/Providers/ProxyGFWlist.yaml:2860](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/Providers/ProxyGFWlist.yaml#L2860) - |
| DOMAIN | cloud.oracle.com | Matches the exact domain name. | [Clash/Providers/ProxyGFWlist.yaml:2898](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/Providers/ProxyGFWlist.yaml#L2898) - |
Example Rule Sets
The repository contains many specialized rule sets.
- ZJU.yaml: Contains rules for services specific to Zhejiang University, including the main university domain, community sites like CC98, and IP-authenticated academic resources.
Sources: [Clash/Providers/ZJU.yaml](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/Providers/ZJU.yaml) - Microsoft.yaml: A comprehensive list of domains and keywords related to Microsoft services, including Office 365, Azure, OneDrive, and Skype.
Sources: [Clash/Providers/Microsoft.yaml](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/Providers/Microsoft.yaml) - ProxyGFWlist.yaml: A very large list of domains, keywords, and IP ranges that are commonly blocked and should be routed through a proxy. It also includes specific rules for popular services like Google, Facebook, and Telegram.
Sources: [Clash/Providers/ProxyGFWlist.yaml](https://github.com/happy-endless/Self-Rule/blob/HEAD/Clash/Providers/ProxyGFWlist.yaml)
Automated Rule Generation
The repository includes Python scripts to automate the creation and maintenance of rule files, ensuring consistency and ease of management.
Clash Provider Generation (update_providers.py)
This script converts simple .list files into the .yaml format required for Clash rule providers. It walks through the Clash directory, finds all .list files, and generates a corresponding .yaml file in Clash/Providers/.
This flowchart shows the process of converting .list files to .yaml provider files.
Sources: [update_providers.py](https://github.com/happy-endless/Self-Rule/blob/HEAD/update_providers.py)
The script correctly formats rules, comments, and handles special cases like USER-AGENT or URL-REGEX by commenting them out, as they are not standard Clash rule types.
Sources: [update_providers.py:27-35](https://github.com/happy-endless/Self-Rule/blob/HEAD/update_providers.py#L27-L35)
Sing-box Rule Generation (update_sing_box.py)
This script performs a more complex conversion, transforming .list files into a JSON format compatible with sing-box, and then compiles them into the binary .srs format.
Data Structures
The script defines two main dataclasses to structure the rules for JSON serialization.
This class diagram shows the structure of Rule and HeadlessRule used for sing-box configuration.
Sources: [update_sing_box.py:14-53](https://github.com/happy-endless/Self-Rule/blob/HEAD/update_sing_box.py#L14-L53)
Conversion Process
The script parses each line of a .list file, identifies the rule type (e.g., DOMAIN-SUFFIX, IP-CIDR), and appends the value to the corresponding attribute in a HeadlessRule object. Finally, it serializes the object to JSON, calls the sing-box command-line tool to compile it, and cleans up the intermediate JSON file.
This flowchart outlines the conversion from .list files to compiled sing-box .srs rules.
Sources: [update_sing_box.py:59-114](https://github.com/happy-endless/Self-Rule/blob/HEAD/update_sing_box.py#L59-L114)
Conclusion
The traffic splitting and policy system in this repository is a well-structured, rule-based routing mechanism for Clash and sing-box. It leverages a modular design with dozens of categorized rule provider files, making it easy to manage and customize. The core logic is defined in standard Clash YAML configuration, while the inclusion of Python automation scripts for converting and compiling rule lists demonstrates a sophisticated approach to maintaining the rule sets. This ensures that traffic is efficiently and correctly routed based on its destination, whether it's a domestic service, a university resource, or a site that requires a proxy.