-
Notifications
You must be signed in to change notification settings - Fork 8
/
grpc.go
143 lines (113 loc) · 3.02 KB
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package grpc
import (
"context"
"crypto/tls"
"errors"
"time"
"github.com/inverse-inc/wireguard-go/dns/plugin"
"github.com/inverse-inc/wireguard-go/dns/plugin/debug"
"github.com/inverse-inc/wireguard-go/dns/request"
"github.com/miekg/dns"
ot "github.com/opentracing/opentracing-go"
)
// GRPC represents a plugin instance that can proxy requests to another (DNS) server via gRPC protocol.
// It has a list of proxies each representing one upstream proxy.
type GRPC struct {
proxies []*Proxy
p Policy
from string
ignored []string
tlsConfig *tls.Config
tlsServerName string
Next plugin.Handler
}
// ServeDNS implements the plugin.Handler interface.
func (g *GRPC) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}
if !g.match(state) {
return plugin.NextOrFailure(g.Name(), g.Next, ctx, w, r)
}
var (
span, child ot.Span
ret *dns.Msg
upstreamErr, err error
i int
)
span = ot.SpanFromContext(ctx)
list := g.list()
deadline := time.Now().Add(defaultTimeout)
for time.Now().Before(deadline) {
if i >= len(list) {
// reached the end of list without any answer
if ret != nil {
// write empty response and finish
w.WriteMsg(ret)
}
break
}
proxy := list[i]
i++
if span != nil {
child = span.Tracer().StartSpan("query", ot.ChildOf(span.Context()))
ctx = ot.ContextWithSpan(ctx, child)
}
ret, err = proxy.query(ctx, r)
if err != nil {
// Continue with the next proxy
continue
}
if child != nil {
child.Finish()
}
upstreamErr = err
// Check if the reply is correct; if not return FormErr.
if !state.Match(ret) {
debug.Hexdumpf(ret, "Wrong reply for id: %d, %s %d", ret.Id, state.QName(), state.QType())
formerr := new(dns.Msg)
formerr.SetRcode(state.Req, dns.RcodeFormatError)
w.WriteMsg(formerr)
return 0, nil
}
w.WriteMsg(ret)
return 0, nil
}
if upstreamErr != nil {
return dns.RcodeServerFailure, upstreamErr
}
return dns.RcodeServerFailure, ErrNoHealthy
}
// NewGRPC returns a new GRPC.
func newGRPC() *GRPC {
g := &GRPC{
p: new(random),
}
return g
}
// Name implements the Handler interface.
func (g *GRPC) Name() string { return "grpc" }
// Len returns the number of configured proxies.
func (g *GRPC) len() int { return len(g.proxies) }
func (g *GRPC) match(state request.Request) bool {
if !plugin.Name(g.from).Matches(state.Name()) || !g.isAllowedDomain(state.Name()) {
return false
}
return true
}
func (g *GRPC) isAllowedDomain(name string) bool {
if dns.Name(name) == dns.Name(g.from) {
return true
}
for _, ignore := range g.ignored {
if plugin.Name(ignore).Matches(name) {
return false
}
}
return true
}
// List returns a set of proxies to be used for this client depending on the policy in p.
func (g *GRPC) list() []*Proxy { return g.p.List(g.proxies) }
const defaultTimeout = 5 * time.Second
var (
// ErrNoHealthy means no healthy proxies left.
ErrNoHealthy = errors.New("no healthy gRPC proxies")
)