7. Routing, Rules, and Policies#


The routing system decides "which outbound a connection ultimately uses." Link1 evaluates rules in order and returns the result from the first matching rule.

Minimal Rule#

rules:
  - MATCH,DIRECT

Meaning: all connections go direct.

Common proxy configuration:

rules:
  - DOMAIN-SUFFIX,google.com,PROXY
  - GEOIP,CN,DIRECT
  - MATCH,PROXY

Meaning:

  1. *.google.com uses PROXY.
  1. If the destination IP belongs to CN, use DIRECT.
  1. All other connections use PROXY.

Rule Format#

Regular rules:

TYPE,ARGUMENT,ACTION[,OPTIONS]

MATCH:

MATCH,ACTION

Logical rules:

AND,((rule1),(rule2)),ACTION
OR,((rule1),(rule2)),ACTION
NOT,((rule1)),ACTION

Sub-rule jump:

SUB-RULE,(condition expression),sub-rule name

Commas and Quotes#

Rule lines use commas to separate fields. In general, node names, policy group names, RULE-SET names, and SUB-RULE names should not contain commas. If they do contain commas, wrap the corresponding field in double quotes:

rules:
  - DOMAIN-SUFFIX,corp.example,"Corp, VPN"
  - RULE-SET,"$Corp, VPN","Corp, VPN"
  - MATCH,"Fallback, Group"

Inside double quotes, use a backslash to escape " and \. Single quotes can also wrap fields; inside single quotes, use two single quotes to represent one literal single quote. Quotes only affect comma splitting and removal of the surrounding quotes; they do not change field semantics. The action must still match an actual node name or policy group name.

What Is an Action?#

An action is the target that handles a rule match. It can be:

If the action does not exist, configuration compilation fails.

Complete Rule Type List#

Domain Rules#

TypeArgumentMeaningExample
DOMAINDomainExact domain matchDOMAIN,example.com,DIRECT
DOMAIN-SUFFIXSuffixMatch domain suffixDOMAIN-SUFFIX,google.com,PROXY
DOMAIN-KEYWORDKeywordDomain contains keywordDOMAIN-KEYWORD,google,PROXY
DOMAIN-WILDCARDWildcardMatch using * wildcardDOMAIN-WILDCARD,*.example.com,PROXY
DOMAIN-REGEXRegexMatch domain by regexDOMAIN-REGEX,^api\..*,PROXY
GEOSITECategoryMatch by GeoSite categoryGEOSITE,cn,DIRECT

Practical impact: these rules require Link1 to know the domain name. Explicit proxies can usually obtain the domain; TUN/transparent proxying depends on DNS/Fake-IP/Sniffer.

IP, GeoIP, and ASN Rules#

TypeArgumentMeaningExample
IP-CIDRCIDRDestination IP belongs to a subnetIP-CIDR,10.0.0.0/8,DIRECT,no-resolve
IP-CIDR6IPv6 CIDRDestination IPv6 belongs to a subnetIP-CIDR6,fc00::/7,DIRECT,no-resolve
IP-SUFFIXIP suffixMatch destination IP suffixIP-SUFFIX,1.2,DIRECT
IP-ASNASNMatch destination IP ASNIP-ASN,13335,PROXY
GEOIPCountry/region codeMatch destination IP GeoIPGEOIP,CN,DIRECT
SRC-GEOIPCountry/region codeMatch source IP GeoIPSRC-GEOIP,CN,DIRECT
SRC-IP-ASNASNMatch source IP ASNSRC-IP-ASN,45102,DIRECT
SRC-IP-CIDRCIDRSource IP belongs to a subnetSRC-IP-CIDR,192.168.9.0/24,DIRECT
SRC-IP-SUFFIXIP suffixMatch source IP suffixSRC-IP-SUFFIX,9.10,DIRECT

Effect of no-resolve:

Port, Inbound, and Network Rules#

TypeArgumentMeaningExample
DST-PORTPort/rangeMatch destination portDST-PORT,443,PROXY
SRC-PORTPort/rangeMatch source portSRC-PORT,50000-60000,DIRECT
IN-PORTPort/rangeMatch inbound portIN-PORT,7890,PROXY
IN-TYPEType, separated by /Match inbound typeIN-TYPE,tun/HTTP,PROXY
IN-USERUser, separated by /Match inbound authenticated userIN-USER,alice,PROXY
IN-NAMEInbound name, separated by /Match listener/TUN and other entry namesIN-NAME,hy2-in,PROXY
NETWORKtcp or udpMatch network typeNETWORK,udp,PROXY
DSCP0-63 or rangeMatch DSCP valueDSCP,46,PROXY

Common port range formats: 80, 443, 10000-20000. In TUN selectors, port ranges use start:end; in rules, use the range format supported by the rule parser.

Process and UID Rules#

TypeArgumentMeaningExample
PROCESS-PATHPathExact process path matchPROCESS-PATH,/usr/bin/curl,DIRECT
PROCESS-PATH-WILDCARDWildcardWildcard process path matchPROCESS-PATH-WILDCARD,*/Chrome,PROXY
PROCESS-PATH-REGEXRegexRegex process path matchPROCESS-PATH-REGEX,.*/Chrome.*,PROXY
PROCESS-NAMENameExact process name matchPROCESS-NAME,curl,DIRECT
PROCESS-NAME-WILDCARDWildcardWildcard process name matchPROCESS-NAME-WILDCARD,Google*,PROXY
PROCESS-NAME-REGEXRegexRegex process name matchPROCESS-NAME-REGEX,.*Chrome.*,PROXY
UIDUID or rangeLinux/Android UID matchUID,1000-1002,PROXY

Practical impact:

Rule Sets and Logical Rules#

TypeArgumentMeaningExample
RULE-SETRule set nameReference rule-providers or rule-setsRULE-SET,private,DIRECT
ANDRule expressionAll conditions must be metAND,((DOMAIN-SUFFIX,example.com),(DST-PORT,443)),PROXY
ORRule expressionAny condition is metOR,((DOMAIN,a.com),(DOMAIN,b.com)),PROXY
NOTRule expressionCondition is not metNOT,((GEOIP,CN)),PROXY
SUB-RULECondition + sub-rule nameJump to a sub-rule when the condition is metSUB-RULE,(DST-PORT,443),tls-flow
MATCHNoneFallbackMATCH,DIRECT

MATCH and SUB-RULE cannot be used as inner matchers in logical rules.

Sub-rules#

Sub-rules let you extract part of the rule flow:

sub-rules:
  tls-flow:
    - DOMAIN-SUFFIX,openai.com,AI
    - DOMAIN-SUFFIX,github.com,PROXY
    - MATCH,DIRECT

rules:
  - SUB-RULE,(DST-PORT,443),tls-flow
  - MATCH,DIRECT

Practical impact:

rule-providers#

Rule providers load large numbers of rules from HTTP, files, or inline payloads.

rule-providers:
  private:
    type: http
    behavior: domain
    format: text
    url: https://example.com/private.txt
    path: ./rules/private.txt
    interval: 86400
    proxy: DIRECT
    size-limit: 1048576

rules:
  - RULE-SET,private,DIRECT

Field effects:

FieldMeaningPractical impact
typehttp, file, inlineDetermines the rule source
behaviordomain, ipcidr, classicalDetermines how the payload is interpreted and optimized
formatyaml, text, mrsDetermines the file format
urlHTTP URLUsed when type=http
pathLocal path/cache pathUsed for http cache or file reading
intervalRefresh interval, in secondsControls automatic refresh frequency
proxyOutbound used to download the providerPrevents rule subscriptions from being blocked by the network environment
headerHTTP request headersUsed for authentication or User-Agent
payloadInline rulesUsed when type=inline
size-limitDownload size limitPrevents abnormally large responses from consuming memory/disk

Choosing behavior#

behaviorPayload contentSuitable for
domainDomains, suffixes, keywords, etc.Domain-based routing
ipcidrCIDR listsIP-based routing, TUN route sets
classicalFull rule syntaxReusing complex rule lists

rule-sets#

rule-sets are parsed/inline rule set configurations, usually used for runtime or advanced configuration. Users should generally prefer rule-providers.

Recommended Rule Order#

Recommended order: from specific to general.

rules:
  # 1. Localhost, LAN, and management plane
  - IP-CIDR,127.0.0.0/8,DIRECT,no-resolve
  - IP-CIDR,192.168.0.0/16,DIRECT,no-resolve
  - DOMAIN-SUFFIX,lan,DIRECT

  # 2. Explicit business domains
  - DOMAIN-SUFFIX,openai.com,AI
  - DOMAIN-SUFFIX,github.com,PROXY

  # 3. Large rule sets
  - RULE-SET,reject,REJECT
  - RULE-SET,private,DIRECT
  - RULE-SET,china,DIRECT
  - RULE-SET,global,PROXY

  # 4. IP geo rules
  - GEOIP,CN,DIRECT

  # 5. Final fallback
  - MATCH,PROXY

Testing Rules with Link1 App#

The rule testing feature in Link1 App can construct a "virtual connection." It does not actually access the network; it only checks how rules would match. Common inputs include:

InputExampleAffects which rules
Destination domainchat.example.comDOMAIN, DOMAIN-SUFFIX, GEOSITE
Destination IP1.1.1.1IP-CIDR, GEOIP, IP-ASN
Port443DST-PORT
Network typetcp / udpNETWORK
Inbound typemixed / tunIN-TYPE, IN-NAME, IN-PORT
Source IP192.168.9.10SRC-IP-CIDR, SRC-GEOIP

Focus on three things in the test result:

If the rule test is correct but the real connection is not, the real connection metadata differs from the test input. The most common reason is that under transparent proxying/TUN, the real connection only has an IP and no domain.