|
1 | 1 | # Routing |
2 | 2 |
|
3 | | -The documentation for this page is missing. Please submit a [pull request](https://github.com/v2fly/v2fly-github-io/pulls) or refer to the Chinese documentation. |
| 3 | +V2Ray has a built-in routing module that can distribute inbound data through different outbound connections based on requirements, enabling selective proxy routing. A common use case is to split domestic and international traffic. V2Ray can internally determine traffic from different countries or regions and route them to different outbound proxies. |
| 4 | + |
| 5 | +## RoutingObject |
| 6 | + |
| 7 | +`RoutingObject` corresponds to the `routing` entry in the configuration file. |
| 8 | + |
| 9 | +```json |
| 10 | +{ |
| 11 | + "domainStrategy": "AsIs", |
| 12 | + "domainMatcher": "mph", |
| 13 | + "rules": [], |
| 14 | + "balancers": [] |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +> `domainStrategy`: "AsIs" | "IPIfNonMatch" | "IPOnDemand" |
| 19 | +
|
| 20 | +Domain name resolution strategy: |
| 21 | + |
| 22 | +* `AsIs`: Uses only domain names for routing selection (default value) |
| 23 | +* `IPIfNonMatch`: Resolves domain names to IP addresses (A or AAAA records) for IP-based rule matching when no domain-based rules match |
| 24 | + * When a domain has multiple IP addresses, all addresses are tried until one matches an IP rule |
| 25 | + * The resolved IP is only used for routing selection; the original domain name is still used in forwarded packets |
| 26 | +* `IPOnDemand`: Immediately resolves domain names to IP addresses when encountering any IP-based rules during matching |
| 27 | + |
| 28 | +> `domainMatcher`: "linear" | "mph" |
| 29 | +
|
| 30 | +Selects the domain matching algorithm: |
| 31 | + |
| 32 | +* `linear`: Uses linear matching algorithm (default value) |
| 33 | +* `mph`: Uses minimal perfect hash algorithm (v4.36.1+) |
| 34 | + * With test data of about 170,000 entries, matching speed improves by ~30% and memory usage reduces by ~15% |
| 35 | + |
| 36 | +> `rules`: \[[RuleObject](#ruleobject)\] |
| 37 | +
|
| 38 | +An array where each item is a rule. For each connection, routing will evaluate these rules in sequence. When a rule takes effect, the connection is forwarded to its specified `outboundTag` (or `balancerTag`, V2Ray 4.4+). When no rules match, traffic is forwarded to the first `outbound` by default. |
| 39 | + |
| 40 | +> `balancers`: \[ [BalancerObject](#balancerobject) \] |
| 41 | +
|
| 42 | +(V2Ray 4.4+) An array where each item is a load balancer configuration. When a rule points to a load balancer, V2Ray selects an `outbound` through this load balancer to forward traffic. |
| 43 | + |
| 44 | +## RuleObject |
| 45 | + |
| 46 | +```json |
| 47 | +{ |
| 48 | + "domainMatcher": "mph", |
| 49 | + "type": "field", |
| 50 | + "domains": [ |
| 51 | + "baidu.com", |
| 52 | + "qq.com", |
| 53 | + "geosite:cn", |
| 54 | + "ext:customizedGeoSiteFile.dat:cn" |
| 55 | + ], |
| 56 | + "ip": [ |
| 57 | + "0.0.0.0/8", |
| 58 | + "10.0.0.0/8", |
| 59 | + "fc00::/7", |
| 60 | + "fe80::/10", |
| 61 | + "geoip:cn", |
| 62 | + "geoip:!cn", |
| 63 | + "ext:customizedGeoIPFile.dat:cn", |
| 64 | + "ext:customizedGeoIPFile.dat:!cn", |
| 65 | + "ext-ip:customizedGeoIPFile.dat:cn", |
| 66 | + "ext-ip:customizedGeoIPFile.dat:!cn" |
| 67 | + ], |
| 68 | + "port": "53,443,1000-2000", |
| 69 | + "sourcePort": "53,443,1000-2000", |
| 70 | + "network": "tcp", |
| 71 | + "source": [ |
| 72 | + "10.0.0.1" |
| 73 | + ], |
| 74 | + "user": [ |
| 75 | + |
| 76 | + ], |
| 77 | + "inboundTag": [ |
| 78 | + "tag-vmess" |
| 79 | + ], |
| 80 | + "protocol": [ |
| 81 | + "http", |
| 82 | + "tls", |
| 83 | + "bittorrent" |
| 84 | + ], |
| 85 | + "attrs": "attrs[':method'] == 'GET'", |
| 86 | + "outboundTag": "direct", |
| 87 | + "balancerTag": "balancer" |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +:::tip |
| 92 | +When multiple attributes are specified, they must all be satisfied for the current rule to take effect. This means `domains` and `ip` rules should be used separately. |
| 93 | +::: |
| 94 | + |
| 95 | +> `domainMatcher`: "linear" | "mph" |
| 96 | +
|
| 97 | +Selects the domain matching algorithm. The `domainMatcher` here takes precedence over the one configured in `RoutingObject`. |
| 98 | + |
| 99 | +* `linear`: Uses linear matching algorithm (default value) |
| 100 | +* `mph`: Uses minimal perfect hash algorithm (v4.36.1+) |
| 101 | + * With test data of about 170,000 entries, matching speed improves by ~30% and memory usage reduces by ~15% |
| 102 | + |
| 103 | +> `type`: "field" |
| 104 | +
|
| 105 | +Currently only supports the "field" option. |
| 106 | + |
| 107 | +> `domains`: \[string\] |
| 108 | +
|
| 109 | +An array where each item is a domain match. Several formats are supported: |
| 110 | + |
| 111 | +* **Plain string**: Rule takes effect when this string matches any part of the target domain. For example, `sina.com` matches `sina.com`, `sina.com.cn`, `sina.company`, and `www.sina.com`, but not `sina.cn`. |
| 112 | +* **Regular expression**: Starts with `regexp:`, followed by a regular expression. Rule takes effect when this regex matches the target domain. For example, `regexp:\.goo.*\.com$` matches `www.google.com`, `fonts.googleapis.com`, but not `google.com`. |
| 113 | +* **Subdomain (recommended)**: Starts with `domain:`, followed by a domain name. Rule takes effect when this domain or its subdomains match the target domain. For example, `domain:v2ray.com` matches `www.v2ray.com`, `v2ray.com`, but not `xv2ray.com`. |
| 114 | +* **Full match**: Starts with `full:`, followed by a domain name. Rule takes effect when this domain exactly matches the target domain. For example, `full:v2ray.com` matches `v2ray.com` but not `www.v2ray.com`. |
| 115 | +* **Predefined domain list**: Starts with `geosite:`, followed by a category name, such as `geosite:google` or `geosite:cn`. See [Predefined Domain Lists](#predefined-domain-lists) for details. |
| 116 | +* **Load from file**: Format is `ext:file:tag`, must start with `ext:`, followed by filename and tag. File should be placed in the [resource directory](env.md#resource-file-path), format same as `geosite.dat`, and tag must exist in the file. |
| 117 | + |
| 118 | +> `ip`: \[string\] |
| 119 | +
|
| 120 | +An array where each item represents an IP range. Rule takes effect when any item matches the target IP. Several formats are supported: |
| 121 | + |
| 122 | +* IP: Like `127.0.0.1` |
| 123 | +* [CIDR](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing): Like `10.0.0.0/8` |
| 124 | +* GeoIP: |
| 125 | + * Format `geoip:cn` for positive match, matches "Mainland China IP addresses". Uses two-letter [country/region codes](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). |
| 126 | + * Format `geoip:!cn` for negative match, matches "non-Mainland China IP addresses". |
| 127 | + * Special value: `geoip:private` (V2Ray 3.5+) includes all private addresses like `127.0.0.1` |
| 128 | +* Load from file: |
| 129 | + * Format `ext:file:tag` or `ext-ip:file:tag` for positive match |
| 130 | + * Format `ext:file:!tag` or `ext-ip:file:!tag` for negative match |
| 131 | + * Must start with `ext:` or `ext-ip:`, followed by filename and tag/!tag |
| 132 | + |
| 133 | +:::tip |
| 134 | +`ext:geoip.dat:cn` and `ext-ip:geoip.dat:cn` are equivalent to `geoip:cn` |
| 135 | + |
| 136 | +`ext:geoip.dat:!cn` and `ext-ip:geoip.dat:!cn` are equivalent to `geoip:!cn` |
| 137 | +::: |
| 138 | + |
| 139 | +> `port`: number | string |
| 140 | +
|
| 141 | +Target port range, three formats: |
| 142 | + |
| 143 | +* `a-b`: a and b are positive integers less than 65536. Rule applies to ports in this inclusive range. |
| 144 | +* `a`: a is a positive integer less than 65536. Rule applies when target port equals a. |
| 145 | +* (V2Ray 4.18+) Mix of above formats, separated by commas. Example: `53,443,1000-2000` |
| 146 | + |
| 147 | +> `sourcePort`: number | string |
| 148 | +
|
| 149 | +Source port range, same format as above |
| 150 | + |
| 151 | +> `network`: "tcp" | "udp" | "tcp,udp" |
| 152 | +
|
| 153 | +Available values are "tcp", "udp", or "tcp,udp". Rule takes effect when connection type matches. |
| 154 | + |
| 155 | +> `source`: \[string\] |
| 156 | +
|
| 157 | +An array where each item represents an IP range. Formats include IP, CIDR, GeoIP, and loading from file. Rule takes effect when any item matches the source IP. |
| 158 | + |
| 159 | +> `user`: \[string\] |
| 160 | +
|
| 161 | +An array where each item is an email address. Rule takes effect when any item matches the source user. Currently supported by Shadowsocks and VMess. |
| 162 | + |
| 163 | +> `inboundTag`: \[string\] |
| 164 | +
|
| 165 | +An array where each item is an identifier. Rule takes effect when any item matches the inbound protocol identifier. |
| 166 | + |
| 167 | +> `protocol`: \[ "http" | "tls" | "bittorrent" \] |
| 168 | +
|
| 169 | +An array where each item represents a protocol. Rule takes effect when any protocol matches the current connection's traffic. Requires `sniffing` option enabled in inbound proxy. |
| 170 | + |
| 171 | +> `attrs`: string |
| 172 | +
|
| 173 | +(V2Ray 4.18+) A script to check traffic attributes. Rule takes effect when this script returns true. |
| 174 | + |
| 175 | +Uses [Starlark](https://github.com/bazelbuild/starlark) scripting language, which is a subset of Python. Script receives a global variable `attrs` containing traffic-related attributes. |
| 176 | + |
| 177 | +Currently only HTTP inbound proxy sets these attributes. |
| 178 | + |
| 179 | +Examples: |
| 180 | + |
| 181 | +* Detect HTTP GET: `attrs[':method'] == 'GET'` |
| 182 | +* Detect HTTP Path: `attrs[':path'].startswith('/test')` |
| 183 | +* Detect Content Type: `attrs['accept'].index('text/html') >= 0` |
| 184 | + |
| 185 | +> `outboundTag`: string |
| 186 | +
|
| 187 | +Corresponds to an identifier of an additional [outbound connection configuration](outbounds.md#outboundobject). |
| 188 | + |
| 189 | +> `balancerTag`: string |
| 190 | +
|
| 191 | +Corresponds to a load balancer identifier. Must choose either `balancerTag` or `outboundTag`. When both are specified, `outboundTag` takes effect. |
| 192 | + |
| 193 | +## BalancerObject |
| 194 | + |
| 195 | +Load balancer configuration. When a load balancer takes effect, it selects the most suitable outbound protocol from specified protocols according to configuration. |
| 196 | + |
| 197 | +```json |
| 198 | +{ |
| 199 | + "tag": "balancer", |
| 200 | + "selector": [], |
| 201 | + "strategy": { |
| 202 | + "type": "random" |
| 203 | + } |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +> `tag`: string |
| 208 | +
|
| 209 | +Identifier for this load balancer, used to match `balancerTag` in `RuleObject`. |
| 210 | + |
| 211 | +> `selector`: \[ string \] |
| 212 | +
|
| 213 | +An array of strings, each used for prefix matching with outbound protocol identifiers. For outbound protocol identifiers `[ "a", "ab", "c", "ba" ]`, `"selector": ["a"]` would match `[ "a", "ab" ]`. |
| 214 | + |
| 215 | +> `strategy`: StrategyObject |
| 216 | +
|
| 217 | +The strategy object for load balancing. |
| 218 | + |
| 219 | +## StrategyObject |
| 220 | + |
| 221 | +```json |
| 222 | +{ |
| 223 | + "type": "random" |
| 224 | +} |
| 225 | +``` |
| 226 | + |
| 227 | +> `type`: string |
| 228 | +
|
| 229 | +The type of load balancing strategy. |
| 230 | + |
| 231 | +Available types include `random` and `leastPing` (v4.38.0+). |
| 232 | + |
| 233 | +`random` randomly selects one outbound as the final outbound connection. |
| 234 | + |
| 235 | +`leastPing` selects the outbound connection with the fastest HTTPS GET request completion time based on observation records. |
| 236 | + |
| 237 | +Load balancing strategies depend on observation records from the outbound [connection observatory](observatory.md) component. |
| 238 | + |
| 239 | +## Predefined Domain Lists |
| 240 | + |
| 241 | +This list is maintained by the [domain-list-community](https://github.com/v2fly/domain-list-community) project and is pre-installed in every V2Ray package as `geosite.dat`. This file contains common domains, used as `geosite:listname`, like `geosite:google` for domains in the `google` file in the `data` directory of the `domain-list-community` project. |
| 242 | + |
| 243 | +:::tip |
| 244 | +If you encounter issues or find missing domains while using `geosite.dat`, feel free to submit an [issue](https://github.com/v2fly/domain-list-community/issues) or [pull request](https://github.com/v2fly/domain-list-community/pulls) to [v2fly/domain-list-community](https://github.com/v2fly/domain-list-community). |
| 245 | +::: |
| 246 | + |
| 247 | +Common domain categories include: |
| 248 | + |
| 249 | +* `category-ads`: Common advertising domains |
| 250 | +* `category-ads-all`: Common advertising domains plus ad provider domains |
| 251 | +* `tld-cn`: CNNIC-managed top-level domains for Mainland China (e.g., `.cn`, `.中国`) |
| 252 | +* `tld-!cn`: Non-Mainland China top-level domains (e.g., `.tw` for Taiwan, `.jp` for Japan, `.sg` for Singapore, `.us` for USA, `.ca` for Canada) |
| 253 | +* `geolocation-cn`: Common Mainland China website domains |
| 254 | +* `geolocation-!cn`: Common non-Mainland China website domains |
| 255 | +* `cn`: Combination of `geolocation-cn` and `tld-cn` |
| 256 | +* `apple`: Most Apple domains |
| 257 | +* `google`: Most Google domains |
| 258 | +* `microsoft`: Most Microsoft domains |
| 259 | +* `facebook`: Most Facebook domains |
| 260 | +* `twitter`: Most Twitter domains |
| 261 | +* `telegram`: Most Telegram domains |
| 262 | +* For more categories, see the [data directory](https://github.com/v2fly/domain-list-community/tree/master/data) |
| 263 | + |
| 264 | +## Geodata File Loader |
| 265 | + |
| 266 | +V2Ray includes multiple loaders for reading and decoding `geoip.dat` and `geosite.dat` files (v4.39.0+). |
| 267 | + |
| 268 | +This setting is controlled by the `v2ray.conf.geoloader` environment variable. See [Environment Variables](env.md#geodata-file-loader) for details. |
0 commit comments