|
|
|
@ -2,7 +2,6 @@ package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
@ -13,7 +12,6 @@ import (
|
|
|
|
|
"regexp"
|
|
|
|
|
"runtime"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
"github.com/google/nftables"
|
|
|
|
@ -71,7 +69,6 @@ func main() {
|
|
|
|
|
pf("\n")
|
|
|
|
|
pf("\tvar expressions []expr.Any\n")
|
|
|
|
|
pf("\tvar chain *nftables.Chain\n")
|
|
|
|
|
pf("\tvar table *nftables.Table\n")
|
|
|
|
|
|
|
|
|
|
tables, err := n.ListTables()
|
|
|
|
|
if err != nil {
|
|
|
|
@ -84,9 +81,7 @@ func main() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, table := range tables {
|
|
|
|
|
log.Printf("processing table: %s", table.Name)
|
|
|
|
|
|
|
|
|
|
pf("\ttable = n.AddTable(&nftables.Table{Family: %s,Name: \"%s\"})\n", TableFamilyString(table.Family), table.Name)
|
|
|
|
|
pf("\ttable:= n.AddTable(&nftables.Table{Family: %s,Name: \"%s\"})\n", TableFamilyString(table.Family), table.Name)
|
|
|
|
|
for _, chain := range chains {
|
|
|
|
|
if chain.Table.Name != table.Name {
|
|
|
|
|
continue
|
|
|
|
@ -125,10 +120,9 @@ func main() {
|
|
|
|
|
pf("\t})\n")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pf("\n\tif err:= n.Flush(); err!= nil {\n")
|
|
|
|
|
pf("\t\tlog.Fatal(err)\n")
|
|
|
|
|
pf("\t\tlog.Fatalf(\"fail to flush rules: %v\", err)\n")
|
|
|
|
|
pf("\t}\n\n")
|
|
|
|
|
pf("\tfmt.Println(\"nft ruleset applied.\")\n")
|
|
|
|
|
pf("}\n")
|
|
|
|
@ -154,11 +148,12 @@ func main() {
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
|
|
fmt.Println("Generated code:")
|
|
|
|
|
|
|
|
|
|
mw := io.MultiWriter(f, os.Stdout)
|
|
|
|
|
buf.WriteTo(mw)
|
|
|
|
|
|
|
|
|
|
// Format the generated code
|
|
|
|
|
log.Printf("formating file: %s", tempGoFile)
|
|
|
|
|
cmd := exec.Command("gofmt", "-w", "-s", tempGoFile)
|
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
@ -166,18 +161,13 @@ func main() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run the generated code
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
log.Printf("executing file: %s", tempGoFile)
|
|
|
|
|
cmd = exec.CommandContext(ctx, "go", "run", tempGoFile)
|
|
|
|
|
cmd = exec.Command("go", "run", tempGoFile)
|
|
|
|
|
output, err = cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Execution error: %v\nOutput: %s", err, output)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retrieve nftables state using nft
|
|
|
|
|
log.Printf("obtain current ruleset: %s", tempGoFile)
|
|
|
|
|
actualOutput, err := listNFTRuleset()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Failed to list nftables ruleset: %v\noutput:%s", err, actualOutput)
|
|
|
|
@ -199,6 +189,7 @@ func main() {
|
|
|
|
|
log.Fatalf("Failed to flush nftables ruleset: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func applyNFTRuleset(scriptPath string) (string, error) {
|
|
|
|
|
cmd := exec.Command("nft", "--debug=netlink", "-f", scriptPath)
|
|
|
|
|