Files

61 lines
1.3 KiB
Go
Raw Permalink Normal View History

2026-04-13 09:46:02 +03:00
package nat
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
)
// Config holds NAT masquerade settings per interface.
type Config struct {
Interfaces []string `json:"interfaces"`
}
func configPath() string {
exe, err := os.Executable()
if err != nil {
return filepath.Join("configs", "nat.json")
}
return filepath.Join(filepath.Dir(exe), "configs", "nat.json")
}
// IsInstalled reports whether the nft (nftables) binary is available.
func IsInstalled() bool {
_, err := exec.LookPath("nft")
return err == nil
}
// Load reads the NAT config from disk.
func Load() (*Config, error) {
data, err := os.ReadFile(configPath())
if err != nil {
if os.IsNotExist(err) {
return &Config{Interfaces: []string{}}, nil
}
return nil, fmt.Errorf("read nat config: %w", err)
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parse nat config: %w", err)
}
if cfg.Interfaces == nil {
cfg.Interfaces = []string{}
}
return &cfg, nil
}
2026-04-13 12:40:49 +03:00
// Save writes the NAT config to the configs/ directory.
2026-04-13 09:46:02 +03:00
func Save(cfg *Config) error {
p := configPath()
if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
return fmt.Errorf("mkdir configs: %w", err)
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
return os.WriteFile(p, data, 0644)
}