-
Notifications
You must be signed in to change notification settings - Fork 12
/
ipset_linux.go
85 lines (67 loc) · 2.57 KB
/
ipset_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
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
package ipset
import (
"net/netip"
"github.com/nadoo/ipset/internal/netlink"
)
var nl *netlink.NetLink
// Option is used to set parameters of ipset operations.
type Option = netlink.Option
// OptIPv6 sets `family inet6` parameter to operations.
func OptIPv6() Option { return func(opts *netlink.Options) { opts.IPv6 = true } }
// OptTimeout sets `timeout xx` parameter to operations.
func OptTimeout(timeout uint32) Option { return func(opts *netlink.Options) { opts.Timeout = timeout } }
func OptExcl() Option { return func(opts *netlink.Options) { opts.Excl = true } }
// Init prepares a netlink socket of ipset.
func Init() (err error) {
nl, err = netlink.New()
return err
}
// Create creates a new set.
func Create(setName string, opts ...Option) (err error) {
return nl.CreateSet(setName, opts...)
}
// Destroy destroys a named set.
func Destroy(setName string) (err error) {
return nl.DestroySet(setName)
}
// Flush flushes a named set.
func Flush(setName string) (err error) {
return nl.FlushSet(setName)
}
// Add adds an entry to the named set.
// entry could be: "1.1.1.1" or "192.168.1.0/24" or "2022::1" or "2022::1/32".
func Add(setName, entry string, opts ...Option) (err error) {
return handleEntry(netlink.IPSET_CMD_ADD, setName, entry, opts...)
}
// Del deletes an entry from the named set.
// entry could be: "1.1.1.1" or "192.168.1.0/24" or "2022::1" or "2022::1/32".
func Del(setName, entry string) (err error) {
return handleEntry(netlink.IPSET_CMD_DEL, setName, entry)
}
func handleEntry(cmd int, setName, entry string, opts ...Option) error {
ip, err := netip.ParseAddr(entry)
if err == nil {
return nl.HandleAddr(cmd, setName, ip, netip.Prefix{}, opts...)
}
cidr, err := netip.ParsePrefix(entry)
if err == nil {
return nl.HandleAddr(cmd, setName, cidr.Addr(), cidr, opts...)
}
return err
}
// AddAddr adds an addr to the named set.
func AddAddr(setName string, ip netip.Addr, opts ...Option) (err error) {
return nl.HandleAddr(netlink.IPSET_CMD_ADD, setName, ip, netip.Prefix{}, opts...)
}
// DelAddr deletes an addr from the named set.
func DelAddr(setName string, ip netip.Addr) (err error) {
return nl.HandleAddr(netlink.IPSET_CMD_DEL, setName, ip, netip.Prefix{})
}
// AddPrefix adds a cidr to the named set.
func AddPrefix(setName string, cidr netip.Prefix, opts ...Option) (err error) {
return nl.HandleAddr(netlink.IPSET_CMD_ADD, setName, cidr.Addr(), cidr, opts...)
}
// DelPrefix deletes a cidr from the named set.
func DelPrefix(setName string, cidr netip.Prefix) (err error) {
return nl.HandleAddr(netlink.IPSET_CMD_DEL, setName, cidr.Addr(), cidr)
}