Skip to content

Commit

Permalink
style: move method to private
Browse files Browse the repository at this point in the history
Signed-off-by: black-desk <[email protected]>
  • Loading branch information
black-desk committed Oct 24, 2023
1 parent facb901 commit 1de6ae8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 65 deletions.
65 changes: 65 additions & 0 deletions pkg/nftman/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package nftman

import (
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"

"github.com/black-desk/cgtproxy/pkg/cgtproxy/config"
"github.com/black-desk/cgtproxy/pkg/types"
. "github.com/black-desk/lib/go/errwrap"
"github.com/google/nftables"
"github.com/google/nftables/binaryutil"
Expand Down Expand Up @@ -376,3 +379,65 @@ func getNFTableRules() string {

return string(out)
}

func (nft *NFTManager) genSetElement(route *types.Route) (ret nftables.SetElement, err error) {
defer Wrap(&err, "generating set element for route",
"Path", route.Path,
"Target", route.Target,
)

nft.log.Debugw("Generating set element for new cgroup route.",
"Path", route.Path,
"Target", route.Target,
)

path := route.Path
target := route.Target

if _, ok := nft.cgroupMapElement[path]; ok {
err = os.ErrExist
return
}

var fileInfo os.FileInfo
fileInfo, err = os.Stat(path)
if err != nil {
return
}

inode := fileInfo.Sys().(*syscall.Stat_t).Ino

route.Path = nft.removeCgroupRootFromPath(path)
path = route.Path

nft.log.Debugw("Get inode of cgroup file using stat(2).",
"path", path,
"inode", inode,
)

setElement := nftables.SetElement{
Key: binaryutil.NativeEndian.PutUint64(inode),
VerdictData: nil,
}

switch target.Op {
case types.TargetDirect:
setElement.VerdictData = &expr.Verdict{
Kind: expr.VerdictReturn,
}

case types.TargetTProxy:
setElement.VerdictData = &expr.Verdict{
Kind: expr.VerdictGoto,
Chain: target.Chain,
}

case types.TargetDrop:
setElement.VerdictData = &expr.Verdict{
Kind: expr.VerdictDrop,
}
}

ret = setElement
return
}
65 changes: 0 additions & 65 deletions pkg/nftman/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,13 @@ import (
"os"
"sort"
"strings"
"syscall"

"github.com/black-desk/cgtproxy/pkg/cgtproxy/config"
"github.com/black-desk/cgtproxy/pkg/types"
. "github.com/black-desk/lib/go/errwrap"
"github.com/google/nftables"
"github.com/google/nftables/binaryutil"
"github.com/google/nftables/expr"
)

func (nft *NFTManager) genSetElement(route *types.Route) (ret nftables.SetElement, err error) {
defer Wrap(&err, "generating set element for route",
"Path", route.Path,
"Target", route.Target,
)

nft.log.Debugw("Generating set element for new cgroup route.",
"Path", route.Path,
"Target", route.Target,
)

path := route.Path
target := route.Target

if _, ok := nft.cgroupMapElement[path]; ok {
err = os.ErrExist
return
}

var fileInfo os.FileInfo
fileInfo, err = os.Stat(path)
if err != nil {
return
}

inode := fileInfo.Sys().(*syscall.Stat_t).Ino

route.Path = nft.removeCgroupRootFromPath(path)
path = route.Path

nft.log.Debugw("Get inode of cgroup file using stat(2).",
"path", path,
"inode", inode,
)

setElement := nftables.SetElement{
Key: binaryutil.NativeEndian.PutUint64(inode),
VerdictData: nil,
}

switch target.Op {
case types.TargetDirect:
setElement.VerdictData = &expr.Verdict{
Kind: expr.VerdictReturn,
}

case types.TargetTProxy:
setElement.VerdictData = &expr.Verdict{
Kind: expr.VerdictGoto,
Chain: target.Chain,
}

case types.TargetDrop:
setElement.VerdictData = &expr.Verdict{
Kind: expr.VerdictDrop,
}
}

ret = setElement
return
}

func (nft *NFTManager) AddRoutes(routes []types.Route) (err error) {
defer Wrap(&err, "add %d routes to nftable", len(routes))

Expand Down

0 comments on commit 1de6ae8

Please sign in to comment.