← Back to Home

Xray routing rules from any website: Xray-core, 3X-UI, v2rayN

Updated · NetRoute version 4.1.4

Xray directs connections by routing.rules: lists of domains and addresses plus the tag of an outbound. NetRoute builds such rules from a website in two variants: a fragment of an Xray-core config — which also fits the 3X-UI panel, where the Xray config is edited as JSON — and a file for importing rules into v2rayN.

What is inside the file

The “Xray / 3X-UI / Streisand” format is a fragment of an Xray-core config with a routing section. Domains and networks are two rules, each with its own list and a shared outboundTag:

{
  "routing": {
    "domainStrategy": "IPIfNonMatch",
    "rules": [
      { "type": "field", "outboundTag": "proxy", "domain": ["domain:github.com", "domain:githubusercontent.com"] },
      { "type": "field", "outboundTag": "proxy", "ip": ["140.82.112.0/20", "185.199.108.0/22", "2606:50c0::/32"] }
    ]
  }
}

The “v2rayN (rules import)” format is an array of rules in the shape v2rayN imports from the clipboard or a file (as in the v2rayCustomRoutingList repository of the v2rayN author):

[
  { "port": "", "outboundTag": "proxy", "domain": ["domain:github.com", "domain:githubusercontent.com"], "enabled": true, "remarks": "NetRoute github.com domains" },
  { "port": "", "outboundTag": "proxy", "ip": ["140.82.112.0/20", "185.199.108.0/22", "2606:50c0::/32"], "enabled": true, "remarks": "NetRoute github.com networks" }
]
NetRoute popup after scanning github.com: the selected format and the result on the Routes tab NetRoute popup after scanning github.com: the selected format and the result on the Routes tab
The NetRoute popup with the “Xray / 3X-UI / Streisand” format: the Device field holds the proxy tag, the Routes tab a routing.rules fragment. Scan data: a github.com page.

What you need

Step 1. Generate the rules in NetRoute

  1. Open the website in Chrome and reload the page so the extension’s sniffer sees the background requests
  2. Start NetRoute
  3. Under Target OS, “Proxy clients” group, pick Xray / 3X-UI / Streisand for an Xray-core config or v2rayN (rules import) for importing into v2rayN
  4. In the Device field enter the tag of your outbound — the placeholder shows the default proxy. The Gateway IP field does not affect these formats
  5. Click Analyze website
  6. On the Domains tab untick what you do not need — the rules update at once
  7. Click Download: the file is named after the site, for example github_com_rules.json

Step 2a. Xray-core: insert the rules into the config

Open Xray’s config.json and add both objects from rules to the routing.rules array above the general rules. Per the Xray documentation rules are checked top to bottom and the first match wins; a connection that matched no rule leaves through the first outbound of the config. Make sure outboundTag equals the tag of your proxy outbound and that domainStrategy is set once in the routing section.

Restart Xray.

Step 2b. 3X-UI: rules in the Xray settings

3X-UI is a web panel for Xray-core servers. Per its documentation the routing rules are Xray rules with the domain, ip, port, network, protocol, inboundTag, sourceIP matchers and an outboundTag or balancerTag target; there is no separate per-rule editor — you edit the JSON of the config under Xray Settings, and the panel reloads Xray by itself.

  1. Open Xray Settings in the panel
  2. Find the routing.rules array in the JSON and insert both objects from the NetRoute file at its beginning
  3. Put the tag of the wanted outbound into outboundTag — for example a WARP connection or another server, if these sites should go that way
  4. Save — the panel reloads Xray

On a server the rule means: the clients’ traffic to these domains and networks is sent by the server to the given outbound, the rest as configured further down the rule list.

Step 2c. v2rayN: importing the rules

For v2rayN pick the v2rayN (rules import) format in NetRoute. The v2rayN interface strings to follow:

  1. Open the downloaded file and copy its contents to the clipboard
  2. In v2rayN open Routing → Routing Setting
  3. Choose Import Rules From Clipboard (there are also Import Rules From File and Import Rules From Subscription URL)
  4. To the question “Do you want to append rules? Choose yes to append, no to replace.” answer Yes to add the rules to the existing ones
  5. Check that the rule set they landed in is marked active (Set as active rule)

In v2rayN’s rule editor the same lists appear in the Domain and IP or IP CIDR fields; the entry syntax is Xray’s, and the v2rayN wiki points to the Xray documentation for it.

Step 3. Check

Open the site — it should go through the outbound with the tag from the rule, other sites as before. If the site does not open or bypasses the proxy, go through the common problems.

Common problems

The rule does not fire

Rules are checked top to bottom and the first match wins — if a rule above already takes this traffic (for example a general geosite rule), the NetRoute rule is never reached. Move the NetRoute rules up.

Xray does not start after the edit

Check the JSON: a stray comma or a second routing section breaks the config. Only the objects from the rules array go in, not the whole file.

Tag not found

outboundTag has to equal the tag of an existing outbound literally. In NetRoute the tag is set in the Device field before the scan.

Streisand

Streisand is a client for iOS, macOS and visionOS with rule-based proxy setup (“Rule-based Proxy Setup” in its App Store description). How to add your own rules to it is described in the app’s documentation; the domain and network lists from the NetRoute file fit that purpose.

Several sites in one file

Click URL list next to the analyze button and paste the list of sites — NetRoute builds one set of rules for all of them, the file is named bulk_N_rules.json.

DNS and leaks

A domain rule in Xray is matched against the destination name; how and where names are resolved is decided by domainStrategy and the dns section of your config — the NetRoute rules do not change them. The browser’s queries go wherever its own DNS is configured: in Chrome turn off “Use secure DNS” or point it at a resolver you trust, otherwise Chrome resolves names past the client.

Check: dnsleaktest.com with the client on should not show the ISP’s resolver.

Example Configuration File

Ready-to-edit template. Replace the example domains and networks with output from NetRoute for your target sites.



Hint: the text formats download with a comment header. Untick “Include comments in exported files” in the NetRoute options to get the rules alone; the JSON formats never carry comments.

All example configs on GitHub →

Official Documentation

Ready to try?

NetRoute — a free Chrome extension to generate routes from any website.

Install Extension