-
Notifications
You must be signed in to change notification settings - Fork 2
/
addip_linux.go
47 lines (39 loc) · 871 Bytes
/
addip_linux.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
// +build linux
package ipset
import (
"errors"
"net"
goipset "github.com/digineo/go-ipset/v2"
"github.com/mdlayher/netlink"
"github.com/ti-mo/netfilter"
)
var (
c *goipset.Conn
)
func initLib() (err error) {
c, err = goipset.Dial(netfilter.ProtoUnspec, &netlink.Config{})
return
}
func addIP(ip net.IP, list string) error {
p, err := c.Header(list)
if err != nil {
return err
}
var typeMatch bool
if uint(p.Family.Value) == uint(netfilter.ProtoIPv4) {
typeMatch = ip.To4() != nil
} else if uint(p.Family.Value) == uint(netfilter.ProtoIPv6) {
typeMatch = ip.To16() != nil
}
if !typeMatch {
return errors.New("not matched type")
}
AddIPCount.WithLabelValues(list).Add(1)
return c.Add(list, goipset.NewEntry(goipset.EntryIP(ip)))
}
func flushSet(list string) error {
return c.Flush(list)
}
func shutdownLib() error {
return c.Close()
}