dns

package
v0.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2026 License: AGPL-3.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dns is the panel's DNS preset module: built-in and custom presets, the DnsService admin API, and the rule that picks the preset that applies to a user (user, then group, then the instance default, then the built-in default). Turning a preset into what a client understands lives next to it: the provider catalog (providers.go), Resolve (transport.go: which endpoints a given client gets for the preset's preferred transport), and HappRouting for Happ (happ.go); the Mihomo and AmneziaWG renderers only add callers of Effective and Preset.EndpointsFor.

The instance default is stored here (setting "dns.default_preset"); SubscriptionService reads and writes SubscriptionSettings.default_dns_preset_id through DefaultPresetID / SetDefaultPresetID so there is one source of truth.

Index

Constants

View Source
const (
	MaxServers         = 16  // main resolvers, and the servers of one split rule
	MaxSplitRules      = 64  // split rules per preset
	MaxSuffixesPerRule = 256 // domain suffixes in one split rule

)

Limits of a preset.

View Source
const CatalogVersion = "2026-09-30"

The provider catalog: well-known public resolvers with their plain, DoT and DoH endpoints. It ships with the panel and changes with releases; a preset server may point at a variant ("cloudflare/family") instead of carrying bare addresses, so a corrected address reaches every preset on the next upgrade.

CatalogVersion is the date every endpoint below was last checked against the provider's own documentation.

View Source
const HappProfileName = "DNS"

HappProfileName is the Name of the generated routing profile.

Variables

Clients lists the clients in display order.

View Source
var ErrUnknownPreset = errors.New("dns: unknown preset")

ErrUnknownPreset is returned when a preset id does not exist.

Functions

func Clean

func Clean(name, description string, servers []Server, split []SplitRule) (n, d string, srv []Server, sp []SplitRule, err error)

Clean validates and normalises the editable fields of a preset.

func HappRouting

func HappRouting(p Preset) (string, error)

HappRouting returns the value of the `routing` header for a preset; its split becomes direct domains only when the preset's SplitDirect is on. An error means Happ cannot express any main server of the preset: the caller leaves the header out.

func HappRoutingNotes

func HappRoutingNotes(p Preset) []string

HappRoutingNotes lists, in English, which parts of the preset the Happ profile drops or changes.

func LookupVariant

func LookupVariant(id string) (Variant, Provider, bool)

LookupVariant returns a catalog variant and its provider.

func NormalizeSuffix

func NormalizeSuffix(s string) (string, error)

NormalizeSuffix validates a domain suffix: ".ru", "gosuslugi.ru", ".рф", "*.example.com". The result is lower case punycode; a leading dot is kept when given (".ru" and "ru" both match the domain and its subdomains, at a label boundary).

Types

type Category

type Category string

Category tells what a variant is for. The admin UI groups the catalog and filters the presets by it.

const (
	CategoryRussia   Category = "russia"   // built for resolving Russian sites from inside Russia
	CategoryRegular  Category = "regular"  // no filtering
	CategoryNoAds    Category = "no_ads"   // blocks ads and trackers
	CategoryFamily   Category = "family"   // blocks adult content (and malware)
	CategorySecurity Category = "security" // blocks malware and phishing
)

func CategoryOf

func CategoryOf(p Preset) Category

CategoryOf is the category a preset is listed under.

type Client

type Client string

Client is an app that carries the DNS of a subscription.

const (
	ClientHapp      Client = "happ"
	ClientMihomo    Client = "mihomo" // the Mihomo core and apps built on it
	ClientAmneziaWG Client = "amneziawg"
)

func (Client) IPv6

func (c Client) IPv6() bool

IPv6 reports whether IPv6 resolver addresses are usable: Mihomo takes them; Happ's profile and Amnezia's key and .conf are IPv4 only.

func (Client) Supports

func (c Client) Supports(k Kind) bool

Supports reports whether the client can carry a transport.

func (Client) Transports

func (c Client) Transports() []Kind

Transports is what a client can carry. Sources: the client config formats (Mihomo dns:, Amnezia dns1/dns2), happ.go (Happ has plain and DoH, no DoT).

type Endpoint

type Endpoint struct {
	Kind    Kind   // the transport actually used
	Address string // plain: "ip" or "ip:port"; DoH: the URL; DoT: "host" or "host:port"
	// IPs of the DoH/DoT host (Happ and Mihomo want an address next to a host name); empty for plain and for a host
	// the catalog does not know.
	IPs []string
	// Variant is the catalog id it came from, "" for a custom server.
	Variant string
}

Endpoint is one resolver as a client gets it.

func Resolve

func Resolve(c Client, pref Kind, servers []Server, ipv4Only bool) (out []Endpoint, notes []string)

Resolve turns a server list (the main servers of a preset, or one split rule's) into the endpoints a client can carry, best first, plus notes (English) on what was dropped. pref is the preset's preferred transport, ipv4Only drops IPv6 addresses. The order follows the server list, round-robin: the first endpoint of every server, then the second of every server. A plain variant has two addresses, so the first two plain endpoints of [Cloudflare, Google] are 1.1.1.1 and 8.8.8.8 (what AmneziaWG's two-address DNS wants).

type Kind

type Kind string

Kind is how a server is reached.

const (
	KindPlain Kind = "plain" // UDP/TCP DNS: "77.88.8.8" or "77.88.8.8:53"
	KindDoH   Kind = "doh"   // DNS over HTTPS: "https://dns.example.com/dns-query"
	KindDoT   Kind = "dot"   // DNS over TLS: "dns.example.com" or "94.140.14.14:853"
)

func NormalizeTransport

func NormalizeTransport(k Kind) (Kind, error)

NormalizeTransport validates a preferred transport; "" means plain.

type MihomoDNS

type MihomoDNS struct {
	Nameserver            []string
	DefaultNameserver     []string // may be empty: the core's own defaults apply
	ProxyServerNameserver []string // never empty ("system" at worst)
	Policy                []MihomoPolicy
	// Direct are the suffix bodies (lower-case punycode, no leading dot) whose traffic goes DIRECT; they are exactly
	// the suffixes of Policy.
	Direct []string
	// Notes (English) say what of the preset a Mihomo profile cannot carry.
	Notes []string
}

MihomoDNS is the dns section and the direct suffixes of one preset.

type MihomoPolicy

type MihomoPolicy struct {
	Domain  string
	Servers []string
}

MihomoPolicy is one nameserver-policy entry: Domain is "+.<suffix>" (the suffix and its subdomains).

type Preset

type Preset struct {
	ID, Name, Description string
	Builtin               bool
	Servers               []Server
	Split                 []SplitRule
	IPv4Only              bool
	// SplitDirect: the split rules' domains bypass the VPN (Happ can only send a split resolver's domains
	// direct). Off = clients that cannot keep the split proxied ignore it.
	SplitDirect bool
	// Transport is the preferred transport (plain, dot, doh); see Resolve. Never empty for a stored preset.
	Transport Kind
}

Preset is a DNS preset ready for use (validated, servers and split decoded).

func (Preset) EndpointsFor

func (p Preset) EndpointsFor(c Client) ([]Endpoint, []string)

EndpointsFor is Resolve for the main servers of a preset: what the client gets for every domain that is not split off.

func (Preset) MihomoDNS

func (p Preset) MihomoDNS(group string) (MihomoDNS, error)

MihomoDNS renders the preset for a Mihomo profile. group is the proxy group the main servers' queries go through ("" = no "#group", the queries use the core's own routing). An error means no main server can be expressed; the caller leaves the dns section out.

func (Preset) SplitEndpointsFor

func (p Preset) SplitEndpointsFor(c Client, i int) ([]Endpoint, []string)

SplitEndpointsFor is Resolve for the servers of split rule i of a preset.

type Provider

type Provider struct {
	ID, Name string
	Variants []Variant
}

Provider groups the variants of one operator.

func Catalog

func Catalog() []Provider

Catalog returns the providers in display order. Callers must not modify it.

type Server

type Server struct {
	Kind    Kind   `json:"kind,omitempty"`
	Address string `json:"address,omitempty"`
	Variant string `json:"variant,omitempty"`
}

Server is one resolver: a catalog variant (Variant set, see providers.go) or a custom address (Kind and Address).

func NormalizeServer

func NormalizeServer(s Server) (Server, error)

NormalizeServer validates a server and returns it in canonical form.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service implements DnsService and answers "which preset applies to this user".

func New

func New(st *store.Store) *Service

New builds the service.

func (*Service) DefaultPresetID

func (s *Service) DefaultPresetID(ctx context.Context) (string, error)

DefaultPresetID returns the instance default as chosen by the admin ("" = the built-in default, also when the stored id no longer exists).

func (*Service) Effective

func (s *Service) Effective(ctx context.Context, userID string) (Preset, Source, error)

Effective returns the preset that applies to a user and where it comes from: the user's own, else the user's group's, else the instance default, else the built-in default. store.ErrNotFound for an unknown user.

func (*Service) Handler

func (s *Service) Handler() (string, http.Handler)

Handler returns the Connect path and handler of DnsService (session checks are added by the HTTP server).

func (*Service) SetDefaultPresetID

func (s *Service) SetDefaultPresetID(ctx context.Context, id string) error

SetDefaultPresetID sets the instance default; "" selects the built-in default. ErrUnknownPreset when the id does not exist. A real change is written to the audit log (the settings page saves it with every edit).

type Source

type Source string

Source tells where an effective preset comes from.

const (
	SourceUser    Source = store.DNSSourceUser
	SourceGroup   Source = store.DNSSourceGroup
	SourceDefault Source = store.DNSSourceDefault
)

func (Source) Proto

func (s Source) Proto() adminv1.DnsSource

Proto converts a source to the API enum.

type SplitRule

type SplitRule struct {
	Suffixes []string `json:"suffixes"`
	Servers  []Server `json:"servers"`
}

SplitRule sends domains ending in one of Suffixes to Servers instead of the preset's main ones.

type Variant

type Variant struct {
	ID             string // "provider/variant"
	NameRU, NameEN string
	Category       Category
	IPv4, IPv6     []string
	NoPlain        bool   // the addresses do not answer plain DNS (they only bootstrap DoT/DoH)
	DoTHost        string // empty: no DoT endpoint
	DoTPort        int
	DoHURL         string // empty: no DoH endpoint
	NoteRU, NoteEN string
}

Variant is one way to use a provider. The IP addresses answer plain DNS (primary first) and, being anycast addresses of the same service, also serve as the bootstrap addresses of the DoT and DoH host names.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL