62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package nft
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/command"
|
|
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/table"
|
|
)
|
|
|
|
// NFT A client for working with nftables
|
|
type NFT interface {
|
|
// Clear clears all rules.
|
|
//
|
|
// This command is equivalent to:
|
|
// nft flush ruleset
|
|
Clear() error
|
|
|
|
// Table returns an API for working with tables.
|
|
Table() table.API
|
|
}
|
|
|
|
type nft struct {
|
|
command command.NFT
|
|
table table.API
|
|
}
|
|
|
|
// New Returns a client for working with nftables.
|
|
// Searches for nft in paths: nft, /usr/sbin/nft, /sbin/nft
|
|
func New() (NFT, error) {
|
|
paths := []string{"nft", "/usr/sbin/nft", "/sbin/nft"}
|
|
for _, path := range paths {
|
|
nftClient, err := NewWithPath(path)
|
|
if err == nil {
|
|
return nftClient, nil
|
|
}
|
|
}
|
|
|
|
return nil, errors.New("nft not found")
|
|
}
|
|
|
|
// NewWithPath Returns the client for working with nftables with its path specified.
|
|
func NewWithPath(path string) (NFT, error) {
|
|
nftCommand, err := command.New(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &nft{
|
|
command: nftCommand,
|
|
table: table.New(nftCommand),
|
|
}, nil
|
|
}
|
|
|
|
func (n *nft) Clear() error {
|
|
args := []string{"flush", "ruleset"}
|
|
return n.command.Run(args...)
|
|
}
|
|
|
|
func (n *nft) Table() table.API {
|
|
return n.table
|
|
}
|