mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
This commit is contained in:
1 parent
6a7fc56a5e
commit
43552b60c9
17 files changed
+504
-126
No files matched your search
@@ -0,0 +1,92 @@
|
||||
package netset
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
libnetset "github.com/nadoo/netset"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultNFTFamily = "inet"
|
||||
defaultNFTTable = "glider"
|
||||
maxNFTNameLength = 256
|
||||
)
|
||||
|
||||
type nftSetConfig struct {
|
||||
family libnetset.Family
|
||||
familyName string
|
||||
table string
|
||||
name string
|
||||
}
|
||||
|
||||
func parseNFTSetConfig(value string) (nftSetConfig, error) {
|
||||
parts := strings.Split(strings.TrimSpace(value), "/")
|
||||
for i := range parts {
|
||||
parts[i] = strings.TrimSpace(parts[i])
|
||||
}
|
||||
config := nftSetConfig{
|
||||
family: libnetset.FamilyINet,
|
||||
familyName: defaultNFTFamily,
|
||||
table: defaultNFTTable,
|
||||
}
|
||||
|
||||
switch len(parts) {
|
||||
case 1:
|
||||
config.name = parts[0]
|
||||
case 2:
|
||||
config.table = parts[0]
|
||||
config.name = parts[1]
|
||||
case 3:
|
||||
config.familyName = parts[0]
|
||||
config.table = parts[1]
|
||||
config.name = parts[2]
|
||||
default:
|
||||
return nftSetConfig{}, fmt.Errorf("invalid nftset %q: expected [[FAMILY/]TABLE/]NAME", value)
|
||||
}
|
||||
|
||||
if config.familyName == "" || config.table == "" || config.name == "" {
|
||||
return nftSetConfig{}, fmt.Errorf("invalid nftset %q: family, table, and set name must not be empty", value)
|
||||
}
|
||||
|
||||
family, ok := nftFamilies[strings.ToLower(config.familyName)]
|
||||
if !ok {
|
||||
return nftSetConfig{}, fmt.Errorf("invalid nftset %q: unsupported family %q", value, config.familyName)
|
||||
}
|
||||
config.family = family
|
||||
config.familyName = strings.ToLower(config.familyName)
|
||||
if err := validateNFTName("table", config.table); err != nil {
|
||||
return nftSetConfig{}, fmt.Errorf("invalid nftset %q: %w", value, err)
|
||||
}
|
||||
if err := validateNFTName("set", config.name); err != nil {
|
||||
return nftSetConfig{}, fmt.Errorf("invalid nftset %q: %w", value, err)
|
||||
}
|
||||
if err := validateNFTName("IPv6 set", config.name+"6"); err != nil {
|
||||
return nftSetConfig{}, fmt.Errorf("invalid nftset %q: %w", value, err)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func validateNFTName(kind, name string) error {
|
||||
if strings.IndexByte(name, 0) >= 0 {
|
||||
return fmt.Errorf("%s name must not contain NUL", kind)
|
||||
}
|
||||
if len(name) >= maxNFTNameLength {
|
||||
return fmt.Errorf("%s name is too long", kind)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c nftSetConfig) key() string {
|
||||
return c.familyName + "/" + c.table + "/" + c.name
|
||||
}
|
||||
|
||||
var nftFamilies = map[string]libnetset.Family{
|
||||
"inet": libnetset.FamilyINet,
|
||||
"ip": libnetset.FamilyIPv4,
|
||||
"ip6": libnetset.FamilyIPv6,
|
||||
"arp": libnetset.FamilyARP,
|
||||
"netdev": libnetset.FamilyNetdev,
|
||||
"bridge": libnetset.FamilyBridge,
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package netset
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"strings"
|
||||
|
||||
"github.com/nadoo/glider/rule"
|
||||
)
|
||||
|
||||
type addressSet interface {
|
||||
AddAddr(netip.Addr) error
|
||||
AddPrefix(netip.Prefix) error
|
||||
}
|
||||
|
||||
type setPair struct {
|
||||
label string
|
||||
ipv4 addressSet
|
||||
ipv6 addressSet
|
||||
}
|
||||
|
||||
func (p *setPair) addAddr(ip netip.Addr) error {
|
||||
if ip.Is4() {
|
||||
return p.ipv4.AddAddr(ip)
|
||||
}
|
||||
return p.ipv6.AddAddr(ip)
|
||||
}
|
||||
|
||||
func (p *setPair) addPrefix(prefix netip.Prefix) error {
|
||||
if prefix.Addr().Is4() {
|
||||
return p.ipv4.AddPrefix(prefix)
|
||||
}
|
||||
return p.ipv6.AddPrefix(prefix)
|
||||
}
|
||||
|
||||
type backend interface {
|
||||
IPSetPair(name string) (*setPair, error)
|
||||
NFTSetPair(config nftSetConfig) (*setPair, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
// Manager manages the ipset and nftables sets associated with rules.
|
||||
type Manager struct {
|
||||
backend backend
|
||||
domainSets map[string][]*setPair
|
||||
pairs map[string]*setPair
|
||||
}
|
||||
|
||||
// NewManager creates and populates all sets configured by rules.
|
||||
func NewManager(rules []*rule.Config) (*Manager, error) {
|
||||
if !hasSetConfig(rules) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
b, err := newBackend()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newManager(rules, b)
|
||||
}
|
||||
|
||||
func newManager(rules []*rule.Config, b backend) (*Manager, error) {
|
||||
m := &Manager{
|
||||
backend: b,
|
||||
domainSets: make(map[string][]*setPair),
|
||||
pairs: make(map[string]*setPair),
|
||||
}
|
||||
|
||||
for _, config := range rules {
|
||||
if err := m.addRule(config); err != nil {
|
||||
return nil, errors.Join(err, b.Close())
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func hasSetConfig(rules []*rule.Config) bool {
|
||||
for _, config := range rules {
|
||||
if config.IPSet != "" || config.NFTSet != "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (m *Manager) addRule(config *rule.Config) error {
|
||||
pairs := make([]*setPair, 0, 2)
|
||||
|
||||
if config.IPSet != "" {
|
||||
pair, err := m.pair("ipset:"+config.IPSet, func() (*setPair, error) {
|
||||
return m.backend.IPSetPair(config.IPSet)
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("rule %q: initialize ipset %q: %w", config.RulePath, config.IPSet, err)
|
||||
}
|
||||
pairs = append(pairs, pair)
|
||||
}
|
||||
|
||||
if config.NFTSet != "" {
|
||||
nftConfig, err := parseNFTSetConfig(config.NFTSet)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rule %q: %w", config.RulePath, err)
|
||||
}
|
||||
pair, err := m.pair("nftset:"+nftConfig.key(), func() (*setPair, error) {
|
||||
return m.backend.NFTSetPair(nftConfig)
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("rule %q: initialize nftset %q: %w", config.RulePath, nftConfig.key(), err)
|
||||
}
|
||||
pairs = append(pairs, pair)
|
||||
}
|
||||
|
||||
for _, item := range config.IP {
|
||||
ip, err := netip.ParseAddr(item)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rule %q: parse set IP %q: %w", config.RulePath, item, err)
|
||||
}
|
||||
for _, pair := range pairs {
|
||||
if err := pair.addAddr(ip); err != nil {
|
||||
return fmt.Errorf("rule %q: add IP %q to %s: %w", config.RulePath, item, pair.label, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, item := range config.CIDR {
|
||||
prefix, err := netip.ParsePrefix(item)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rule %q: parse set CIDR %q: %w", config.RulePath, item, err)
|
||||
}
|
||||
for _, pair := range pairs {
|
||||
if err := pair.addPrefix(prefix); err != nil {
|
||||
return fmt.Errorf("rule %q: add CIDR %q to %s: %w", config.RulePath, item, pair.label, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, domain := range config.Domain {
|
||||
domain = strings.ToLower(domain)
|
||||
for _, pair := range pairs {
|
||||
m.domainSets[domain] = appendPair(m.domainSets[domain], pair)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) pair(key string, create func() (*setPair, error)) (*setPair, error) {
|
||||
if pair, ok := m.pairs[key]; ok {
|
||||
return pair, nil
|
||||
}
|
||||
pair, err := create()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m.pairs[key] = pair
|
||||
return pair, nil
|
||||
}
|
||||
|
||||
func appendPair(pairs []*setPair, pair *setPair) []*setPair {
|
||||
for _, item := range pairs {
|
||||
if item == pair {
|
||||
return pairs
|
||||
}
|
||||
}
|
||||
return append(pairs, pair)
|
||||
}
|
||||
|
||||
// AddDomainIP updates all sets associated with domain or one of its parents.
|
||||
func (m *Manager) AddDomainIP(domain string, ip netip.Addr) error {
|
||||
if !ip.IsValid() {
|
||||
return errors.New("netset: invalid DNS answer address")
|
||||
}
|
||||
|
||||
domain = strings.ToLower(domain)
|
||||
seen := make(map[*setPair]struct{})
|
||||
var errs []error
|
||||
for i := len(domain); i != -1; {
|
||||
i = strings.LastIndexByte(domain[:i], '.')
|
||||
for _, pair := range m.domainSets[domain[i+1:]] {
|
||||
if _, ok := seen[pair]; ok {
|
||||
continue
|
||||
}
|
||||
seen[pair] = struct{}{}
|
||||
if err := pair.addAddr(ip); err != nil {
|
||||
errs = append(errs, fmt.Errorf("add DNS answer %s for %q to %s: %w", ip, domain, pair.label, err))
|
||||
}
|
||||
}
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
// Close closes all netlink sockets owned by the manager.
|
||||
func (m *Manager) Close() error {
|
||||
if m == nil || m.backend == nil {
|
||||
return nil
|
||||
}
|
||||
return m.backend.Close()
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
//go:build linux
|
||||
|
||||
package netset
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"syscall"
|
||||
|
||||
libnetset "github.com/nadoo/netset"
|
||||
)
|
||||
|
||||
type linuxBackend struct {
|
||||
ipset *libnetset.IPSet
|
||||
nftsets []*libnetset.NFTSet
|
||||
}
|
||||
|
||||
func newBackend() (backend, error) {
|
||||
return &linuxBackend{}, nil
|
||||
}
|
||||
|
||||
func (b *linuxBackend) IPSetPair(name string) (*setPair, error) {
|
||||
if b.ipset == nil {
|
||||
client, err := libnetset.NewIPSet()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b.ipset = client
|
||||
}
|
||||
|
||||
if err := ensureIPSet(b.ipset, name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ensureIPSet(b.ipset, name+"6", libnetset.OptIPv6()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &setPair{
|
||||
label: "ipset " + name,
|
||||
ipv4: ipSetTarget{client: b.ipset, name: name},
|
||||
ipv6: ipSetTarget{client: b.ipset, name: name + "6"},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ensureIPSet(client *libnetset.IPSet, name string, opts ...libnetset.Option) error {
|
||||
if err := client.Create(name, opts...); err != nil && !errors.Is(err, syscall.EEXIST) {
|
||||
return fmt.Errorf("create %q: %w", name, err)
|
||||
}
|
||||
if err := client.Flush(name); err != nil {
|
||||
return fmt.Errorf("flush %q: %w", name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *linuxBackend) NFTSetPair(config nftSetConfig) (*setPair, error) {
|
||||
ipv4, err := b.newNFTSet(config, config.name, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ipv6, err := b.newNFTSet(config, config.name+"6", true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &setPair{
|
||||
label: "nftset " + config.key(),
|
||||
ipv4: nftSetTarget{set: ipv4},
|
||||
ipv6: nftSetTarget{set: ipv6},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (b *linuxBackend) newNFTSet(config nftSetConfig, name string, ipv6 bool) (*libnetset.NFTSet, error) {
|
||||
opts := []libnetset.Option{libnetset.OptFamily(config.family), libnetset.OptInterval()}
|
||||
if ipv6 {
|
||||
opts = append(opts, libnetset.OptIPv6())
|
||||
}
|
||||
|
||||
set, err := libnetset.NewNFTSet(config.table, name, opts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open %s/%s/%s: %w", config.familyName, config.table, name, err)
|
||||
}
|
||||
b.nftsets = append(b.nftsets, set)
|
||||
|
||||
if err := set.Create(); err != nil && !errors.Is(err, syscall.EEXIST) {
|
||||
return nil, fmt.Errorf("create %s/%s/%s: %w", config.familyName, config.table, name, err)
|
||||
}
|
||||
if err := set.Flush(); err != nil {
|
||||
return nil, fmt.Errorf("flush %s/%s/%s: %w", config.familyName, config.table, name, err)
|
||||
}
|
||||
return set, nil
|
||||
}
|
||||
|
||||
func (b *linuxBackend) Close() error {
|
||||
var errs []error
|
||||
if b.ipset != nil {
|
||||
errs = append(errs, b.ipset.Close())
|
||||
b.ipset = nil
|
||||
}
|
||||
for _, set := range b.nftsets {
|
||||
errs = append(errs, set.Close())
|
||||
}
|
||||
b.nftsets = nil
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
type ipSetTarget struct {
|
||||
client *libnetset.IPSet
|
||||
name string
|
||||
}
|
||||
|
||||
func (s ipSetTarget) AddAddr(ip netip.Addr) error {
|
||||
return s.client.AddAddr(s.name, ip)
|
||||
}
|
||||
|
||||
func (s ipSetTarget) AddPrefix(prefix netip.Prefix) error {
|
||||
return s.client.AddPrefix(s.name, prefix)
|
||||
}
|
||||
|
||||
type nftSetTarget struct {
|
||||
set *libnetset.NFTSet
|
||||
}
|
||||
|
||||
func (s nftSetTarget) AddAddr(ip netip.Addr) error {
|
||||
return s.set.AddAddr(ip)
|
||||
}
|
||||
|
||||
func (s nftSetTarget) AddPrefix(prefix netip.Prefix) error {
|
||||
return s.set.AddPrefix(prefix)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
//go:build !linux
|
||||
|
||||
package netset
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
libnetset "github.com/nadoo/netset"
|
||||
)
|
||||
|
||||
func newBackend() (backend, error) {
|
||||
return nil, fmt.Errorf("netset manager is supported only on linux: %w", libnetset.ErrUnsupported)
|
||||
}
|
||||
Reference in new issue
Block a user