aboutsummaryrefslogtreecommitdiff
path: root/plugin/forward/proxy.go
blob: c89490374ebf0f948db90ea5c94a6b5d44685942 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package forward

import (
	"crypto/tls"
	"sync"
	"time"

	"github.com/miekg/dns"
)

// Proxy defines an upstream host.
type Proxy struct {
	host *host

	transport *transport

	// copied from Forward.
	hcInterval time.Duration
	forceTCP   bool

	stop chan bool

	sync.RWMutex
}

// NewProxy returns a new proxy.
func NewProxy(addr string) *Proxy {
	host := newHost(addr)

	p := &Proxy{
		host:       host,
		hcInterval: hcDuration,
		stop:       make(chan bool),
		transport:  newTransport(host),
	}
	return p
}

// SetTLSConfig sets the TLS config in the lower p.host.
func (p *Proxy) SetTLSConfig(cfg *tls.Config) { p.host.tlsConfig = cfg }

// SetExpire sets the expire duration in the lower p.host.
func (p *Proxy) SetExpire(expire time.Duration) { p.host.expire = expire }

func (p *Proxy) close() { p.stop <- true }

// Dial connects to the host in p with the configured transport.
func (p *Proxy) Dial(proto string) (*dns.Conn, error) { return p.transport.Dial(proto) }

// Yield returns the connection to the pool.
func (p *Proxy) Yield(c *dns.Conn) { p.transport.Yield(c) }

// Down returns if this proxy is up or down.
func (p *Proxy) Down(maxfails uint32) bool { return p.host.down(maxfails) }

func (p *Proxy) healthCheck() {

	// stop channel
	p.host.SetClient()

	p.host.Check()
	tick := time.NewTicker(p.hcInterval)
	for {
		select {
		case <-tick.C:
			p.host.Check()
		case <-p.stop:
			return
		}
	}
}

const (
	dialTimeout = 4 * time.Second
	timeout     = 2 * time.Second
	hcDuration  = 500 * time.Millisecond
)