metadnsq

package
v0.0.0-...-406c194 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2022 License: Apache-2.0 Imports: 47 Imported by: 0

README

dnssrc

dnssrc - a CoreDNS forwarding/proxy plugin with the main function of triaging based on client sources

This plugin is based on the github.com/leiless/dnsredir extension, many thanks to dnsredir for the inspiration

Most of the configurations are similar except for the client-based source address changes

Example

. { 
    # Read address list file,One IP address or CIDR per line
    dnssrc locals.conf {
        expire 30s
        path_reload 3s
        max_fails 0
        health_check 5s
        to 114.114.114.114 223.5.5.5
        policy round_robin
        bootstrap 172.21.66.1
        debug
    }

    # Read url data, Content format as above
    dnssrc https://xxx.xx/xx {
    
    }

    dnssrc 172.21.66.137 192.168.0.1/24 {
        expire 1s
        path_reload 3s
        max_fails 0
        health_check 5s
        to json-doh://dns.google/resolve
        to 1.1.1.1
        to 9.9.9.9
        policy round_robin
        bootstrap 172.21.66.1
        debug
    }
}

Documentation

Index

Constants

View Source
const (
	MatcherLoadFromURI   = "matcherUri"
	MatcherLoadFromLocal = "matcherLocal"
)
View Source
const (
	GlobalTag = "global"
)

Variables

View Source
var (

	// This metric value mainly used for benchmarking purpose
	NameLookupDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
		Namespace: plugin.Namespace,
		Subsystem: pluginName,
		Name:      "name_lookup_duration_ms",
		Buckets:   nameLookupBuckets,
		Help:      "Histogram of the time(in milliseconds) each name lookup took.",
	}, []string{"server", "matched"})

	RequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
		Namespace: plugin.Namespace,
		Subsystem: pluginName,
		Name:      "request_duration_ms",
		Buckets:   requestBuckets,
		Help:      "Histogram of the time(in milliseconds) each request took.",
	}, []string{"server", "to"})

	RequestCount = promauto.NewCounterVec(prometheus.CounterOpts{
		Namespace: plugin.Namespace,
		Subsystem: pluginName,
		Name:      "request_count_total",
		Help:      "Counter of requests made per upstream.",
	}, []string{"server", "to"})

	RcodeCount = promauto.NewCounterVec(prometheus.CounterOpts{
		Namespace: plugin.Namespace,
		Subsystem: pluginName,
		Name:      "response_rcode_count_total",
		Help:      "Rcode counter of requests made per upstream.",
	}, []string{"server", "to", "rcode"})

	// XXX: currently server not embedded into hc failure count label
	HealthCheckFailureCount = promauto.NewCounterVec(prometheus.CounterOpts{
		Namespace: plugin.Namespace,
		Subsystem: pluginName,
		Name:      "hc_failure_count_total",
		Help:      "Counter of the number of failed healthchecks.",
	}, []string{"to"})

	// XXX: Ditto.
	HealthCheckAllDownCount = promauto.NewCounterVec(prometheus.CounterOpts{
		Namespace: plugin.Namespace,
		Subsystem: pluginName,
		Name:      "hc_all_down_count_total",
		Help:      "Counter of the number of complete failures of the healthchecks.",
	}, []string{"to"})
)
View Source
var SupportedPolicies = map[string]Policy{
	"random":      &Random{},
	"round_robin": &RoundRobin{},
	"sequential":  &Sequential{},
	"spray":       &Spray{},
}

SupportedPolicies is the collection of policies registered

Functions

func HostPort

func HostPort(servers []string) ([]string, error)

Taken from parse.HostPortOrFile() with modification

func PluginError

func PluginError(err error) error

func SplitByByte

func SplitByByte(s string, c byte) (string, string)

Return two strings delimited by the `c', the second one will including `c' as beginning character If `c' not found in `s', the second string will be empty

func SplitTransportHost

func SplitTransportHost(s string) (trans string, addr string)

Types

type HealthCheck

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

func (*HealthCheck) Select

func (hc *HealthCheck) Select() *UpstreamHost

func (*HealthCheck) SelectByTag

func (hc *HealthCheck) SelectByTag(tag string) *UpstreamHost

Select an upstream host based on the policy and the health check result Taken from proxy/healthcheck/healthcheck.go with modification

func (*HealthCheck) Start

func (hc *HealthCheck) Start()

func (*HealthCheck) Stop

func (hc *HealthCheck) Stop()

type MetadnsQ

type MetadnsQ struct {
	Next      plugin.Handler
	Upstreams *[]Upstream
}

func (*MetadnsQ) Name

func (r *MetadnsQ) Name() string

func (*MetadnsQ) OnShutdown

func (r *MetadnsQ) OnShutdown() error

func (*MetadnsQ) OnStartup

func (r *MetadnsQ) OnStartup() error

func (*MetadnsQ) ServeDNS

func (r *MetadnsQ) ServeDNS(ctx context.Context, w dns.ResponseWriter, req *dns.Msg) (int, error)

func (*MetadnsQ) TestMatchQuery

func (r *MetadnsQ) TestMatchQuery(req *dns.Msg, reply *dns.Msg) []string

type NoMatcherResponseReverter

type NoMatcherResponseReverter struct {
	dns.ResponseWriter
	// contains filtered or unexported fields
}

func NewNoMatcherResponseReverterr

func NewNoMatcherResponseReverterr(w dns.ResponseWriter) *NoMatcherResponseReverter

func (*NoMatcherResponseReverter) Write

func (r *NoMatcherResponseReverter) Write(buf []byte) (int, error)

func (*NoMatcherResponseReverter) WriteMsg

func (r *NoMatcherResponseReverter) WriteMsg(res1 *dns.Msg) error

WriteMsg records the status code and calls the underlying ResponseWriter's WriteMsg method.

type Once

type Once int32

func (*Once) Do

func (o *Once) Do(f func())

type Policy

type Policy interface {
	// nil will be selected if all hosts are down
	// NOTE: Spray policy will always return a nonnull host
	Select(pool UpstreamHostPool) *UpstreamHost
	SelectByTag(pool UpstreamHostPool, tags string) *UpstreamHost
}

Policy decides how a host will be selected from a pool. When all hosts are unhealthy, it is assumed the health checking failed. In this case each policy will *randomly* return a host from the pool

to prevent no traffic to go through at all.

type Random

type Random struct{}

Random is a policy that selects up hosts from a pool at random.

func (*Random) Select

func (r *Random) Select(pool UpstreamHostPool) *UpstreamHost

Select selects an up host at random from the specified pool.

func (*Random) SelectByTag

func (r *Random) SelectByTag(pool UpstreamHostPool, tag string) *UpstreamHost

func (*Random) String

func (r *Random) String() string

type ResponseReverter

type ResponseReverter struct {
	dns.ResponseWriter
	// contains filtered or unexported fields
}

func NewResponseReverter

func NewResponseReverter(w dns.ResponseWriter) *ResponseReverter

func (*ResponseReverter) Write

func (r *ResponseReverter) Write(buf []byte) (int, error)

func (*ResponseReverter) WriteMsg

func (r *ResponseReverter) WriteMsg(res1 *dns.Msg) error

WriteMsg records the status code and calls the underlying ResponseWriter's WriteMsg method.

type RoundRobin

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

RoundRobin is a policy that selects hosts based on round robin ordering.

func (*RoundRobin) Select

func (r *RoundRobin) Select(pool UpstreamHostPool) *UpstreamHost

Select selects an up host from the pool using a round robin ordering scheme.

func (*RoundRobin) SelectByTag

func (r *RoundRobin) SelectByTag(pool UpstreamHostPool, tag string) *UpstreamHost

func (*RoundRobin) String

func (r *RoundRobin) String() string

type SelectOption

type SelectOption struct {
	ID    string `json:"id"`
	Value string `json:"value"`
}

SelectOption 提供外部模块使用的选项数据

type Sequential

type Sequential struct{}

Sequential is a policy that selects always the first healthy host in the list order.

func (*Sequential) Select

func (s *Sequential) Select(pool UpstreamHostPool) *UpstreamHost

Select always the first that is not Down, nil if all hosts are down

func (*Sequential) SelectByTag

func (s *Sequential) SelectByTag(pool UpstreamHostPool, tag string) *UpstreamHost

func (*Sequential) String

func (s *Sequential) String() string

type Spray

type Spray struct{}

Spray is a policy that selects a host from a pool at random. This should be used as a last ditch attempt to get

a host when all hosts are reporting unhealthy.

func (*Spray) Select

func (s *Spray) Select(pool UpstreamHostPool) *UpstreamHost

Select selects an up host at random from the specified pool.

func (*Spray) SelectByTag

func (s *Spray) SelectByTag(pool UpstreamHostPool, tag string) *UpstreamHost

func (*Spray) String

func (s *Spray) String() string

type StringSet

type StringSet map[string]struct{}

XXX: not thread safe

func (*StringSet) Add

func (s *StringSet) Add(str string)

func (*StringSet) Contains

func (s *StringSet) Contains(str string) bool

type SubMatcherJSON

type SubMatcherJSON struct {
	Enabled      int    `json:"enabled"`
	Upstreams    string `json:"upstreams,omitempty"`
	Name         string `json:"name,omitempty"`
	To           string `json:"to,omitempty"`
	ClientIps    string `json:"client_ips,omitempty"`
	AnwserIps    string `json:"anwser_ips,omitempty"`
	QueryNames   string `json:"query_names,omitempty"`
	AnwserCNames string `json:"anwser_cnames,omitempty"`
	ForceEcs     string `json:"force_ecs,omitempty"`
	Notify       string `json:"notify,omitempty"`
	Nxdomain     int    `json:"nxdomain"`
	Ipset        string `json:"ipset,omitempty"`
}

type Transport

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

Transport settings Inspired from coredns/plugin/forward/persistent.go addr isn't sealed into this struct since it's a high-level item

func (*Transport) Start

func (t *Transport) Start()

Start starts the transport's connection manager.

func (*Transport) Stop

func (t *Transport) Stop()

Stop stops the transport's connection manager.

func (*Transport) Yield

func (t *Transport) Yield(pc *persistConn)

Yield return the connection to transport for reuse.

type Upstream

type Upstream interface {
	// Check if given domain name should be routed to this upstream zone
	Match(name string) (bool, string)
	ReloadSubMatchers() bool
	FromTags() []string
	Name() string
	MatchTag(tag string) bool
	// Select an upstream host to be routed to, nil if no available host
	Select() *UpstreamHost

	UpstreamSelectOptions() []SelectOption
	MatcherData() []map[string]interface{}
	ConfigData() map[string]interface{}

	Start() error
	Stop() error
}

Upstream manages a pool of proxy upstream hosts see: github.com/coredns/proxy#proxy.go 上游转发器接口

func NewReloadableUpstreams

func NewReloadableUpstreams(c *caddy.Controller) ([]Upstream, error)

Parses Caddy config input and return a list of reloadable upstream for this plugin

type UpstreamHost

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

UpstreamHost represents a single upstream DNS server

func (*UpstreamHost) Check

func (uh *UpstreamHost) Check() error

For health check we send to . IN NS +norec message to the upstream. Dial timeouts and empty replies are considered fails

basically anything else constitutes a healthy upstream.

func (*UpstreamHost) Dial

func (uh *UpstreamHost) Dial(proto string, bootstrap []string, noIPv6 bool) (*persistConn, bool, error)

Return:

#0	Persistent connection
#1	true if it's a cached connection
#2	error(if any)

func (*UpstreamHost) Down

func (uh *UpstreamHost) Down() bool

Down checks whether the upstream host is down or not Down will try to use uh.downFunc first, and will fallback

to some default criteria if necessary.

func (*UpstreamHost) Exchange

func (uh *UpstreamHost) Exchange(ctx context.Context, state *request.Request, bootstrap []string, noIPv6 bool) (*dns.Msg, error)

func (*UpstreamHost) InitDOH

func (uh *UpstreamHost) InitDOH(u *reloadableUpstream)

func (*UpstreamHost) IsDOH

func (uh *UpstreamHost) IsDOH() bool

func (*UpstreamHost) Name

func (uh *UpstreamHost) Name() string

type UpstreamHostDownFunc

type UpstreamHostDownFunc func(*UpstreamHost) bool

UpstreamHostDownFunc can be used to customize how Down behaves see: proxy/healthcheck/healthcheck.go

type UpstreamHostPool

type UpstreamHostPool []*UpstreamHost

UpstreamHostPool is an array of upstream DNS servers

Jump to

Keyboard shortcuts

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