-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresolver_doh_json.go
198 lines (187 loc) · 4.73 KB
/
resolver_doh_json.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/miekg/dns"
"net"
"net/http"
"net/url"
"strings"
"time"
)
var Quad9JsonEndpoints = []string{
"https://149.112.112.11:5053/dns-query",
"https://9.9.9.11:5053/dns-query",
}
type DohJsonResolver struct {
httpClient *http.Client
cache Cache
cacheType string
useCache bool
endpoints []string
nextEndpoint func() string
}
func NewDohJsonResolver(endpoints []string, useCache bool, cacheOptions *CacheOptions) (rsv *DohJsonResolver) {
httpTransport_ := &http.Transport{
Proxy: nil,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 3 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
if ExecConfig.UpstreamHostResolver != "" {
url_, err := url.Parse(strings.TrimSpace(ExecConfig.UpstreamHostResolver))
if err == nil {
if ListenAddrPortAvailable(url_.Host) {
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
dialer := &net.Dialer{
Resolver: &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Duration(5000) * time.Millisecond,
}
return d.DialContext(ctx, url_.Scheme, url_.Host)
},
},
}
return dialer.DialContext(ctx, network, addr)
}
httpTransport_.DialContext = dialContext
}
}
}
rsv = &DohJsonResolver{
httpClient: &http.Client{
Transport: httpTransport_,
},
useCache: useCache,
endpoints: endpoints,
}
rsv.nextEndpoint = func() func() string {
initV_ := 0
if len(rsv.endpoints) == 1 {
return func() string {
return rsv.endpoints[0]
}
} else {
return func() string {
ret_ := rsv.endpoints[initV_]
initV_ = (initV_ + 1) % len(rsv.endpoints)
return ret_
}
}
}()
// If using cache
if rsv.useCache {
if cacheOptions.cacheType == CacheTypeInternal {
rsv.cache = NewCacheInternal()
rsv.cacheType = CacheTypeInternal
}
// TODO: redis cache type
}
return
}
func (rsv *DohJsonResolver) IsUsingCache() bool {
return rsv.useCache
}
func (rsv *DohJsonResolver) GetCache(key string) (rsp ResolverRsp, ok bool) {
cacheItem_, ok := rsv.cache.Get(key)
if !ok {
return nil, false
}
if rsv.cacheType == CacheTypeInternal {
return cacheItem_.(*RspCacheItem).ResolverResponse, true
} else {
// TODO: redis cache type
return nil, false
}
}
func (rsv *DohJsonResolver) SetCache(key string, value *RspCacheItem, ttl uint32) {
rsv.cache.Set(key, value, ttl)
}
// Query Dns over HTTPS endpoint.
func (rsv *DohJsonResolver) Query(qName string, qType uint16, ecsIPs string) (
rsp ResolverRsp, err error) {
return CommonResolverQuery(rsv, qName, qType, ecsIPs)
}
func (rsv *DohJsonResolver) Resolve(qName string, qType uint16, ecsIP *net.IP) (
rsp ResolverRsp, err error) {
ecsP_ := fmt.Sprintf("")
if ecsIP != nil {
ecsP_ = fmt.Sprintf("&edns_client_subnet=%s", ecsIP.String())
}
urlStr_ := fmt.Sprintf("%s?name=%s&type=%d&do=1%s&random_padding=%d",
rsv.nextEndpoint(), qName, qType, ecsP_, time.Now().Nanosecond())
url_, err := url.Parse(urlStr_)
if err != nil {
log.Error(err)
return
}
httpReq_ := &http.Request{
URL: url_,
Header: map[string][]string{"Accept": {"application/dns-message"}},
}
defer func() {
httpReq_.URL = nil
httpReq_.Header = nil
httpReq_ = nil
}()
httpRsp_, err := rsv.httpClient.Do(httpReq_)
defer func() {
if httpRsp_ != nil && httpRsp_.Body != nil {
_ = httpRsp_.Body.Close()
}
}()
if err != nil {
log.Error(err)
return
}
if httpRsp_.StatusCode >= 400 {
err = fmt.Errorf("got status code: %d", httpRsp_.StatusCode)
log.Error(err)
return nil, err
}
jsonRsp_ := new(DohJsonResolverRsp)
decoder_ := json.NewDecoder(httpRsp_.Body)
err = decoder_.Decode(&jsonRsp_)
if err != nil {
log.Error(err)
return
}
if jsonRsp_.Status != 0 {
log.Warnf("response status is not 0: %+v", rsp)
} else {
log.Tracef("json response: %+v", jsonRsp_)
}
if len(jsonRsp_.Question) == 1 {
log.Infof("got reply to question: %s %s", jsonRsp_.Question[0].Name,
dns.TypeToString[jsonRsp_.Question[0].Type])
}
for _, r_ := range jsonRsp_.Answer {
if rr_, err := r_.RR(); err != nil {
log.Error(err)
return nil, err
} else {
log.Debugf("answer record: %s", rr_)
}
}
for _, r_ := range jsonRsp_.Authority {
if rr_, err := r_.RR(); err != nil {
log.Error(err)
} else {
log.Debugf("authority record: %s", rr_)
}
}
for _, r_ := range jsonRsp_.Additional {
if rr_, err := r_.RR(); err != nil {
log.Error(err)
} else {
log.Debugf("extra record: %s", rr_)
}
}
jsonRsp_.UnixTSOfArrival_ = time.Now().Unix()
return jsonRsp_, nil
}