-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.go
More file actions
185 lines (152 loc) · 5.09 KB
/
setup.go
File metadata and controls
185 lines (152 loc) · 5.09 KB
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
package carbolicacid
import (
"sync"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
)
func init() {
plugin.Register("carbolicacid", setup)
}
func setup(c *caddy.Controller) error {
cfg, err := parseConfig(c)
if err != nil {
return err
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return &CarbolicAcid{
Next: next,
cfg: cfg,
}
})
return nil
}
type ResponseAction int
type RuleKind int
const (
RulePreset RuleKind = iota
RuleInclude // 用于 block
)
const (
ActionDrop ResponseAction = iota
ActionServfail
ActionNxdomain
ActionBypass // v0.3.3: 透传但记录告警
)
// v0.3.3 新语法:结构化 preset/block 节点
type BlockNode struct {
Kind RuleKind // RulePreset 或 RuleInclude(block)
Value string // preset 名称或 CIDR
Excl []string // exclude 列表
}
type Config struct {
Action ResponseAction
// v0.3.3 新语法:结构化 block/preset
Blocks []*BlockNode
// 运行时结构:双表模型
blockList *IPSet
allowList *IPSet
initOnce sync.Once
initErr error
presetAllIP bool // 是否使用 preset allip }
}
func parseConfig(c *caddy.Controller) (*Config, error) {
cfg := &Config{
Action: ActionDrop,
}
for c.Next() {
for c.NextBlock() {
switch c.Val() {
// -------------------------
// preset iana { exclude ... }
// preset iana
// -------------------------
case "preset":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
name := args[0]
node := &BlockNode{
Kind: RulePreset,
Value: name,
}
// 尝试读取内层 block:preset NAME { ... }
if c.NextBlock() {
for {
switch c.Val() {
case "exclude":
exArgs := c.RemainingArgs()
if len(exArgs) != 1 {
return nil, c.ArgErr()
}
node.Excl = append(node.Excl, exArgs[0])
default:
return nil, c.Errf("unknown directive %q inside preset %q", c.Val(), name)
}
if !c.NextBlock() {
break
}
}
}
// 无内层 block → 等价于 preset name {}
cfg.Blocks = append(cfg.Blocks, node)
// -------------------------
// block CIDR { exclude ... }
// block CIDR
// -------------------------
case "block":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
cidr := args[0]
node := &BlockNode{
Kind: RuleInclude,
Value: cidr,
}
if c.NextBlock() {
for {
switch c.Val() {
case "exclude":
exArgs := c.RemainingArgs()
if len(exArgs) != 1 {
return nil, c.ArgErr()
}
node.Excl = append(node.Excl, exArgs[0])
default:
return nil, c.Errf("unknown directive %q inside block %q", c.Val(), cidr)
}
if !c.NextBlock() {
break
}
}
}
cfg.Blocks = append(cfg.Blocks, node)
// -------------------------
// responses drop|servfail|nxdomain|bypass
// -------------------------
case "responses":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
switch args[0] {
case "drop":
cfg.Action = ActionDrop
case "servfail":
cfg.Action = ActionServfail
case "nxdomain":
cfg.Action = ActionNxdomain
case "bypass":
cfg.Action = ActionBypass
default:
return nil, c.Errf("invalid responses action: %s", args[0])
}
default:
return nil, c.Errf("unknown directive: %s", c.Val())
}
}
}
return cfg, nil
}