2022-11-03 18:39:37 +01:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-05-26 17:13:59 +02:00
|
|
|
"fmt"
|
2022-11-03 18:39:37 +01:00
|
|
|
"net"
|
2023-02-13 15:25:11 +01:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2022-11-03 18:39:37 +01:00
|
|
|
"time"
|
2023-02-13 15:25:11 +01:00
|
|
|
|
2023-05-26 17:13:59 +02:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2023-02-13 15:25:11 +01:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-11-03 18:39:37 +01:00
|
|
|
)
|
|
|
|
|
2023-02-13 15:25:11 +01:00
|
|
|
const (
|
2023-05-26 17:13:59 +02:00
|
|
|
failsTillDeact = int32(5)
|
|
|
|
reactivatePeriod = 30 * time.Second
|
2023-02-13 15:25:11 +01:00
|
|
|
upstreamTimeout = 15 * time.Second
|
|
|
|
)
|
2022-11-03 18:39:37 +01:00
|
|
|
|
2023-05-26 17:13:59 +02:00
|
|
|
type upstreamClient interface {
|
|
|
|
ExchangeContext(ctx context.Context, m *dns.Msg, a string) (r *dns.Msg, rtt time.Duration, err error)
|
|
|
|
}
|
|
|
|
|
2022-11-03 18:39:37 +01:00
|
|
|
type upstreamResolver struct {
|
2023-02-13 15:25:11 +01:00
|
|
|
ctx context.Context
|
2023-05-26 17:13:59 +02:00
|
|
|
cancel context.CancelFunc
|
|
|
|
upstreamClient upstreamClient
|
2023-02-13 15:25:11 +01:00
|
|
|
upstreamServers []string
|
|
|
|
disabled bool
|
|
|
|
failsCount atomic.Int32
|
|
|
|
failsTillDeact int32
|
|
|
|
mutex sync.Mutex
|
|
|
|
reactivatePeriod time.Duration
|
|
|
|
upstreamTimeout time.Duration
|
|
|
|
|
|
|
|
deactivate func()
|
|
|
|
reactivate func()
|
|
|
|
}
|
|
|
|
|
2023-05-26 17:13:59 +02:00
|
|
|
func newUpstreamResolver(parentCTX context.Context) *upstreamResolver {
|
|
|
|
ctx, cancel := context.WithCancel(parentCTX)
|
2023-02-13 15:25:11 +01:00
|
|
|
return &upstreamResolver{
|
|
|
|
ctx: ctx,
|
2023-05-26 17:13:59 +02:00
|
|
|
cancel: cancel,
|
2023-02-13 15:25:11 +01:00
|
|
|
upstreamClient: &dns.Client{},
|
|
|
|
upstreamTimeout: upstreamTimeout,
|
|
|
|
reactivatePeriod: reactivatePeriod,
|
|
|
|
failsTillDeact: failsTillDeact,
|
|
|
|
}
|
2022-11-03 18:39:37 +01:00
|
|
|
}
|
|
|
|
|
2023-05-26 17:13:59 +02:00
|
|
|
func (u *upstreamResolver) stop() {
|
|
|
|
log.Debugf("stoping serving DNS for upstreams %s", u.upstreamServers)
|
|
|
|
u.cancel()
|
|
|
|
}
|
|
|
|
|
2022-11-03 18:39:37 +01:00
|
|
|
// ServeDNS handles a DNS request
|
|
|
|
func (u *upstreamResolver) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
|
2023-02-13 15:25:11 +01:00
|
|
|
defer u.checkUpstreamFails()
|
2022-11-03 18:39:37 +01:00
|
|
|
|
2023-02-13 15:25:11 +01:00
|
|
|
log.WithField("question", r.Question[0]).Trace("received an upstream question")
|
2022-11-03 18:39:37 +01:00
|
|
|
|
|
|
|
select {
|
2023-02-13 15:25:11 +01:00
|
|
|
case <-u.ctx.Done():
|
2022-11-03 18:39:37 +01:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, upstream := range u.upstreamServers {
|
2023-02-13 15:25:11 +01:00
|
|
|
ctx, cancel := context.WithTimeout(u.ctx, u.upstreamTimeout)
|
2022-11-03 18:39:37 +01:00
|
|
|
rm, t, err := u.upstreamClient.ExchangeContext(ctx, r, upstream)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == context.DeadlineExceeded || isTimeout(err) {
|
2023-02-13 15:25:11 +01:00
|
|
|
log.WithError(err).WithField("upstream", upstream).
|
|
|
|
Warn("got an error while connecting to upstream")
|
2022-11-03 18:39:37 +01:00
|
|
|
continue
|
|
|
|
}
|
2023-02-13 15:25:11 +01:00
|
|
|
u.failsCount.Add(1)
|
|
|
|
log.WithError(err).WithField("upstream", upstream).
|
|
|
|
Error("got an error while querying the upstream")
|
2022-11-03 18:39:37 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("took %s to query the upstream %s", t, upstream)
|
|
|
|
|
|
|
|
err = w.WriteMsg(rm)
|
|
|
|
if err != nil {
|
2023-02-13 15:25:11 +01:00
|
|
|
log.WithError(err).Error("got an error while writing the upstream resolver response")
|
|
|
|
}
|
|
|
|
// count the fails only if they happen sequentially
|
|
|
|
u.failsCount.Store(0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
u.failsCount.Add(1)
|
|
|
|
log.Error("all queries to the upstream nameservers failed with timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkUpstreamFails counts fails and disables or enables upstream resolving
|
|
|
|
//
|
|
|
|
// If fails count is greater that failsTillDeact, upstream resolving
|
|
|
|
// will be disabled for reactivatePeriod, after that time period fails counter
|
|
|
|
// will be reset and upstream will be reactivated.
|
|
|
|
func (u *upstreamResolver) checkUpstreamFails() {
|
|
|
|
u.mutex.Lock()
|
|
|
|
defer u.mutex.Unlock()
|
|
|
|
|
|
|
|
if u.failsCount.Load() < u.failsTillDeact || u.disabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-u.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
log.Warnf("upstream resolving is disabled for %v", reactivatePeriod)
|
|
|
|
u.deactivate()
|
|
|
|
u.disabled = true
|
2023-05-26 17:13:59 +02:00
|
|
|
go u.waitUntilResponse()
|
2023-02-13 15:25:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 17:13:59 +02:00
|
|
|
// waitUntilResponse retries, in an exponential interval, querying the upstream servers until it gets a positive response
|
|
|
|
func (u *upstreamResolver) waitUntilResponse() {
|
|
|
|
exponentialBackOff := &backoff.ExponentialBackOff{
|
|
|
|
InitialInterval: 500 * time.Millisecond,
|
|
|
|
RandomizationFactor: 0.5,
|
|
|
|
Multiplier: 1.1,
|
|
|
|
MaxInterval: u.reactivatePeriod,
|
|
|
|
MaxElapsedTime: 0,
|
|
|
|
Stop: backoff.Stop,
|
|
|
|
Clock: backoff.SystemClock,
|
|
|
|
}
|
|
|
|
|
|
|
|
r := new(dns.Msg).SetQuestion("netbird.io.", dns.TypeA)
|
|
|
|
|
|
|
|
operation := func() error {
|
|
|
|
select {
|
|
|
|
case <-u.ctx.Done():
|
|
|
|
return backoff.Permanent(fmt.Errorf("exiting upstream retry loop for upstreams %s: parent context has been canceled", u.upstreamServers))
|
|
|
|
default:
|
2022-11-03 18:39:37 +01:00
|
|
|
}
|
2023-02-13 15:25:11 +01:00
|
|
|
|
2023-05-26 17:13:59 +02:00
|
|
|
var err error
|
|
|
|
for _, upstream := range u.upstreamServers {
|
|
|
|
ctx, cancel := context.WithTimeout(u.ctx, u.upstreamTimeout)
|
|
|
|
_, _, err = u.upstreamClient.ExchangeContext(ctx, r, upstream)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("checking connectivity with upstreams %s failed with error: %s. Retrying in %s", err, u.upstreamServers, exponentialBackOff.NextBackOff())
|
|
|
|
return fmt.Errorf("got an error from upstream check call")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := backoff.Retry(operation, exponentialBackOff)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
2022-11-03 18:39:37 +01:00
|
|
|
return
|
|
|
|
}
|
2023-05-26 17:13:59 +02:00
|
|
|
|
|
|
|
log.Infof("upstreams %s are responsive again. Adding them back to system", u.upstreamServers)
|
|
|
|
u.failsCount.Store(0)
|
|
|
|
u.reactivate()
|
|
|
|
u.disabled = false
|
2022-11-03 18:39:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// isTimeout returns true if the given error is a network timeout error.
|
|
|
|
//
|
|
|
|
// Copied from k8s.io/apimachinery/pkg/util/net.IsTimeout
|
|
|
|
func isTimeout(err error) bool {
|
|
|
|
var neterr net.Error
|
|
|
|
if errors.As(err, &neterr) {
|
|
|
|
return neterr != nil && neterr.Timeout()
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|