banger/internal/daemon/nat.go
Thales Maciel 430f66d5dd 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.
2026-03-17 15:07:49 -03:00

53 lines
1.3 KiB
Go

package daemon
import (
"context"
"banger/internal/hostnat"
"banger/internal/model"
"banger/internal/system"
)
type natRule = hostnat.Rule
func (d *Daemon) ensureNAT(ctx context.Context, vm model.VMRecord, enable bool) error {
return hostnat.Ensure(ctx, d.runner, vm.Runtime.GuestIP, vm.Runtime.TapDevice, enable)
}
func (d *Daemon) validateNATPrereqs(ctx context.Context) (string, error) {
checks := system.NewPreflight()
checks.RequireCommand("ip", toolHint("ip"))
d.addNATPrereqs(ctx, checks)
if err := checks.Err("nat preflight failed"); err != nil {
return "", err
}
return d.defaultUplink(ctx)
}
func (d *Daemon) defaultUplink(ctx context.Context) (string, error) {
return hostnat.DefaultUplink(ctx, d.runner)
}
func parseDefaultUplink(output string) (string, error) {
return hostnat.ParseDefaultUplink(output)
}
func natRulesForVM(vm model.VMRecord, uplink string) ([]natRule, error) {
return hostnat.Rules(vm.Runtime.GuestIP, vm.Runtime.TapDevice, uplink)
}
func natRuleArgs(action string, rule natRule) []string {
return hostnat.RuleArgs(action, rule)
}
func natAddPlan(rules []natRule) [][]string {
return hostnat.AddPlan(rules)
}
func natRemovePlan(rules []natRule) [][]string {
return hostnat.RemovePlan(rules)
}
func natRuleKey(rule natRule) string {
return hostnat.RuleKey(rule)
}