forked from networkservicemesh/fanout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
123 lines (111 loc) · 2.9 KB
/
client.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
// Copyright (c) 2020 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fanout
import (
"context"
"crypto/tls"
"fmt"
"time"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
ot "github.com/opentracing/opentracing-go"
otext "github.com/opentracing/opentracing-go/ext"
)
// Client represents the proxy for remote DNS server
type Client interface {
Request(context.Context, *request.Request) (*dns.Msg, error)
Endpoint() string
SetTLSConfig(*tls.Config)
}
type client struct {
transport Transport
addr string
net string
}
// NewClient creates new client with specific addr and network
func NewClient(addr, net string) Client {
a := &client{
addr: addr,
net: net,
transport: NewTransport(addr),
}
return a
}
// SetTLSConfig sets tls config for client
func (c *client) SetTLSConfig(cfg *tls.Config) {
if cfg != nil {
c.net = tcptls
}
c.transport.SetTLSConfig(cfg)
}
// Endpoint returns address of DNS server
func (c *client) Endpoint() string {
return c.addr
}
// Request sends request to DNS server
func (c *client) Request(ctx context.Context, r *request.Request) (*dns.Msg, error) {
span := ot.SpanFromContext(ctx)
if span != nil {
childSpan := span.Tracer().StartSpan("request", ot.ChildOf(span.Context()))
otext.PeerAddress.Set(childSpan, c.addr)
ctx = ot.ContextWithSpan(ctx, childSpan)
defer childSpan.Finish()
}
start := time.Now()
conn, err := c.transport.Dial(ctx, c.net)
if err != nil {
return nil, err
}
//Set buffer size correctly for this conn.
conn.UDPSize = uint16(r.Size())
if conn.UDPSize < 512 {
conn.UDPSize = 512
}
defer func() {
_ = conn.Close()
}()
go func() {
<-ctx.Done()
_ = conn.Close()
}()
if err = conn.SetWriteDeadline(time.Now().Add(maxTimeout)); err != nil {
return nil, err
}
if err = conn.WriteMsg(r.Req); err != nil {
return nil, err
}
if err = conn.SetReadDeadline(time.Now().Add(readTimeout)); err != nil {
return nil, err
}
var ret *dns.Msg
for {
ret, err = conn.ReadMsg()
if err != nil {
return nil, err
}
if r.Req.Id == ret.Id {
break
}
}
rc, ok := dns.RcodeToString[ret.Rcode]
if !ok {
rc = fmt.Sprint(ret.Rcode)
}
RequestCount.WithLabelValues(c.addr).Add(1)
RcodeCount.WithLabelValues(rc, c.addr).Add(1)
RequestDuration.WithLabelValues(c.addr).Observe(time.Since(start).Seconds())
return ret, nil
}