Skip to content

DNS rules ​

dns.rules choose which upstream answers a query. The structure is the same as for the route rules: match and exactly one of do or to, logic through any, all, not. A rule without match always matches. If no to matched, dns.final answers.

yaml
rule_sets:
  ru:
    inline:
      - domain_suffix: [ru, su]

dns:
  upstreams:
    router:
      udp: 192.168.1.1
    remote:
      https: 1.1.1.1
      via: proxy
  rules:
    - match:
        query_type: HTTPS
      to: block
    - match:
        any:
          - domain_suffix: lan
          - rule_set: ru
      to:
        upstream: router
  final:
    upstream: remote

Where queries come from ​

  • Client queries — from connections sent to to: hijack-dns, and from the own listeners dns.listen.
  • Internal queries — the proxy itself resolves a name: an outbound's server, a destination domain, addresses for group probes and for downloads of rule-sets and subscriptions.

Both kinds go through one list of rules. Conditions on the inbound and the source (inbound, source_*) match only client queries, and the outbound condition only internal ones.

A do: resolve query is matched like a client query: it has the connection's inbound and source, and no outbound.

Conditions ​

All the route rule conditions except client, and also:

ConditionMatches if
query_typeThe query type: A, AAAA, HTTPS, … or a number
query_client_subnetThe query carries an EDNS Client Subnet from these subnets
query_dnssectrue: the query asks for DNSSEC
outboundThe internal query is made for this outbound; a glob is allowed, * is any outbound

Domain conditions check the name being asked. ip_cidr and ip_is_private check the addresses in the answer: such a rule applies only to A and AAAA queries, and if the answer does not match, the next rule is tried. Port and source conditions check the connection the query came with.

The other answer conditions (match_response, response_rcode, response_answer, response_ns, response_extra) do not exist here: a rule chooses the upstream before the answer, and such a condition is a load error.

outbound takes the names of outbounds, not groups: a group resolves nothing by itself. For an outbound inside a group, the condition sees the name of that outbound.

Actions ​

EntryMeaning
to: {upstream: <name>}Answer with this upstream
to: {upstream: <name>, skip_adblock: true}The same, without the adblock filter
to: blockA REFUSED answer
to: {block: drop}Do not answer
do: fakeipMark the query: the client gets an address from the pool, see Fakeip
do: {options: {…}}Query parameters; processing continues

The options parameters:

FieldMeaning
strategyprefer_ipv4, prefer_ipv6, ipv4_only, ipv6_only
timeoutThe timeout
cachefalse — do not take from the cache; {optimistic: false} — do not answer with stale records
rewrite_ttlReplace the answer's TTL
client_subnetAdd an EDNS Client Subnet
remove_client_subnetRemove the client's EDNS Client Subnet

As in route, do: options immediately before a to with the same match applies only to that to.

yaml
dns:
  upstreams:
    router:
      udp: 192.168.1.1
    remote:
      https: 1.1.1.1
  rules:
    - match:
        domain_suffix: example.org
      do:
        options:
          strategy: ipv4_only
          rewrite_ttl: 300
    - match:
        domain_suffix: example.org
      to:
        upstream: router
  final:
    upstream: remote

Resolvers for outbounds ​

How an outbound resolves the name of its server is set by rules at the start of the list of the form match: {outbound: <name>} + to: {upstream: …} (optionally with do: options before them):

yaml
outbounds:
  direct:
    direct:
  wg-kernel:
    direct:
    dial:
      interface: wg0
  proxy:
    trojan:
      server: proxy.example.com:443
      password: secret
      tls:
        sni: proxy.example.com

dns:
  upstreams:
    router:
      udp: 192.168.1.1
    wg-dns:
      udp: 10.8.0.1
      via: wg-kernel
    remote:
      https: 1.1.1.1
      via: proxy
  rules:
    - match:
        outbound: wg-kernel
      to:
        upstream: wg-dns
    - match:
        outbound: "*"
      to:
        upstream: router
    - match:
        domain_suffix: corp.example
      to:
        upstream: wg-dns
  final:
    upstream: remote

route:
  final:
    outbound: proxy
  • Each such leading rule with an exact outbound name makes the upstream the resolver of that outbound. outbound: "*" is the resolver of all the other outbounds. The queries of these outbounds go straight to the upstream, and the other rules do not see them.
  • The leading part ends at the first rule of another form, at a repeated name, or after *. A group name there is an error.
  • A rule with outbound further down the list is an ordinary rule: an internal query reaches it in order. Loading it logs outbound DNS rule item is deprecated, use `domain_resolver` instead, a warning inherited from sing-box; ignore it.
  • An outbound without a resolver resolves the name through the whole list of rules with outbound equal to its name, and final.

Internal queries ignore the fakeip mark.

If the resolver's upstream itself goes through the same outbound (via), the query cannot complete: make sure the resolver of a proxy server goes directly or through another outbound.

GPL-3.0-or-later