dns

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: AGPL-3.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

View Source
const NextProtoDQ = "doq"

NextProtoDQ - During connection establishment, DNS/QUIC support is indicated by selecting the ALPN token "doq" in the crypto handshake.

Variables

View Source
var ErrAllServersFailed = errors.New("all dns servers failed")
View Source
var ErrConditionNotMatch = errors.New("condition not match")
View Source
var ErrNoQuestion = errors.New("no question in dns query")
View Source
var ErrNotFound = errors.New("not found")

Functions

func MsgToBuffer

func MsgToBuffer(msg *d.Msg) (*buf.Buffer, error)

func NewDnsConnImpl

func NewDnsConnImpl(tag string, rrCache *rrCache,
	send func(*dns.Msg) error) *dnsConnImpl

func NewRrCache

func NewRrCache(setting RrCacheSetting) *rrCache

func UnFqdn

func UnFqdn(domain string) string

UnFqdn removes the trailing dot from the domain

Types

type AllFakeDns

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

func (*AllFakeDns) AddFakeDns

func (a *AllFakeDns) AddFakeDns(fakeDns *FakeDns)

func (*AllFakeDns) GetDomainFromFakeDNS

func (a *AllFakeDns) GetDomainFromFakeDNS(ip net.Address) string

func (*AllFakeDns) IsIPInIPPool

func (a *AllFakeDns) IsIPInIPPool(ip net.Address) bool

type Condition

type Condition interface {
	Match(msg *DnsMsgMeta) bool
}

type ConditionDnsServer

type ConditionDnsServer struct {
	DnsServer
	// contains filtered or unexported fields
}

func NewConditionDnsServer

func NewConditionDnsServer(dnsServer DnsServer, conditions ...Condition) *ConditionDnsServer

func (*ConditionDnsServer) MatchConditions

func (d *ConditionDnsServer) MatchConditions(msg *DnsMsgMeta) bool

type Dns

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

func NewDns

func NewDns(local *StaticDnsServer, rules []*DnsRule, dnsServers []DnsServer) *Dns

func (*Dns) Close

func (dsp *Dns) Close() error

func (*Dns) HandleQuery

func (d *Dns) HandleQuery(ctx context.Context, msg *DnsMsgMeta) (*dns.Msg, error)

func (*Dns) ReadPacket

func (dsp *Dns) ReadPacket() (*udp.Packet, error)

func (*Dns) Start

func (dsp *Dns) Start() error

func (*Dns) WritePacket

func (dsp *Dns) WritePacket(p *udp.Packet) error

type DnsConn

type DnsConn interface {
	common.Runnable
	// packet contains a dns message.
	// WritePacket takes ownership of the packet. Caller should not
	// use Packet after WritePacket.
	WritePacket(*udp.Packet) error
	// packet contains a dns message
	ReadPacket() (*udp.Packet, error)
}

type DnsHandler

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

intercept dns queries and send back dns responses

func NewHandlerV

func NewHandlerV() *DnsHandler

func (*DnsHandler) HandleFlow

func (s *DnsHandler) HandleFlow(ctx context.Context, dst net.Destination, rw buf.ReaderWriter) error

implements i.FlowHandler

func (*DnsHandler) HandlePacketConn

func (s *DnsHandler) HandlePacketConn(ctx context.Context, dst net.Destination, rw udp.PacketReaderWriter) error

func (*DnsHandler) Tag

func (s *DnsHandler) Tag() string

func (*DnsHandler) WithDns

func (s *DnsHandler) WithDns(dns dnsServer) *DnsHandler

func (*DnsHandler) WithTag

func (s *DnsHandler) WithTag(tag string) *DnsHandler

type DnsMsgMeta

type DnsMsgMeta struct {
	*dns.Msg
	Src *net.Destination
}

func (*DnsMsgMeta) Tcp

func (d *DnsMsgMeta) Tcp() bool

type DnsResolver

type DnsResolver struct {
	*net.Resolver
}

func NewGoIpResolver

func NewGoIpResolver() *DnsResolver

func (*DnsResolver) LookupIP

func (d *DnsResolver) LookupIP(ctx context.Context, host string) ([]net.IP, error)

func (*DnsResolver) LookupIPPrefer4

func (d *DnsResolver) LookupIPPrefer4(ctx context.Context, host string) ([]net.IP, error)

func (*DnsResolver) LookupIPSpeed

func (d *DnsResolver) LookupIPSpeed(ctx context.Context, host string) ([]net.IP, error)

func (*DnsResolver) LookupIPv4

func (d *DnsResolver) LookupIPv4(ctx context.Context, host string) ([]net.IP, error)

func (*DnsResolver) LookupIPv6

func (d *DnsResolver) LookupIPv6(ctx context.Context, host string) ([]net.IP, error)

type DnsRule

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

func NewDnsRule

func NewDnsRule(dnsServer DnsServer, conditions ...Condition) *DnsRule

type DnsServer

type DnsServer interface {
	common.Runnable
	i.DnsServer
}

type DnsServerConcurrent

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

reuse conn for dns query

func (*DnsServerConcurrent) Close

func (ns *DnsServerConcurrent) Close() error

func (*DnsServerConcurrent) HandleQuery

func (ns *DnsServerConcurrent) HandleQuery(ctx context.Context, msg *dns.Msg, tcp bool) (*dns.Msg, error)

msg should be standard dns query message: has only one question, opcode is QUERY

func (DnsServerConcurrent) ReadPacket

func (ns DnsServerConcurrent) ReadPacket() (*udp.Packet, error)

func (*DnsServerConcurrent) RemoveDest

func (d *DnsServerConcurrent) RemoveDest(remove net.AddressPort, fallback []net.AddressPort)

func (*DnsServerConcurrent) SetDests

func (d *DnsServerConcurrent) SetDests(dests []net.AddressPort)

func (*DnsServerConcurrent) Start

func (ns *DnsServerConcurrent) Start() error

func (DnsServerConcurrent) WritePacket

func (ns DnsServerConcurrent) WritePacket(p *udp.Packet) error

msg should be standard dns query message: has only one question, opcode is QUERY, no other fancy stuff.

type DnsServerConcurrentOption

type DnsServerConcurrentOption struct {
	Name            string
	NameserverAddrs []net.AddressPort
	Handler         i.FlowHandler
	IPToDomain      *IPToDomain
	Tls             bool
	ClientIp        net.IP
	Dispatcher      packetDispatcher
	RrCache         *rrCache
}

type DnsServerSerial

type DnsServerSerial struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

does not reuse conn for dns query

func NewDnsServerSerial

func NewDnsServerSerial(dests []net.AddressPort, dialer i.Dialer, ipToDomain *IPToDomain) *DnsServerSerial

func (*DnsServerSerial) Close

func (d *DnsServerSerial) Close() error

func (*DnsServerSerial) HandleQuery

func (d *DnsServerSerial) HandleQuery(ctx context.Context, msg *dns.Msg, tcp bool) (*dns.Msg, error)

func (*DnsServerSerial) RemoveDest

func (d *DnsServerSerial) RemoveDest(toBeRemoved net.AddressPort, fallback []net.AddressPort)

func (*DnsServerSerial) ReplaceDests

func (d *DnsServerSerial) ReplaceDests(dests []net.AddressPort)

func (*DnsServerSerial) Start

func (d *DnsServerSerial) Start() error

type DnsServerToResolver

type DnsServerToResolver struct {
	DnsServers []DnsServer
}

func NewDnsServerToResolver

func NewDnsServerToResolver(dnsServers ...DnsServer) *DnsServerToResolver

func (*DnsServerToResolver) LookupECH

func (d *DnsServerToResolver) LookupECH(ctx context.Context, domain string) ([]byte, error)

func (*DnsServerToResolver) LookupIP

func (d *DnsServerToResolver) LookupIP(ctx context.Context, host string) ([]net.IP, error)

func (*DnsServerToResolver) LookupIPPrefer4

func (d *DnsServerToResolver) LookupIPPrefer4(ctx context.Context, host string) ([]net.IP, error)

func (*DnsServerToResolver) LookupIPv4

func (d *DnsServerToResolver) LookupIPv4(ctx context.Context, host string) ([]net.IP, error)

func (*DnsServerToResolver) LookupIPv6

func (d *DnsServerToResolver) LookupIPv6(ctx context.Context, host string) ([]net.IP, error)

type DnsToDnsServer

type DnsToDnsServer struct {
	*Dns
}

func (*DnsToDnsServer) HandleQuery

func (d *DnsToDnsServer) HandleQuery(ctx context.Context, msg *dns.Msg, tcp bool) (*dns.Msg, error)

type DoHNameServer

type DoHNameServer struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

DoHNameServer implemented DNS over HTTPS (RFC8484) Wire Format, which is compatible with traditional dns over udp(RFC1035), thus most of the DOH implementation is copied from udpns.go

func NewDoHNameServer

func NewDoHNameServer(option DoHNameServerOption) (*DoHNameServer, error)

NewDoHNameServer creates DOH server object for remote resolving.

func (*DoHNameServer) Close

func (d *DoHNameServer) Close() error

func (*DoHNameServer) HandleQuery

func (d *DoHNameServer) HandleQuery(ctx context.Context, msg *dns.Msg, tcp bool) (*dns.Msg, error)

func (*DoHNameServer) Name

func (d *DoHNameServer) Name() string

func (*DoHNameServer) Start

func (d *DoHNameServer) Start() error

type DoHNameServerOption

type DoHNameServerOption struct {
	ClientIP   net.IP
	Handler    i.FlowHandler
	Name       string
	Url        string
	IpToDomain *IPToDomain
	Tls        *tls.Config
	RrCache    *rrCache
}

type DomainAndResolver

type DomainAndResolver struct {
	Domain     string
	Resolver   net.Address
	ExpireTime time.Time
}

type ExcludeDomainCondition

type ExcludeDomainCondition struct {
	DomainSet i.DomainSet
}

func (*ExcludeDomainCondition) Match

func (e *ExcludeDomainCondition) Match(msg *dns.Msg) bool

type FakeDns

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

func NewFakeDns

func NewFakeDns(pools Pools) *FakeDns

func (*FakeDns) Close

func (*FakeDns) Close() error

func (*FakeDns) GetDomainFromFakeDNS

func (f *FakeDns) GetDomainFromFakeDNS(ip net.Address) string

func (*FakeDns) GetResolver

func (f *FakeDns) GetResolver(domain string, ip net.Address) (string, bool)

func (*FakeDns) HandleQuery

func (f *FakeDns) HandleQuery(ctx context.Context, msg *dns.Msg, _ bool) (*dns.Msg, error)

func (*FakeDns) IsIPInIPPool

func (f *FakeDns) IsIPInIPPool(ip net.Address) bool

func (*FakeDns) Name

func (*FakeDns) Name() string

func (*FakeDns) Start

func (*FakeDns) Start() error

type FakeDnsCondition

type FakeDnsCondition struct {
	FakeDnsEnabled *atomic.Bool
}

func (*FakeDnsCondition) Match

func (f *FakeDnsCondition) Match(msg *DnsMsgMeta) bool

type HasSrcCondition

type HasSrcCondition struct{}

func (*HasSrcCondition) Match

func (h *HasSrcCondition) Match(msg *DnsMsgMeta) bool

type IPOption

type IPOption struct {
	IPv4Enable bool
	IPv6Enable bool
}

type IPToDomain

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

func NewIPToDomain

func NewIPToDomain(size int) *IPToDomain

func (*IPToDomain) GetDomain

func (i *IPToDomain) GetDomain(ip net.IP) []string

func (*IPToDomain) GetResolvers

func (i *IPToDomain) GetResolvers(domain string, ip net.IP) []net.Address

func (*IPToDomain) SetDomain

func (i *IPToDomain) SetDomain(reply *dns.Msg, src net.Address)

type IncludedTypesCondition

type IncludedTypesCondition struct {
	Types []uint16
}

func (*IncludedTypesCondition) Match

func (i *IncludedTypesCondition) Match(msg *DnsMsgMeta) bool

type InternalDns

type InternalDns struct {
	StaticDns             *StaticDnsServer
	DnsServers            []DnsServer
	DnsServerToIPResolver *DnsServerToResolver
}

for dialer to lookup ip when dialing

func NewInternalDns

func NewInternalDns(staticDns *StaticDnsServer, DnsServers ...DnsServer) *InternalDns

func (*InternalDns) Close

func (d *InternalDns) Close() error

func (*InternalDns) LookupIP

func (d *InternalDns) LookupIP(ctx context.Context, host string) ([]net.IP, error)

func (*InternalDns) LookupIPSpeed

func (d *InternalDns) LookupIPSpeed(ctx context.Context, host string) ([]net.IP, error)

func (*InternalDns) LookupIPv4

func (d *InternalDns) LookupIPv4(ctx context.Context, host string) ([]net.IP, error)

func (*InternalDns) LookupIPv6

func (d *InternalDns) LookupIPv6(ctx context.Context, host string) ([]net.IP, error)

func (*InternalDns) Start

func (d *InternalDns) Start() error

type IpOptionDnsServer

type IpOptionDnsServer struct {
	IpOption *IPOption
	DnsServer
}

if ip option conflicts, return a response with no answer

func (*IpOptionDnsServer) HandleQuery

func (p *IpOptionDnsServer) HandleQuery(ctx context.Context, msg *dns.Msg, tcp bool) (*dns.Msg, error)

msg should contain a domain that will go proxy

type Pool

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

func NewPool

func NewPool(cidr string, lruSize int) (*Pool, error)

func (*Pool) GetDomainFromFakeIP

func (fkdns *Pool) GetDomainFromFakeIP(ip nethelper.Address) string

GetDomainFromFakeIP checks if an IP is a fake IP and have corresponding domain name

func (*Pool) GetFakeIPForDomain

func (fkdns *Pool) GetFakeIPForDomain(domain string, ipv4 bool) nethelper.Address

GetFakeIPForDomain checks if there is already an ip for domain first, if so returns the ip; if not generate a new fake IP for domain

func (*Pool) IsIPInIPPool

func (fkdns *Pool) IsIPInIPPool(ip nethelper.Address) bool

type PoolConfig

type PoolConfig struct {
	Cidr    string
	LruSize uint16
}

type Pools

type Pools []*Pool

func NewPools

func NewPools(poolConfigs []*configs.FakeDnsServer_PoolConfig) (Pools, error)

func (Pools) GetDomainFromFakeDNS

func (h Pools) GetDomainFromFakeDNS(ip nethelper.Address) string

func (Pools) GetFakeIP

func (h Pools) GetFakeIP(domain string) []net.IP

func (Pools) GetFakeIPv4

func (h Pools) GetFakeIPv4(domain string) net.IP

func (Pools) GetFakeIPv6

func (h Pools) GetFakeIPv6(domain string) net.IP

func (Pools) IsIPInIPPool

func (h Pools) IsIPInIPPool(ip nethelper.Address) bool

type Prefer4IPResolver

type Prefer4IPResolver struct {
	i.IPResolver
}

func (*Prefer4IPResolver) LookupIP

func (d *Prefer4IPResolver) LookupIP(ctx context.Context, host string) ([]net.IP, error)

type PreferDomainCondition

type PreferDomainCondition struct {
	DomainSet i.DomainSet
	// contains filtered or unexported fields
}

func (*PreferDomainCondition) Match

func (p *PreferDomainCondition) Match(msg *DnsMsgMeta) bool

type QUICNameServer

type QUICNameServer struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

QUICNameServer implemented DNS over QUIC

func NewQUICNameServer

func NewQUICNameServer(option QuicNameServerOption) (*QUICNameServer, error)

NewQUICNameServer creates DNS-over-QUIC client object for local resolving

func (*QUICNameServer) Close

func (s *QUICNameServer) Close() error

func (*QUICNameServer) HandleQuery

func (s *QUICNameServer) HandleQuery(ctx context.Context, msg *dns.Msg, tcp bool) (*dns.Msg, error)

QueryIP is called from dns.Server->queryIPTimeout

func (*QUICNameServer) Name

func (s *QUICNameServer) Name() string

func (*QUICNameServer) Start

func (s *QUICNameServer) Start() error

type QuicNameServerOption

type QuicNameServerOption struct {
	Name        string
	Destination net.Destination
	ClientIp    net.IP
	Handler     i.PacketHandler
	IpToDomain  *IPToDomain
	IPResolver  i.IPResolver
	RrCache     *rrCache
}

type ResolverGetter

type ResolverGetter interface {
	// return true and the nameserver if [ResolverGetter] actually handles the dns query for [domain]
	// and get result of [ip]
	GetResolver(domain string, ip net.Address) (string, bool)
}

type RrCacheSetting added in v1.0.2

type RrCacheSetting struct {
	Duration uint32
}

type StaticDnsServer

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

func NewStaticDnsServer

func NewStaticDnsServer(records []*configs.Record) *StaticDnsServer

func (*StaticDnsServer) ReplyFor

func (s *StaticDnsServer) ReplyFor(msg *dns.Msg) (*dns.Msg, bool)

Jump to

Keyboard shortcuts

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