https_proxy.go 993 B

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