-
Notifications
You must be signed in to change notification settings - Fork 339
/
response_matcher.go
97 lines (80 loc) · 2.89 KB
/
response_matcher.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
// Copyright (C) 2020-2021, 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 responsematcher
import (
"context"
"fmt"
"github.com/IrineSistiana/mosdns/dispatcher/handler"
"github.com/IrineSistiana/mosdns/dispatcher/matcher/domain"
"github.com/IrineSistiana/mosdns/dispatcher/matcher/elem"
"github.com/IrineSistiana/mosdns/dispatcher/matcher/netlist"
"github.com/IrineSistiana/mosdns/dispatcher/utils"
"github.com/miekg/dns"
)
const PluginType = "response_matcher"
func init() {
handler.RegInitFunc(PluginType, Init, func() interface{} { return new(Args) })
handler.MustRegPlugin(preset(handler.NewBP("_response_rcode_success", PluginType), &Args{Rcode: []int{dns.RcodeSuccess}}), true)
}
var _ handler.MatcherPlugin = (*responseMatcher)(nil)
type Args struct {
Rcode []int `yaml:"rcode"`
IP []string `yaml:"ip"` // ip files
CNAME []string `yaml:"cname"` // domain files
IsLogicalAND bool `yaml:"logical_and"`
}
type responseMatcher struct {
*handler.BP
args *Args
matcherGroup []handler.Matcher
}
func (m *responseMatcher) Match(ctx context.Context, qCtx *handler.Context) (matched bool, err error) {
return utils.BoolLogic(ctx, qCtx, m.matcherGroup, m.args.IsLogicalAND)
}
func Init(bp *handler.BP, args interface{}) (p handler.Plugin, err error) {
return newResponseMatcher(bp, args.(*Args))
}
func newResponseMatcher(bp *handler.BP, args *Args) (m *responseMatcher, err error) {
m = new(responseMatcher)
m.BP = bp
m.args = args
if len(args.Rcode) > 0 {
m.matcherGroup = append(m.matcherGroup, newRCodeMatcher(elem.NewIntMatcher(args.Rcode)))
}
if len(args.CNAME) > 0 {
mixMatcher, err := domain.BatchLoadMixMatcherV2Matcher(args.CNAME)
if err != nil {
return nil, err
}
m.matcherGroup = append(m.matcherGroup, newCnameMatcher(mixMatcher))
}
if len(args.IP) > 0 {
ipMatcher, err := netlist.BatchLoad(args.IP)
if err != nil {
return nil, err
}
m.matcherGroup = append(m.matcherGroup, newResponseIPMatcher(ipMatcher))
}
return m, nil
}
func preset(bp *handler.BP, args *Args) (m *responseMatcher) {
m, err := newResponseMatcher(bp, args)
if err != nil {
panic(fmt.Sprintf("response_matcher: failed to init pre-set plugin %s: %s", bp.Tag(), err))
}
return m
}