dns

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2025 License: GPL-2.0 Imports: 25 Imported by: 0

README

sing-dns

DNS client with caching

License

Copyright (C) 2022 by nekohasekai <contact-sagernet@sekai.icu>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Documentation

Index

Constants

View Source
const (
	DefaultTTL     = 600
	DefaultTimeout = 10 * time.Second
)
View Source
const MimeType = "application/dns-message"

Variables

View Source
var (
	ErrNoRawSupport           = E.New("no raw query support by current transport")
	ErrNotCached              = E.New("not cached")
	ErrResponseRejected       = E.New("response rejected")
	ErrResponseRejectedCached = E.Extend(ErrResponseRejected, "cached")
)

Functions

func ClientSubnetFromContext

func ClientSubnetFromContext(ctx context.Context) (netip.Prefix, bool)

func ContextWithClientSubnet

func ContextWithClientSubnet(ctx context.Context, clientSubnet netip.Prefix) context.Context

func ContextWithDisableCache

func ContextWithDisableCache(ctx context.Context, val bool) context.Context

func ContextWithRewriteTTL

func ContextWithRewriteTTL(ctx context.Context, val uint32) context.Context

func DisableCacheFromContext

func DisableCacheFromContext(ctx context.Context) bool

func FixedResponse

func FixedResponse(id uint16, question dns.Question, addresses []netip.Addr, timeToLive uint32) *dns.Msg

func MessageToAddresses

func MessageToAddresses(response *dns.Msg) ([]netip.Addr, error)

func NewDialerWrapper

func NewDialerWrapper(dialer N.Dialer, client *Client, transport Transport, strategy DomainStrategy, fallbackDelay time.Duration) N.Dialer

func RegisterTransport

func RegisterTransport(schemes []string, constructor TransportConstructor)

func RewriteTTLFromContext

func RewriteTTLFromContext(ctx context.Context) (uint32, bool)

func SetClientSubnet

func SetClientSubnet(message *dns.Msg, clientSubnet netip.Prefix, override bool) *dns.Msg

func TruncateDNSMessage

func TruncateDNSMessage(request *dns.Msg, response *dns.Msg, headroom int) (*buf.Buffer, error)

Types

type Client

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

func NewClient

func NewClient(options ClientOptions) *Client

func (*Client) ClearCache

func (c *Client) ClearCache()

func (*Client) Exchange

func (c *Client) Exchange(ctx context.Context, transport Transport, message *dns.Msg, strategy DomainStrategy) (*dns.Msg, error)

func (*Client) ExchangeCache

func (c *Client) ExchangeCache(ctx context.Context, message *dns.Msg) (*dns.Msg, bool)

func (*Client) ExchangeWithResponseCheck

func (c *Client) ExchangeWithResponseCheck(ctx context.Context, transport Transport, message *dns.Msg, strategy DomainStrategy, responseChecker func(response *dns.Msg) bool) (*dns.Msg, error)

func (*Client) Lookup

func (c *Client) Lookup(ctx context.Context, transport Transport, domain string, strategy DomainStrategy) ([]netip.Addr, error)

func (*Client) LookupCache

func (c *Client) LookupCache(ctx context.Context, domain string, strategy DomainStrategy) ([]netip.Addr, bool)

func (*Client) LookupWithResponseCheck

func (c *Client) LookupWithResponseCheck(ctx context.Context, transport Transport, domain string, strategy DomainStrategy, responseChecker func(responseAddrs []netip.Addr) bool) ([]netip.Addr, error)

func (*Client) Start

func (c *Client) Start()

type ClientOptions

type ClientOptions struct {
	Timeout          time.Duration
	DisableCache     bool
	DisableExpire    bool
	IndependentCache bool
	RDRC             func() RDRCStore
	Logger           logger.ContextLogger
}

type DialerWrapper

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

func (*DialerWrapper) DialContext

func (d *DialerWrapper) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error)

func (*DialerWrapper) ListenPacket

func (d *DialerWrapper) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error)

func (*DialerWrapper) Upstream

func (d *DialerWrapper) Upstream() any

type DomainStrategy

type DomainStrategy = uint8
const (
	DomainStrategyAsIS DomainStrategy = iota
	DomainStrategyPreferIPv4
	DomainStrategyPreferIPv6
	DomainStrategyUseIPv4
	DomainStrategyUseIPv6
)

type HTTPSTransport

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

func NewHTTPSTransport

func NewHTTPSTransport(options TransportOptions) *HTTPSTransport

func (*HTTPSTransport) Close

func (t *HTTPSTransport) Close() error

func (*HTTPSTransport) Exchange

func (t *HTTPSTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error)

func (*HTTPSTransport) Lookup

func (t *HTTPSTransport) Lookup(ctx context.Context, domain string, strategy DomainStrategy) ([]netip.Addr, error)

func (*HTTPSTransport) Name

func (t *HTTPSTransport) Name() string

func (*HTTPSTransport) Raw

func (t *HTTPSTransport) Raw() bool

func (*HTTPSTransport) Reset

func (t *HTTPSTransport) Reset()

func (*HTTPSTransport) Start

func (t *HTTPSTransport) Start() error

type LocalTransport

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

func NewLocalTransport

func NewLocalTransport(options TransportOptions) *LocalTransport

func (*LocalTransport) Close

func (t *LocalTransport) Close() error

func (*LocalTransport) Exchange

func (t *LocalTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error)

func (*LocalTransport) Lookup

func (t *LocalTransport) Lookup(ctx context.Context, domain string, strategy DomainStrategy) ([]netip.Addr, error)

func (*LocalTransport) Name

func (t *LocalTransport) Name() string

func (*LocalTransport) Raw

func (t *LocalTransport) Raw() bool

func (*LocalTransport) Reset

func (t *LocalTransport) Reset()

func (*LocalTransport) Start

func (t *LocalTransport) Start() error

type RCodeError

type RCodeError uint16
const (
	RCodeSuccess        RCodeError = 0 // NoError
	RCodeFormatError    RCodeError = 1 // FormErr
	RCodeServerFailure  RCodeError = 2 // ServFail
	RCodeNameError      RCodeError = 3 // NXDomain
	RCodeNotImplemented RCodeError = 4 // NotImp
	RCodeRefused        RCodeError = 5 // Refused
)

func (RCodeError) Error

func (e RCodeError) Error() string

type RCodeTransport

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

func NewRCodeTransport

func NewRCodeTransport(options TransportOptions) (*RCodeTransport, error)

func (*RCodeTransport) Close

func (t *RCodeTransport) Close() error

func (*RCodeTransport) Exchange

func (t *RCodeTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error)

func (*RCodeTransport) Lookup

func (t *RCodeTransport) Lookup(ctx context.Context, domain string, strategy DomainStrategy) ([]netip.Addr, error)

func (*RCodeTransport) Name

func (t *RCodeTransport) Name() string

func (*RCodeTransport) Raw

func (t *RCodeTransport) Raw() bool

func (*RCodeTransport) Reset

func (t *RCodeTransport) Reset()

func (*RCodeTransport) Start

func (t *RCodeTransport) Start() error

type RDRCStore

type RDRCStore interface {
	LoadRDRC(transportName string, qName string, qType uint16) (rejected bool)
	SaveRDRC(transportName string, qName string, qType uint16) error
	SaveRDRCAsync(transportName string, qName string, qType uint16, logger logger.Logger)
}

type TCPTransport

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

func NewTCPTransport

func NewTCPTransport(options TransportOptions) (*TCPTransport, error)

func (*TCPTransport) Close

func (t *TCPTransport) Close() error

func (*TCPTransport) Exchange

func (t *TCPTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error)

func (*TCPTransport) Lookup

func (t *TCPTransport) Lookup(ctx context.Context, domain string, strategy DomainStrategy) ([]netip.Addr, error)

func (*TCPTransport) Name

func (t *TCPTransport) Name() string

func (*TCPTransport) Raw

func (t *TCPTransport) Raw() bool

func (*TCPTransport) Reset

func (t *TCPTransport) Reset()

func (*TCPTransport) Start

func (t *TCPTransport) Start() error

type TLSTransport

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

func NewTLSTransport

func NewTLSTransport(options TransportOptions) (*TLSTransport, error)

func (*TLSTransport) Close

func (t *TLSTransport) Close() error

func (*TLSTransport) Exchange

func (t *TLSTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error)

func (*TLSTransport) Lookup

func (t *TLSTransport) Lookup(ctx context.Context, domain string, strategy DomainStrategy) ([]netip.Addr, error)

func (*TLSTransport) Name

func (t *TLSTransport) Name() string

func (*TLSTransport) Raw

func (t *TLSTransport) Raw() bool

func (*TLSTransport) Reset

func (t *TLSTransport) Reset()

func (*TLSTransport) Start

func (t *TLSTransport) Start() error

type Transport

type Transport interface {
	Name() string
	Start() error
	Reset()
	Close() error
	Raw() bool
	Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error)
	Lookup(ctx context.Context, domain string, strategy DomainStrategy) ([]netip.Addr, error)
}

func CreateTransport

func CreateTransport(options TransportOptions) (Transport, error)

type TransportConstructor

type TransportConstructor = func(options TransportOptions) (Transport, error)

type TransportOptions

type TransportOptions struct {
	Context      context.Context
	Logger       logger.ContextLogger
	Name         string
	Dialer       N.Dialer
	Address      string
	ClientSubnet netip.Prefix
}

type UDPTransport

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

func NewUDPTransport

func NewUDPTransport(options TransportOptions) (*UDPTransport, error)

func (*UDPTransport) Close

func (t *UDPTransport) Close() error

func (*UDPTransport) Exchange

func (t *UDPTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error)

func (*UDPTransport) Lookup

func (t *UDPTransport) Lookup(ctx context.Context, domain string, strategy DomainStrategy) ([]netip.Addr, error)

func (*UDPTransport) Name

func (t *UDPTransport) Name() string

func (*UDPTransport) Raw

func (t *UDPTransport) Raw() bool

func (*UDPTransport) Reset

func (t *UDPTransport) Reset()

func (*UDPTransport) Start

func (t *UDPTransport) Start() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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