-
Notifications
You must be signed in to change notification settings - Fork 51
/
helpers.go
53 lines (39 loc) · 956 Bytes
/
helpers.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
package ipsetmanager
import (
"strings"
)
func addToIPset(set Ipset, data string) error {
// ipset can not program this rule
if data == IPv4DefaultIP {
if err := addToIPset(set, "0.0.0.0/1"); err != nil {
return err
}
return addToIPset(set, "128.0.0.0/1")
}
// ipset can not program this rule
if data == IPv6DefaultIP {
if err := addToIPset(set, "::/1"); err != nil {
return err
}
return addToIPset(set, "8000::/1")
}
if strings.HasPrefix(data, "!") {
return set.AddOption(data[1:], "nomatch", 0)
}
return set.Add(data, 0)
}
func delFromIPset(set Ipset, data string) error {
if data == IPv4DefaultIP {
if err := delFromIPset(set, "0.0.0.0/1"); err != nil {
return err
}
return delFromIPset(set, "128.0.0.0/1")
}
if data == IPv6DefaultIP {
if err := delFromIPset(set, "::/1"); err != nil {
return err
}
return delFromIPset(set, "8000::/1")
}
return set.Del(strings.TrimPrefix(data, "!"))
}