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