2023-10-06 11:16:39 +01:00
|
|
|
// Package ipset provides ipset functionality.
|
|
|
|
package ipset
|
2021-06-18 15:55:01 +01:00
|
|
|
|
|
|
|
import (
|
2024-08-26 11:30:00 +01:00
|
|
|
"context"
|
|
|
|
"log/slog"
|
2021-06-18 15:55:01 +01:00
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2023-10-06 11:16:39 +01:00
|
|
|
// Manager is the ipset manager interface.
|
2021-06-18 15:55:01 +01:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Perhaps generalize this into some kind of a NetFilter type,
|
|
|
|
// since ipset is exclusive to Linux?
|
2023-10-06 11:16:39 +01:00
|
|
|
type Manager interface {
|
2024-08-26 11:30:00 +01:00
|
|
|
Add(ctx context.Context, host string, ip4s, ip6s []net.IP) (n int, err error)
|
2021-06-18 15:55:01 +01:00
|
|
|
Close() (err error)
|
|
|
|
}
|
|
|
|
|
2024-08-26 11:30:00 +01:00
|
|
|
// Config is the configuration structure for the ipset manager.
|
|
|
|
type Config struct {
|
|
|
|
// Logger is used for logging the operation of the ipset manager. It must
|
|
|
|
// not be nil.
|
|
|
|
Logger *slog.Logger
|
|
|
|
|
|
|
|
// Lines is the ipset configuration with the following syntax:
|
|
|
|
//
|
|
|
|
// DOMAIN[,DOMAIN].../IPSET_NAME[,IPSET_NAME]...
|
|
|
|
//
|
|
|
|
// Lines must not contain any blank lines or comments.
|
|
|
|
Lines []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewManager returns a new ipset manager. IPv4 addresses are added to an ipset
|
|
|
|
// with an ipv4 family; IPv6 addresses, to an ipv6 ipset. ipset must exist.
|
2021-06-18 15:55:01 +01:00
|
|
|
//
|
2024-08-26 11:30:00 +01:00
|
|
|
// If conf.Lines is empty, mgr and err are nil. The error's chain contains
|
2024-06-14 10:32:19 +01:00
|
|
|
// [errors.ErrUnsupported] if current OS is not supported.
|
2024-08-26 11:30:00 +01:00
|
|
|
func NewManager(ctx context.Context, conf *Config) (mgr Manager, err error) {
|
|
|
|
if len(conf.Lines) == 0 {
|
2021-12-27 17:54:00 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-08-26 11:30:00 +01:00
|
|
|
return newManager(ctx, conf)
|
2021-06-18 15:55:01 +01:00
|
|
|
}
|