Move helper NAT management into Go
Remove the last shell-owned NAT surface by extracting the iptables logic into a shared Go package and using it from both bangerd and a hidden helper bridge in the CLI. Route customize.sh and interactive.sh through banger internal nat up/down so the remaining shell helpers reuse the same rule logic, resolve the local banger binary explicitly, and tear NAT back down during cleanup. Drop nat.sh from the runtime bundle and docs now that NAT is Go-managed everywhere, and keep coverage aligned with the new shared package and helper command. Validation: go test ./..., bash -n customize.sh interactive.sh verify.sh, make build, and a live ./verify.sh --nat run that installed host rules, reached outbound network access, and cleaned them up successfully.
This commit is contained in:
parent
60294e8c90
commit
430f66d5dd
13 changed files with 378 additions and 250 deletions
|
|
@ -15,6 +15,7 @@ import (
|
|||
|
||||
"banger/internal/api"
|
||||
"banger/internal/config"
|
||||
"banger/internal/hostnat"
|
||||
"banger/internal/model"
|
||||
"banger/internal/paths"
|
||||
"banger/internal/rpc"
|
||||
|
|
@ -39,10 +40,60 @@ func NewBangerCommand() *cobra.Command {
|
|||
RunE: helpNoArgs,
|
||||
}
|
||||
root.CompletionOptions.DisableDefaultCmd = true
|
||||
root.AddCommand(newDaemonCommand(), newVMCommand(), newImageCommand(), newTUICommand())
|
||||
root.AddCommand(newDaemonCommand(), newVMCommand(), newImageCommand(), newTUICommand(), newInternalCommand())
|
||||
return root
|
||||
}
|
||||
|
||||
func newInternalCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "internal",
|
||||
Hidden: true,
|
||||
RunE: helpNoArgs,
|
||||
}
|
||||
cmd.AddCommand(newInternalNATCommand())
|
||||
return cmd
|
||||
}
|
||||
|
||||
func newInternalNATCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "nat",
|
||||
Hidden: true,
|
||||
RunE: helpNoArgs,
|
||||
}
|
||||
cmd.AddCommand(
|
||||
newInternalNATActionCommand("up", true),
|
||||
newInternalNATActionCommand("down", false),
|
||||
)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func newInternalNATActionCommand(use string, enable bool) *cobra.Command {
|
||||
var guestIP string
|
||||
var tapDevice string
|
||||
cmd := &cobra.Command{
|
||||
Use: use,
|
||||
Hidden: true,
|
||||
Args: noArgsUsage("usage: banger internal nat " + use + " --guest-ip <ip> --tap <tap-device>"),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
guestIP = strings.TrimSpace(guestIP)
|
||||
tapDevice = strings.TrimSpace(tapDevice)
|
||||
if guestIP == "" {
|
||||
return errors.New("guest IP is required")
|
||||
}
|
||||
if tapDevice == "" {
|
||||
return errors.New("tap device is required")
|
||||
}
|
||||
if err := system.EnsureSudo(cmd.Context()); err != nil {
|
||||
return err
|
||||
}
|
||||
return hostnat.Ensure(cmd.Context(), system.NewRunner(), guestIP, tapDevice, enable)
|
||||
},
|
||||
}
|
||||
cmd.Flags().StringVar(&guestIP, "guest-ip", "", "guest IPv4 address")
|
||||
cmd.Flags().StringVar(&tapDevice, "tap", "", "tap device name")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func newDaemonCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "daemon",
|
||||
|
|
|
|||
|
|
@ -18,12 +18,33 @@ func TestNewBangerCommandHasExpectedSubcommands(t *testing.T) {
|
|||
for _, sub := range cmd.Commands() {
|
||||
names = append(names, sub.Name())
|
||||
}
|
||||
want := []string{"daemon", "image", "tui", "vm"}
|
||||
want := []string{"daemon", "image", "internal", "tui", "vm"}
|
||||
if !reflect.DeepEqual(names, want) {
|
||||
t.Fatalf("subcommands = %v, want %v", names, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInternalNATFlagsExist(t *testing.T) {
|
||||
root := NewBangerCommand()
|
||||
internal, _, err := root.Find([]string{"internal"})
|
||||
if err != nil {
|
||||
t.Fatalf("find internal: %v", err)
|
||||
}
|
||||
nat, _, err := internal.Find([]string{"nat"})
|
||||
if err != nil {
|
||||
t.Fatalf("find nat: %v", err)
|
||||
}
|
||||
up, _, err := nat.Find([]string{"up"})
|
||||
if err != nil {
|
||||
t.Fatalf("find nat up: %v", err)
|
||||
}
|
||||
for _, flagName := range []string{"guest-ip", "tap"} {
|
||||
if up.Flags().Lookup(flagName) == nil {
|
||||
t.Fatalf("missing flag %q", flagName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestVMCreateFlagsExist(t *testing.T) {
|
||||
root := NewBangerCommand()
|
||||
vm, _, err := root.Find([]string{"vm"})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue