utils

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2021 License: GPL-3.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IPv4UdpMaxPayload = 1472 // MTU 1500 - 20 IPv4 header - 8 udp header
	IPv6UdpMaxPayload = 1452 // MTU 1500 - 40 IPv6 header - 8 udp header
)

Variables

This section is empty.

Functions

func ApplyMaximumTTL added in v1.3.0

func ApplyMaximumTTL(m *dns.Msg, ttl uint32)

func ApplyMinimalTTL added in v1.3.0

func ApplyMinimalTTL(m *dns.Msg, ttl uint32)

func BoolLogic added in v0.23.0

func BoolLogic(ctx context.Context, qCtx *handler.Context, fs []handler.Matcher, logicalAND bool) (matched bool, err error)

func BytesToStringUnsafe added in v1.2.0

func BytesToStringUnsafe(b []byte) string

BytesToStringUnsafe converts bytes to string.

func ExchangeParallel added in v0.25.2

func ExchangeParallel(ctx context.Context, qCtx *handler.Context, upstreams []Upstream, logger *zap.Logger) (r *dns.Msg, err error)

func GenerateCertificate added in v0.21.0

func GenerateCertificate(dnsName string) (cert tls.Certificate, err error)

GenerateCertificate generates a ecdsa certificate with given dnsName. This should only use in test.

func GetIPFromAddr added in v0.15.0

func GetIPFromAddr(addr net.Addr) (ip net.IP)

GetIPFromAddr returns net.IP from net.Addr. Will return nil if no ip address can be parsed.

func GetMinimalTTL added in v1.3.0

func GetMinimalTTL(m *dns.Msg) uint32

GetMinimalTTL returns the minimal ttl of this msg. If msg m has no record, it returns 0.

func GetMsgBuf

func GetMsgBuf(size int) []byte

func GetMsgBufFor

func GetMsgBufFor(m *dns.Msg) ([]byte, error)

func GetMsgKey added in v0.17.1

func GetMsgKey(m *dns.Msg, salt uint16) (string, error)

GetMsgKey unpacks m and set its id to salt.

func GetTimer

func GetTimer(t time.Duration) *time.Timer

func LoadCertPool added in v0.21.0

func LoadCertPool(certs []string) (*x509.CertPool, error)

LoadCertPool reads and loads certificates in certs.

func ReadMsgFromTCP

func ReadMsgFromTCP(c io.Reader) (m *dns.Msg, n int, err error)

ReadMsgFromTCP reads msg from a tcp connection. n represents how many bytes are read from c.

func ReadMsgFromUDP

func ReadMsgFromUDP(c io.Reader, bufSize int) (m *dns.Msg, n int, err error)

func ReadUDPMsgFrom

func ReadUDPMsgFrom(c net.PacketConn, bufSize int) (m *dns.Msg, from net.Addr, n int, err error)

func ReleaseMsgBuf

func ReleaseMsgBuf(buf []byte)

func ReleaseTimer

func ReleaseTimer(timer *time.Timer)

func RemoveComment added in v0.21.0

func RemoveComment(s, symbol string) string

RemoveComment removes comment after "symbol".

func ResetAndDrainTimer

func ResetAndDrainTimer(timer *time.Timer, d time.Duration)

func SetTTL added in v1.3.0

func SetTTL(m *dns.Msg, ttl uint32)

SetTTL updates all records' ttl to ttl, except opt record.

func SplitLine added in v0.21.0

func SplitLine(s string) []string

SplitLine removes all spaces " " and extracts words from s.

func SplitLineReg added in v1.3.3

func SplitLineReg(s string) []string

SplitLineReg extracts words from s by using regexp "\S+".

func SplitSchemeAndHost added in v1.3.0

func SplitSchemeAndHost(addr string) (protocol, host string)

SplitSchemeAndHost splits addr to protocol and host.

func SplitString2 added in v1.3.3

func SplitString2(s, symbol string) (s1 string, s2 string, ok bool)

SplitString2 split s to two parts by given symbol

func TryAddPort added in v0.15.0

func TryAddPort(host string, port uint16) string

TryAddPort add port to host if host does not has an port suffix.

func WalkExecutableCmd added in v1.0.0

func WalkExecutableCmd(ctx context.Context, qCtx *handler.Context, logger *zap.Logger, entry ExecutableCmd) (err error)

WalkExecutableCmd executes the ExecutableCmd, include its `goto`. This should only be used in root cmd node.

func WriteMsgToTCP

func WriteMsgToTCP(c io.Writer, m *dns.Msg) (n int, err error)

WriteMsgToTCP writes m to c. n represents how many bytes are wrote to c. This includes 2 bytes tcp header.

func WriteMsgToUDP

func WriteMsgToUDP(c io.Writer, m *dns.Msg) (n int, err error)

func WriteRawMsgToTCP

func WriteRawMsgToTCP(c io.Writer, b []byte) (n int, err error)

WriteRawMsgToTCP writes b to c. n represents how many bytes are wrote to c. This includes 2 bytes tcp header.

func WriteRawMsgToUDP

func WriteRawMsgToUDP(c io.Writer, b []byte) (n int, err error)

func WriteUDPMsgTo

func WriteUDPMsgTo(m *dns.Msg, c net.PacketConn, to net.Addr) (n int, err error)

Types

type Allocator

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

func NewAllocator

func NewAllocator() *Allocator

NewAllocator initiates a []byte allocator for dns.Msg less than 65536 bytes, the waste(memory fragmentation) of space allocation is guaranteed to be no more than 50%.

func (*Allocator) Get

func (alloc *Allocator) Get(size int) []byte

Get a []byte from pool with most appropriate cap

func (*Allocator) Put

func (alloc *Allocator) Put(buf []byte)

Put returns a []byte to pool for future use, which the cap must be exactly 2^n

type BytesBufPool added in v1.3.0

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

func NewBytesBufPool added in v1.3.0

func NewBytesBufPool(initSize int) *BytesBufPool

func (*BytesBufPool) Get added in v1.3.0

func (p *BytesBufPool) Get() *bytes.Buffer

func (*BytesBufPool) Release added in v1.3.0

func (p *BytesBufPool) Release(b *bytes.Buffer)

type ConcurrentLimiter added in v1.3.3

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

ConcurrentLimiter

func NewConcurrentLimiter added in v1.3.3

func NewConcurrentLimiter(max int) *ConcurrentLimiter

NewConcurrentLimiter returns a ConcurrentLimiter, max must > 0.

func (*ConcurrentLimiter) Available added in v1.3.3

func (l *ConcurrentLimiter) Available() int

func (*ConcurrentLimiter) Done added in v1.3.3

func (l *ConcurrentLimiter) Done()

func (*ConcurrentLimiter) Wait added in v1.3.3

func (l *ConcurrentLimiter) Wait() <-chan struct{}

type DefaultServerHandler added in v1.0.0

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

func NewDefaultServerHandler added in v1.0.0

func NewDefaultServerHandler(config *DefaultServerHandlerConfig) *DefaultServerHandler

NewDefaultServerHandler: concurrentLimit <= 0 means no concurrent limit. Also see DefaultServerHandler.ServeDNS.

func (*DefaultServerHandler) ServeDNS added in v1.0.0

ServeDNS: If entry returns an err, a SERVFAIL response will be sent back to client. If concurrentLimit is reached, the query will block and wait available token until ctx is done.

type DefaultServerHandlerConfig added in v1.0.0

type DefaultServerHandlerConfig struct {
	// Logger is used for logging, it cannot be nil.
	Logger *zap.Logger
	// Entry is the entry ExecutablePlugin's tag. This shouldn't be empty.
	Entry *ExecutableCmdSequence
	// ConcurrentLimit controls the max concurrent queries.
	// If ConcurrentLimit <= 0, means no limit.
	ConcurrentLimit int
}

type DummyServerHandler added in v1.0.0

type DummyServerHandler struct {
	T       *testing.T
	WantMsg *dns.Msg
	WantErr error
}

func (*DummyServerHandler) ServeDNS added in v1.0.0

type ExchangeSingleFlightGroup added in v0.21.0

type ExchangeSingleFlightGroup struct {
	singleflight.Group
}

func (*ExchangeSingleFlightGroup) Exchange added in v0.21.0

func (g *ExchangeSingleFlightGroup) Exchange(ctx context.Context, qCtx *handler.Context, upstreams []Upstream, logger *zap.Logger) (r *dns.Msg, err error)

type ExecutableCmd added in v1.0.0

type ExecutableCmd interface {
	ExecCmd(ctx context.Context, qCtx *handler.Context, logger *zap.Logger) (goTwo string, earlyStop bool, err error)
}

type ExecutableCmdSequence added in v1.0.0

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

func ParseExecutableCmdSequence added in v1.0.0

func ParseExecutableCmdSequence(in []interface{}) (*ExecutableCmdSequence, error)

func (*ExecutableCmdSequence) ExecCmd added in v1.0.0

func (es *ExecutableCmdSequence) ExecCmd(ctx context.Context, qCtx *handler.Context, logger *zap.Logger) (goTwo string, earlyStop bool, err error)

ExecCmd executes the sequence.

func (*ExecutableCmdSequence) Len added in v1.0.0

func (es *ExecutableCmdSequence) Len() int

type FallbackConfig added in v1.0.0

type FallbackConfig struct {
	// Primary exec sequence, must have at least one element.
	Primary []interface{} `yaml:"primary"`
	// Secondary exec sequence, must have at least one element.
	Secondary []interface{} `yaml:"secondary"`

	StatLength int `yaml:"stat_length"` // An Zero value disables the (normal) fallback.
	Threshold  int `yaml:"threshold"`

	// FastFallback threshold in milliseconds. Zero means fast fallback is disabled.
	FastFallback int `yaml:"fast_fallback"`

	// AlwaysStandby: secondary should always standby in fast fallback.
	AlwaysStandby bool `yaml:"always_standby"`
}

type FallbackECS added in v1.0.0

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

func ParseFallbackECS added in v1.0.0

func ParseFallbackECS(c *FallbackConfig) (*FallbackECS, error)

func (*FallbackECS) ExecCmd added in v1.0.0

func (f *FallbackECS) ExecCmd(ctx context.Context, qCtx *handler.Context, logger *zap.Logger) (goTwo string, earlyStop bool, err error)

type IfBlock added in v1.0.0

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

func ParseIfBlock added in v1.0.0

func ParseIfBlock(in map[string]interface{}) (*IfBlock, error)

func (*IfBlock) ExecCmd added in v1.0.0

func (b *IfBlock) ExecCmd(ctx context.Context, qCtx *handler.Context, logger *zap.Logger) (goTwo string, earlyStop bool, err error)

type IfBlockConfig added in v1.0.0

type IfBlockConfig struct {
	If    []string      `yaml:"if"`
	IfAnd []string      `yaml:"if_and"`
	Exec  []interface{} `yaml:"exec"`
	Goto  string        `yaml:"goto"`
}

type LRU added in v1.3.0

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

func NewLRU added in v1.3.0

func NewLRU(maxSize int, onEvict func(key string, v interface{})) *LRU

func (*LRU) Add added in v1.3.0

func (q *LRU) Add(key string, v interface{})

func (*LRU) Clean added in v1.3.0

func (q *LRU) Clean(f func(key string, v interface{}) (remove bool)) (removed int)

func (*LRU) Del added in v1.3.0

func (q *LRU) Del(key string)

func (*LRU) Get added in v1.3.0

func (q *LRU) Get(key string) (interface{}, bool)

func (*LRU) Len added in v1.3.0

func (q *LRU) Len() int

func (*LRU) PopFirst added in v1.3.0

func (q *LRU) PopFirst() (key string, v interface{}, ok bool)

type LoadOnceCache

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

func NewCache

func NewCache() *LoadOnceCache

func (*LoadOnceCache) Load

func (c *LoadOnceCache) Load(key string) (interface{}, bool)

func (*LoadOnceCache) LoadFromCacheOrRawDisk

func (c *LoadOnceCache) LoadFromCacheOrRawDisk(file string) (interface{}, []byte, error)

func (*LoadOnceCache) Put

func (c *LoadOnceCache) Put(key string, data interface{}, ttl time.Duration)

func (*LoadOnceCache) Remove

func (c *LoadOnceCache) Remove(key string)

type NetAddr added in v0.17.1

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

NetAddr implements net.Addr interface.

func NewNetAddr added in v0.17.1

func NewNetAddr(addr string, network string) *NetAddr

func (*NetAddr) IP added in v1.3.0

func (n *NetAddr) IP() net.IP

func (*NetAddr) Network added in v0.17.1

func (n *NetAddr) Network() string

func (*NetAddr) String added in v0.17.1

func (n *NetAddr) String() string

type ParallelECS added in v1.0.0

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

func ParseParallelECS added in v1.0.0

func ParseParallelECS(c *ParallelECSConfig) (*ParallelECS, error)

func (*ParallelECS) ExecCmd added in v1.0.0

func (p *ParallelECS) ExecCmd(ctx context.Context, qCtx *handler.Context, logger *zap.Logger) (goTwo string, earlyStop bool, err error)

type ParallelECSConfig added in v1.0.0

type ParallelECSConfig struct {
	Parallel [][]interface{} `yaml:"parallel"`
	Timeout  uint            `yaml:"timeout"`
}

type ResponseWriter added in v1.0.0

type ResponseWriter interface {
	Write(m *dns.Msg) (n int, err error)
}

ResponseWriter can write msg to the client.

type ServerHandler added in v1.0.0

type ServerHandler interface {
	// ServeDNS uses ctx to control deadline, exchanges qCtx, and writes response to w.
	ServeDNS(ctx context.Context, qCtx *handler.Context, w ResponseWriter)
}

type Upstream added in v1.3.0

type Upstream interface {
	Exchange(qCtx *handler.Context) (*dns.Msg, error)
	Address() string
	Trusted() bool
}

Jump to

Keyboard shortcuts

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