mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-04 10:11:10 +01:00 
			
		
		
		
	* plugin/forward: add it This moves coredns/forward into CoreDNS. Fixes as a few bugs, adds a policy option and more tests to the plugin. Update the documentation, test IPv6 address and add persistent tests. * Always use random policy when spraying * include scrub fix here as well * use correct var name * Code review * go vet * Move logging to metrcs * Small readme updates * Fix readme
		
			
				
	
	
		
			78 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			78 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
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
 | 
						|
)
 |