cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
tunneldns/https_proxy.go
38lines · modeblame
d06fc520Areg Harutyunyan8 years ago | 1 | package tunneldns |
| 2 | | |
| 3 | import ( | |
| 4 | "github.com/coredns/coredns/plugin" | |
| 5 | "github.com/miekg/dns" | |
| 6 | "github.com/pkg/errors" | |
| 7 | "golang.org/x/net/context" | |
| 8 | ) | |
| 9 | | |
| 10 | // Upstream is a simplified interface for proxy destination | |
| 11 | type Upstream interface { | |
| 12 | Exchange(ctx context.Context, query *dns.Msg) (*dns.Msg, error) | |
| 13 | } | |
| 14 | | |
| 15 | // ProxyPlugin is a simplified DNS proxy using a generic upstream interface | |
| 16 | type ProxyPlugin struct { | |
| 17 | Upstreams []Upstream | |
| 18 | Next plugin.Handler | |
| 19 | } | |
| 20 | | |
| 21 | // ServeDNS implements interface for CoreDNS plugin | |
| 22 | func (p ProxyPlugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | |
| 23 | var reply *dns.Msg | |
| 24 | var backendErr error | |
| 25 | | |
| 26 | for _, upstream := range p.Upstreams { | |
| 27 | reply, backendErr = upstream.Exchange(ctx, r) | |
| 28 | if backendErr == nil { | |
| 29 | w.WriteMsg(reply) | |
| 30 | return 0, nil | |
| 31 | } | |
| 32 | } | |
| 33 | | |
| 34 | return dns.RcodeServerFailure, errors.Wrap(backendErr, "failed to contact any of the upstreams") | |
| 35 | } | |
| 36 | | |
| 37 | // Name implements interface for CoreDNS plugin | |
| 38 | func (p ProxyPlugin) Name() string { return "proxy" } |