65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package nft
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os/exec"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
func executeCommand(name string, arg ...string) error {
|
|
cmd := exec.Command(name, arg...)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
if len(out) > 0 {
|
|
return errors.New(string(out))
|
|
}
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func checkingNFT(path string) error {
|
|
if path == "" {
|
|
return errors.New("path is empty")
|
|
}
|
|
|
|
cmd := exec.Command(path, "-V")
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return errors.New("nftables not found")
|
|
}
|
|
|
|
lines := regexp.MustCompile("\r?\n").Split(strings.TrimSpace(string(out)), -1)
|
|
json := false
|
|
for index, line := range lines {
|
|
line = strings.TrimSpace(line)
|
|
|
|
if index == 0 {
|
|
if !strings.HasPrefix(line, "nftables") {
|
|
return errors.New("nftables not found")
|
|
}
|
|
continue
|
|
}
|
|
|
|
if strings.HasPrefix(line, "json:") && strings.HasSuffix(line, "yes") {
|
|
json = true
|
|
}
|
|
}
|
|
|
|
if !json {
|
|
return errors.New("nftables disabled json")
|
|
}
|
|
|
|
cmd = exec.Command(path, "list", "ruleset")
|
|
out, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("nftables is not available or not supported by the kernel: %s", string(out))
|
|
}
|
|
|
|
return nil
|
|
}
|