cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2019.3.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tunneldns/https_proxy.go

39lines · modecode

1package tunneldns
2
3import (
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
12type 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
17type ProxyPlugin struct {
18 Upstreams []Upstream
19 Next plugin.Handler
20}
21
22// ServeDNS implements interface for CoreDNS plugin
23func (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
39func (p ProxyPlugin) Name() string { return "proxy" }
40