/
entry_handler.go
150 lines (131 loc) · 4.04 KB
/
entry_handler.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
/*
* Copyright (C) 2020-2022, IrineSistiana
*
* This file is part of mosdns.
*
* mosdns is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mosdns is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package dns_handler
import (
"context"
"errors"
"github.com/IrineSistiana/mosdns/v4/pkg/executable_seq"
"github.com/IrineSistiana/mosdns/v4/pkg/query_context"
"github.com/IrineSistiana/mosdns/v4/pkg/utils"
"github.com/miekg/dns"
"go.uber.org/zap"
"testing"
"time"
)
const (
defaultQueryTimeout = time.Second * 5
)
var (
nopLogger = zap.NewNop()
)
// Handler handles dns query.
type Handler interface {
// ServeDNS handles incoming request req and returns a response.
// Implements must not keep and use req after the ServeDNS returned.
// ServeDNS should handle dns errors by itself and return a proper error responses
// for clients.
// ServeDNS should always return a responses.
// If ServeDNS returns an error, caller considers that the error is associated
// with the downstream connection and will close the downstream connection
// immediately.
// All input parameters won't be nil.
ServeDNS(ctx context.Context, req *dns.Msg, meta *query_context.RequestMeta) (*dns.Msg, error)
}
type EntryHandlerOpts struct {
// Logger is used for logging. Default is a noop logger.
Logger *zap.Logger
Entry executable_seq.Executable
// QueryTimeout limits the timeout value of each query.
// Default is defaultQueryTimeout.
QueryTimeout time.Duration
// RecursionAvailable sets the dns.Msg.RecursionAvailable flag globally.
RecursionAvailable bool
}
func (opts *EntryHandlerOpts) Init() error {
if opts.Logger == nil {
opts.Logger = nopLogger
}
if opts.Entry == nil {
return errors.New("nil entry")
}
utils.SetDefaultNum(&opts.QueryTimeout, defaultQueryTimeout)
return nil
}
type EntryHandler struct {
opts EntryHandlerOpts
}
func NewEntryHandler(opts EntryHandlerOpts) (*EntryHandler, error) {
if err := opts.Init(); err != nil {
return nil, err
}
return &EntryHandler{opts: opts}, nil
}
// ServeDNS implements Handler.
// If entry returns an error, a SERVFAIL response will be returned.
// If entry returns without a response, a REFUSED response will be returned.
func (h *EntryHandler) ServeDNS(ctx context.Context, req *dns.Msg, meta *query_context.RequestMeta) (*dns.Msg, error) {
// apply timeout to ctx
ddl := time.Now().Add(h.opts.QueryTimeout)
ctxDdl, ok := ctx.Deadline()
if !(ok && ctxDdl.Before(ddl)) {
newCtx, cancel := context.WithDeadline(ctx, ddl)
defer cancel()
ctx = newCtx
}
// exec entry
qCtx := query_context.NewContext(req, meta)
err := h.opts.Entry.Exec(ctx, qCtx, nil)
respMsg := qCtx.R()
if err != nil {
h.opts.Logger.Warn("entry returned an err", qCtx.InfoField(), zap.Error(err))
} else {
h.opts.Logger.Debug("entry returned", qCtx.InfoField())
}
if err == nil && respMsg == nil {
h.opts.Logger.Error("entry returned an nil response", qCtx.InfoField())
}
if respMsg == nil || err != nil {
respMsg = new(dns.Msg)
respMsg.SetReply(req)
respMsg.Rcode = dns.RcodeServerFailure
}
if h.opts.RecursionAvailable {
respMsg.RecursionAvailable = true
}
return respMsg, nil
}
type DummyServerHandler struct {
T *testing.T
WantMsg *dns.Msg
WantErr error
}
func (d *DummyServerHandler) ServeDNS(_ context.Context, req *dns.Msg, meta *query_context.RequestMeta) (*dns.Msg, error) {
if d.WantErr != nil {
return nil, d.WantErr
}
var resp *dns.Msg
if d.WantMsg != nil {
resp = d.WantMsg.Copy()
resp.Id = req.Id
} else {
resp = new(dns.Msg)
resp.SetReply(req)
}
return resp, nil
}