Compare commits
2 Commits
c3a513f92c
...
09ac999346
| Author | SHA1 | Date | |
|---|---|---|---|
|
09ac999346
|
|||
|
9210448f16
|
@@ -10,10 +10,11 @@ const (
|
||||
INET
|
||||
ARP
|
||||
BRIDGE
|
||||
NETDEV
|
||||
)
|
||||
|
||||
func (f Type) String() string {
|
||||
switch f {
|
||||
func (t Type) String() string {
|
||||
switch t {
|
||||
case IP:
|
||||
return "ip"
|
||||
case IP6:
|
||||
@@ -24,7 +25,9 @@ func (f Type) String() string {
|
||||
return "arp"
|
||||
case BRIDGE:
|
||||
return "bridge"
|
||||
case NETDEV:
|
||||
return "netdev"
|
||||
default:
|
||||
return fmt.Sprintf("Encoding(%d)", f)
|
||||
return fmt.Sprintf("unknown family %d", t)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
type NFT interface {
|
||||
Run(arg ...string) error
|
||||
RunWithOutput(arg ...string) (string, error)
|
||||
}
|
||||
|
||||
type execNFT struct {
|
||||
@@ -35,3 +36,15 @@ func (r *execNFT) Run(arg ...string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *execNFT) RunWithOutput(arg ...string) (string, error) {
|
||||
cmd := exec.Command(r.nftPath, arg...)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
if len(out) > 0 {
|
||||
return string(out), err
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
49
nft.go
49
nft.go
@@ -2,7 +2,10 @@ package nft
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/chain"
|
||||
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/command"
|
||||
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/table"
|
||||
)
|
||||
@@ -15,6 +18,12 @@ type NFT interface {
|
||||
// nft flush ruleset
|
||||
Clear() error
|
||||
|
||||
// Version returns the version of nftables.
|
||||
//
|
||||
// This command is equivalent to:
|
||||
// nft -V
|
||||
Version() (Version, error)
|
||||
|
||||
// Table returns an API for working with tables.
|
||||
Table() table.API
|
||||
}
|
||||
@@ -22,6 +31,7 @@ type NFT interface {
|
||||
type nft struct {
|
||||
command command.NFT
|
||||
table table.API
|
||||
chain chain.API
|
||||
}
|
||||
|
||||
// New Returns a client for working with nftables.
|
||||
@@ -48,6 +58,7 @@ func NewWithPath(path string) (NFT, error) {
|
||||
return &nft{
|
||||
command: nftCommand,
|
||||
table: table.New(nftCommand),
|
||||
chain: chain.New(nftCommand),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -56,6 +67,44 @@ func (n *nft) Clear() error {
|
||||
return n.command.Run(args...)
|
||||
}
|
||||
|
||||
func (n *nft) Version() (Version, error) {
|
||||
args := []string{"-V"}
|
||||
out, err := n.command.RunWithOutput(args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
vers := ""
|
||||
opts := make(map[string]string)
|
||||
|
||||
lines := regexp.MustCompile("\r?\n").Split(strings.TrimSpace(string(out)), -1)
|
||||
for index, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
|
||||
if index == 0 {
|
||||
vers = line
|
||||
continue
|
||||
}
|
||||
|
||||
values := strings.Split(line, ":")
|
||||
if len(values) != 2 {
|
||||
continue
|
||||
}
|
||||
name := strings.TrimSpace(values[0])
|
||||
value := strings.TrimSpace(values[1])
|
||||
opts[name] = value
|
||||
}
|
||||
|
||||
return &version{
|
||||
version: vers,
|
||||
opts: opts,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (n *nft) Table() table.API {
|
||||
return n.table
|
||||
}
|
||||
|
||||
func (n *nft) Chain() chain.API {
|
||||
return n.chain
|
||||
}
|
||||
|
||||
21
version.go
Normal file
21
version.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package nft
|
||||
|
||||
type Version interface {
|
||||
// Version returns the version of the nftables client.
|
||||
Version() string
|
||||
// Opts returns the options of the nftables client.
|
||||
Opts() map[string]string
|
||||
}
|
||||
|
||||
type version struct {
|
||||
version string
|
||||
opts map[string]string
|
||||
}
|
||||
|
||||
func (v version) Version() string {
|
||||
return v.version
|
||||
}
|
||||
|
||||
func (v version) Opts() map[string]string {
|
||||
return v.opts
|
||||
}
|
||||
Reference in New Issue
Block a user