Skip to content

dns — DNS ​

The built-in DNS does two jobs:

  • answers clients: queries intercepted by the rule to: hijack-dns, and queries to its own listeners dns.listen;
  • resolves names for the proxy itself: the addresses of outbound servers, destination domains for direct, do: resolve in rules, addresses for probes and downloads.
yaml
outbounds:
  direct:
    direct:
  proxy:
    trojan:
      server: proxy.example.com:443
      password: secret
      tls:
        sni: proxy.example.com

dns:
  upstreams:
    router:
      udp: 192.168.1.1
    remote:
      https: 1.1.1.1
      via: proxy
  rules:
    - match:
        outbound: "*"
      to:
        upstream: router
    - match:
        domain_suffix: lan
      to:
        upstream: router
  final:
    upstream: remote
  cache:
    capacity: 4096
    optimistic: true

route:
  rules:
    - match:
        protocol: dns
      to: hijack-dns
  final:
    outbound: proxy

The first rule sets the resolver for outbound servers, see Resolvers for outbounds.

Sections ​

  • Upstreams — whom to ask: UDP, TCP, DoT, DoH, DoQ, DoH3, the system resolver, hosts, mDNS; via and bootstrap.
  • Rules — which upstream answers which query; resolvers for outbounds.
  • Fakeip and listeners — the pool of fake addresses and own DNS inbounds listen.

Section fields ​

FieldMeaning
upstreamsUpstreams by name
bootstrapThe upstream for the names of the upstreams themselves, see Upstreams
rulesRules
finalupstream: <name> — who answers if no to rule matched. Required if there are upstreams
fakeipThe fakeip pool
listenAddresses of own DNS listeners
cacheThe answer cache, see below
strategyWhich addresses to return when resolving for the proxy: prefer_ipv4, prefer_ipv6, ipv4_only, ipv6_only
timeoutThe timeout of a query to an upstream; 10s by default
client_subnetThe EDNS Client Subnet for all queries: 203.0.113.0/24 or an address
reverse_mappingRemember which domain gave which address, so that connections to that address get the domain for the route rules

Cache: cache ​

FieldMeaningDefault
capacityHow many answers to keep; never less than 1024 (check warns)1024
optimisticAnswer with a stale record and refresh it in the background: true or a block {enabled: true, timeout: 1d} (how long a stale record may still be served; 3d by default). Needs the cache: with disabled or disable_expire it is a load erroroff
disabledDo not cache at allfalse
disable_expireIgnore the TTL: records live until they are evictedfalse

Each upstream has its own cache: the same domain may have different answers from different upstreams. The sing-box fields at the section level (cache_capacity, disable_cache, disable_expire, optimistic, independent_cache) are an error pointing to the cache field (dns: `cache_capacity` is `cache.capacity` ). Answers can be kept across restarts with cache.store: [dns] (cache).

Without a dns section ​

Without the section (or without upstreams) all queries — both for the proxy and those intercepted by hijack-dns — are answered by the system resolver, like the local upstream.

Checking ​

ours explain --dns example.com --type AAAA -c config.yaml shows which DNS rules would match (Command line).

GPL-3.0-or-later