-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitmask.go
138 lines (123 loc) · 2.82 KB
/
bitmask.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
/*
* Copyright (c) 2024 Johan Stenstam, johan.stenstam@internetstiftelsen.se
*/
package tapir
import (
"fmt"
"log"
"strings"
)
type TagMask uint32
const (
NewName TagMask = 1 << iota
HighVolume
BadIP
CdnTracker
LikelyMalware
LikelyBotnetCC
Foo
Bar
Baz
Gazonk
Frotz
)
var DefinedTags = []string{
"newname", "highvolume", "badip", "cdntracker", "likelymalware", "likelybotnetcc",
// "childporn",
"foo", "bar", "baz", "gazonk", "frotz",
}
func SetTag(b, tag TagMask) TagMask { return b | tag }
func ClearTag(b, tag TagMask) TagMask { return b &^ tag }
func ToggleTag(b, tag TagMask) TagMask { return b ^ tag }
func HasTag(b, tag TagMask) bool { return b&tag != 0 }
func (tm *TagMask) SetTag(tag TagMask) { *tm = *tm | tag }
func (tm *TagMask) ClearTag(tag TagMask) { *tm = *tm &^ tag }
func (tm *TagMask) ToggleTag(tag TagMask) { *tm = *tm ^ tag }
func (tm *TagMask) HasTag(tag TagMask) bool { return *tm&tag != 0 }
func (tm *TagMask) NumTags() int {
var res int
for i := 0; i < 32; i++ {
if tm.HasTag(TagMask(1 << i)) {
// fmt.Printf("Bit %d is set\n", i)
res++
}
}
return res
}
func StringsToTagMask(ss []string) (TagMask, error) {
var res TagMask
for _, s := range ss {
switch strings.ToLower(s) {
case "newname":
res.SetTag(NewName)
case "highvolume":
res.SetTag(HighVolume)
case "badip":
res.SetTag(BadIP)
case "cdntracker":
res.SetTag(CdnTracker)
case "likelymalware":
res.SetTag(LikelyMalware)
case "likelybotnetcc":
res.SetTag(LikelyBotnetCC)
// case "childporn":
// res.SetTag(ChildPorn)
case "foo":
res.SetTag(Foo)
case "bar":
res.SetTag(Bar)
case "baz":
res.SetTag(Baz)
case "gazonk":
res.SetTag(Gazonk)
case "frotz":
res.SetTag(Frotz)
default:
log.Printf("Error: unknown tag: \"%s\"", s)
return res, fmt.Errorf("unknown tapir tag: \"%s\"", s)
}
}
return res, nil
}
type Action uint8
const (
NXDOMAIN Action = 1 << iota
NODATA
DROP
REDIRECT
ALLOWLIST
PASSTHRU
UnknownAction
)
func (tn *TapirName) HasAction(action Action) bool { return tn.Action&action != 0 }
func StringToAction(s string) (Action, error) {
switch strings.ToLower(s) {
case "allowlist", "passthru":
return ALLOWLIST, nil
case "nxdomain":
return NXDOMAIN, nil
case "nodata":
return NODATA, nil
case "drop":
return DROP, nil
case "redirect":
return REDIRECT, nil
default:
log.Printf("Error: unknown RPZ action: \"%s\"", s)
return 0, fmt.Errorf("unknown tapir RPZ action: \"%s\"", s)
}
}
var ActionToCNAMETarget = map[Action]string{
NXDOMAIN: ".",
NODATA: "*.",
DROP: "rpz-drop.",
ALLOWLIST: "rpz-passthru.",
REDIRECT: "what-to-do-about-this",
}
var ActionToString = map[Action]string{
NXDOMAIN: "NXDOMAIN",
NODATA: "NODATA",
DROP: "DROP",
ALLOWLIST: "ALLOWLIST",
REDIRECT: "WHAT-TO-DO-ABOUT-REDIRECTS",
}