mirror of
https://github.com/coredns/coredns.git
synced 2025-12-15 14:55:14 -05:00
middleware/httpproxy: Add (#439)
This PR adds a middleware that talks to dns.google.com over HTTPS, meaning all your DNS traffic is encrypted when traversing your ISP and the internet. The `dns.google.com` address is re-resolved every 30 seconds.
This commit is contained in:
92
middleware/httpproxy/upstream.go
Normal file
92
middleware/httpproxy/upstream.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package httpproxy
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/coredns/middleware/pkg/dnsutil"
|
||||
"github.com/miekg/coredns/middleware/proxy"
|
||||
)
|
||||
|
||||
type simpleUpstream struct {
|
||||
from string
|
||||
Hosts proxy.HostPool
|
||||
Policy proxy.Policy
|
||||
|
||||
FailTimeout time.Duration
|
||||
MaxFails int32
|
||||
}
|
||||
|
||||
// newSimpleUpstream return a new simpleUpstream initialized with the addresses.
|
||||
func newSimpleUpstream(hosts []string) (*simpleUpstream, error) {
|
||||
upstream := &simpleUpstream{
|
||||
Hosts: nil,
|
||||
Policy: &proxy.Random{},
|
||||
FailTimeout: 10 * time.Second,
|
||||
MaxFails: 3,
|
||||
}
|
||||
|
||||
toHosts, err := dnsutil.ParseHostPortOrFile(hosts...)
|
||||
if err != nil {
|
||||
return upstream, err
|
||||
}
|
||||
|
||||
upstream.Hosts = make([]*proxy.UpstreamHost, len(toHosts))
|
||||
for i, host := range toHosts {
|
||||
uh := &proxy.UpstreamHost{
|
||||
Name: host,
|
||||
Conns: 0,
|
||||
Fails: 0,
|
||||
FailTimeout: upstream.FailTimeout,
|
||||
Unhealthy: false,
|
||||
|
||||
CheckDown: func(upstream *simpleUpstream) proxy.UpstreamHostDownFunc {
|
||||
return func(uh *proxy.UpstreamHost) bool {
|
||||
if uh.Unhealthy {
|
||||
return true
|
||||
}
|
||||
|
||||
fails := atomic.LoadInt32(&uh.Fails)
|
||||
if fails >= upstream.MaxFails && upstream.MaxFails != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}(upstream),
|
||||
}
|
||||
upstream.Hosts[i] = uh
|
||||
}
|
||||
return upstream, nil
|
||||
}
|
||||
|
||||
func (u *simpleUpstream) From() string { return u.from }
|
||||
func (u *simpleUpstream) Options() proxy.Options { return proxy.Options{} }
|
||||
func (u *simpleUpstream) IsAllowedPath(name string) bool { return true }
|
||||
|
||||
func (u *simpleUpstream) Select() *proxy.UpstreamHost {
|
||||
pool := u.Hosts
|
||||
if len(pool) == 1 {
|
||||
if pool[0].Down() {
|
||||
return nil
|
||||
}
|
||||
return pool[0]
|
||||
}
|
||||
allDown := true
|
||||
for _, host := range pool {
|
||||
if !host.Down() {
|
||||
allDown = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if allDown {
|
||||
return nil
|
||||
}
|
||||
|
||||
if u.Policy == nil {
|
||||
h := (&proxy.Random{}).Select(pool)
|
||||
return h
|
||||
}
|
||||
|
||||
h := u.Policy.Select(pool)
|
||||
return h
|
||||
}
|
||||
Reference in New Issue
Block a user