Add initial nftables client implementation
This commit is contained in:
53
nft.go
Normal file
53
nft.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package nft
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// NFT A client for working with nftables
|
||||
type NFT interface {
|
||||
// Clear clears all rules.
|
||||
Clear() error
|
||||
|
||||
// AddTable adds a new table.
|
||||
AddTable(family FamilyType, name string) error
|
||||
}
|
||||
|
||||
type nft struct {
|
||||
path string
|
||||
}
|
||||
|
||||
// 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) {
|
||||
if err := checkingNFT(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &nft{
|
||||
path: path,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (n *nft) Clear() error {
|
||||
args := []string{"flush", "ruleset"}
|
||||
return executeCommand(n.path, args...)
|
||||
}
|
||||
|
||||
func (n *nft) AddTable(family FamilyType, name string) error {
|
||||
args := []string{"add", "table", family.String(), name}
|
||||
return executeCommand(n.path, args...)
|
||||
}
|
||||
Reference in New Issue
Block a user