/
entry_handler.go
154 lines (130 loc) · 3.74 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
151
152
153
154
/*
* 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 server_handler
import (
"context"
"time"
"github.com/IrineSistiana/mosdns/v5/mlog"
"github.com/IrineSistiana/mosdns/v5/pkg/query_context"
"github.com/IrineSistiana/mosdns/v5/pkg/server"
"github.com/IrineSistiana/mosdns/v5/pkg/utils"
"github.com/IrineSistiana/mosdns/v5/plugin/executable/sequence"
"github.com/miekg/dns"
"go.uber.org/zap"
)
const (
defaultQueryTimeout = time.Second * 5
)
var (
nopLogger = mlog.Nop()
// options that can forward to upstream
queryForwardEDNS0Option = map[uint16]struct{}{
dns.EDNS0SUBNET: {},
}
// options that useless for downstream
respRemoveEDNS0Option = map[uint16]struct{}{
dns.EDNS0PADDING: {},
}
)
type EntryHandlerOpts struct {
// Logger is used for logging. Default is a noop logger.
Logger *zap.Logger
// Required.
Entry sequence.Executable
// QueryTimeout limits the timeout value of each query.
// Default is defaultQueryTimeout.
QueryTimeout time.Duration
}
func (opts *EntryHandlerOpts) init() {
if opts.Logger == nil {
opts.Logger = nopLogger
}
utils.SetDefaultNum(&opts.QueryTimeout, defaultQueryTimeout)
}
type EntryHandler struct {
opts EntryHandlerOpts
}
var _ server.Handler = (*EntryHandler)(nil)
func NewEntryHandler(opts EntryHandlerOpts) *EntryHandler {
opts.init()
return &EntryHandler{opts: opts}
}
// ServeDNS implements server.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) Handle(ctx context.Context, q *dns.Msg, serverMeta server.QueryMeta, packMsgPayload func(m *dns.Msg) (*[]byte, error)) *[]byte {
// basic query check.
if q.Response || len(q.Question) != 1 || len(q.Answer)+len(q.Ns) > 0 || len(q.Extra) > 1 {
return nil
}
ddl := time.Now().Add(h.opts.QueryTimeout)
ctx, cancel := context.WithDeadline(ctx, ddl)
defer cancel()
qCtx := query_context.NewContext(q)
qCtx.ServerMeta = serverMeta
// exec entry
err := h.opts.Entry.Exec(ctx, qCtx)
var resp *dns.Msg
if err != nil {
h.opts.Logger.Warn("entry err", qCtx.InfoField(), zap.Error(err))
resp = new(dns.Msg)
resp.SetReply(q)
resp.Rcode = dns.RcodeServerFailure
} else {
resp = qCtx.R()
}
if resp == nil {
resp = new(dns.Msg)
resp.SetReply(q)
resp.Rcode = dns.RcodeRefused
}
// We assume that our server is a forwarder.
resp.RecursionAvailable = true
// add respOpt back to resp
if respOpt := qCtx.RespOpt(); respOpt != nil {
resp.Extra = append(resp.Extra, respOpt)
}
if serverMeta.FromUDP {
udpSize := getValidUDPSize(qCtx.ClientOpt())
resp.Truncate(udpSize)
}
payload, err := packMsgPayload(resp)
if err != nil {
h.opts.Logger.Error("internal err: failed to pack resp msg", qCtx.InfoField(), zap.Error(err))
return nil
}
return payload
}
// opt can be nil.
func getValidUDPSize(opt *dns.OPT) int {
var s uint16
if opt != nil {
s = opt.UDPSize()
}
if s < dns.MinMsgSize {
s = dns.MinMsgSize
}
return int(s)
}
func newOpt() *dns.OPT {
opt := new(dns.OPT)
opt.Hdr.Name = "."
opt.Hdr.Rrtype = dns.TypeOPT
return opt
}