2023-03-24 12:55:51 +00:00
|
|
|
package proxy
|
2018-02-05 22:00:47 +00:00
|
|
|
|
|
|
|
|
import (
|
2018-07-09 15:14:55 +01:00
|
|
|
"crypto/tls"
|
2018-02-05 22:00:47 +00:00
|
|
|
"sync/atomic"
|
2018-07-09 15:14:55 +01:00
|
|
|
"time"
|
2018-02-05 22:00:47 +00:00
|
|
|
|
2023-03-24 12:55:51 +00:00
|
|
|
"github.com/coredns/coredns/plugin/pkg/log"
|
2018-09-19 07:29:37 +01:00
|
|
|
"github.com/coredns/coredns/plugin/pkg/transport"
|
|
|
|
|
|
2018-02-05 22:00:47 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
)
|
|
|
|
|
|
2018-07-09 15:14:55 +01:00
|
|
|
// HealthChecker checks the upstream health.
|
|
|
|
|
type HealthChecker interface {
|
|
|
|
|
Check(*Proxy) error
|
|
|
|
|
SetTLSConfig(*tls.Config)
|
2023-03-24 12:55:51 +00:00
|
|
|
GetTLSConfig() *tls.Config
|
2020-03-06 11:52:43 +01:00
|
|
|
SetRecursionDesired(bool)
|
|
|
|
|
GetRecursionDesired() bool
|
2022-04-13 00:39:48 +08:00
|
|
|
SetDomain(domain string)
|
|
|
|
|
GetDomain() string
|
2022-02-09 15:45:52 +01:00
|
|
|
SetTCPTransport()
|
2023-03-24 12:55:51 +00:00
|
|
|
GetReadTimeout() time.Duration
|
|
|
|
|
SetReadTimeout(time.Duration)
|
|
|
|
|
GetWriteTimeout() time.Duration
|
|
|
|
|
SetWriteTimeout(time.Duration)
|
2018-07-09 15:14:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// dnsHc is a health checker for a DNS endpoint (DNS, and DoT).
|
2020-03-06 11:52:43 +01:00
|
|
|
type dnsHc struct {
|
|
|
|
|
c *dns.Client
|
|
|
|
|
recursionDesired bool
|
2022-04-13 00:39:48 +08:00
|
|
|
domain string
|
2020-03-06 11:52:43 +01:00
|
|
|
}
|
2018-07-09 15:14:55 +01:00
|
|
|
|
2018-09-19 07:29:37 +01:00
|
|
|
// NewHealthChecker returns a new HealthChecker based on transport.
|
2022-04-13 00:39:48 +08:00
|
|
|
func NewHealthChecker(trans string, recursionDesired bool, domain string) HealthChecker {
|
2018-09-19 07:29:37 +01:00
|
|
|
switch trans {
|
|
|
|
|
case transport.DNS, transport.TLS:
|
2018-07-09 15:14:55 +01:00
|
|
|
c := new(dns.Client)
|
|
|
|
|
c.Net = "udp"
|
2023-03-24 12:55:51 +00:00
|
|
|
c.ReadTimeout = 1 * time.Second
|
|
|
|
|
c.WriteTimeout = 1 * time.Second
|
2018-07-09 15:14:55 +01:00
|
|
|
|
2023-03-24 12:55:51 +00:00
|
|
|
return &dnsHc{
|
|
|
|
|
c: c,
|
|
|
|
|
recursionDesired: recursionDesired,
|
|
|
|
|
domain: domain,
|
|
|
|
|
}
|
2018-07-09 15:14:55 +01:00
|
|
|
}
|
|
|
|
|
|
2018-10-09 22:50:30 +03:00
|
|
|
log.Warningf("No healthchecker for transport %q", trans)
|
2018-07-09 15:14:55 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *dnsHc) SetTLSConfig(cfg *tls.Config) {
|
|
|
|
|
h.c.Net = "tcp-tls"
|
|
|
|
|
h.c.TLSConfig = cfg
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 12:55:51 +00:00
|
|
|
func (h *dnsHc) GetTLSConfig() *tls.Config {
|
|
|
|
|
return h.c.TLSConfig
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 11:52:43 +01:00
|
|
|
func (h *dnsHc) SetRecursionDesired(recursionDesired bool) {
|
|
|
|
|
h.recursionDesired = recursionDesired
|
|
|
|
|
}
|
|
|
|
|
func (h *dnsHc) GetRecursionDesired() bool {
|
|
|
|
|
return h.recursionDesired
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 00:39:48 +08:00
|
|
|
func (h *dnsHc) SetDomain(domain string) {
|
|
|
|
|
h.domain = domain
|
|
|
|
|
}
|
|
|
|
|
func (h *dnsHc) GetDomain() string {
|
|
|
|
|
return h.domain
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-09 15:45:52 +01:00
|
|
|
func (h *dnsHc) SetTCPTransport() {
|
|
|
|
|
h.c.Net = "tcp"
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 12:55:51 +00:00
|
|
|
func (h *dnsHc) GetReadTimeout() time.Duration {
|
|
|
|
|
return h.c.ReadTimeout
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *dnsHc) SetReadTimeout(t time.Duration) {
|
|
|
|
|
h.c.ReadTimeout = t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *dnsHc) GetWriteTimeout() time.Duration {
|
|
|
|
|
return h.c.WriteTimeout
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *dnsHc) SetWriteTimeout(t time.Duration) {
|
|
|
|
|
h.c.WriteTimeout = t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For HC, we send to . IN NS +[no]rec message to the upstream. Dial timeouts and empty
|
2018-02-05 22:00:47 +00:00
|
|
|
// replies are considered fails, basically anything else constitutes a healthy upstream.
|
|
|
|
|
|
2018-02-15 10:21:57 +01:00
|
|
|
// Check is used as the up.Func in the up.Probe.
|
2018-07-09 15:14:55 +01:00
|
|
|
func (h *dnsHc) Check(p *Proxy) error {
|
|
|
|
|
err := h.send(p.addr)
|
2018-02-05 22:00:47 +00:00
|
|
|
if err != nil {
|
2018-02-15 10:21:57 +01:00
|
|
|
HealthcheckFailureCount.WithLabelValues(p.addr).Add(1)
|
2023-04-16 22:08:56 +08:00
|
|
|
p.incrementFails()
|
2018-02-15 10:21:57 +01:00
|
|
|
return err
|
2018-02-05 22:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
2018-02-15 10:21:57 +01:00
|
|
|
atomic.StoreUint32(&p.fails, 0)
|
|
|
|
|
return nil
|
2018-02-05 22:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
2018-07-09 15:14:55 +01:00
|
|
|
func (h *dnsHc) send(addr string) error {
|
|
|
|
|
ping := new(dns.Msg)
|
2022-04-13 00:39:48 +08:00
|
|
|
ping.SetQuestion(h.domain, dns.TypeNS)
|
2020-03-06 11:52:43 +01:00
|
|
|
ping.MsgHdr.RecursionDesired = h.recursionDesired
|
2018-02-05 22:00:47 +00:00
|
|
|
|
2018-07-09 15:14:55 +01:00
|
|
|
m, _, err := h.c.Exchange(ping, addr)
|
|
|
|
|
// If we got a header, we're alright, basically only care about I/O errors 'n stuff.
|
2018-02-05 22:00:47 +00:00
|
|
|
if err != nil && m != nil {
|
2018-07-09 15:14:55 +01:00
|
|
|
// Silly check, something sane came back.
|
2018-02-05 22:00:47 +00:00
|
|
|
if m.Response || m.Opcode == dns.OpcodeQuery {
|
|
|
|
|
err = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|