Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dnsmasq ipset handing #3

Merged
merged 4 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module github.com/maksimkurb/keenetic-pbr

go 1.21
go 1.23.4

require github.com/BurntSushi/toml v1.4.0

require github.com/vishvananda/netlink v1.3.0
require (
github.com/pelletier/go-toml/v2 v2.2.3
github.com/coreos/go-iptables v0.8.0
github.com/valyala/fasttemplate v1.2.2
github.com/vishvananda/netlink v1.3.0
golang.org/x/sys v0.10.0
)

require (
github.com/coreos/go-iptables v0.8.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
golang.org/x/sys v0.10.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
Expand Down
26 changes: 15 additions & 11 deletions lib/commands/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func CreateApplyCommand() *ApplyCommand {
fs: flag.NewFlagSet("apply", flag.ExitOnError),
}

gc.fs.BoolVar(&gc.SkipDnsmasq, "skip-dnsmasq", false, "Skip dnsmasq config files generation")
gc.fs.BoolVar(&gc.SkipIpset, "skip-ipset", false, "Skip ipset filling")
gc.fs.BoolVar(&gc.SkipRouting, "skip-routing", false, "Skip ip routes and ip rules applying")
gc.fs.StringVar(&gc.OnlyRoutingForInterface, "only-routing-for-interface", "", "Only apply ip routes/rules for the specified interface (if it is present in keenetic-pbr config)")
Expand All @@ -28,7 +27,6 @@ type ApplyCommand struct {
fs *flag.FlagSet
cfg *config.Config

SkipDnsmasq bool
SkipIpset bool
SkipRouting bool
OnlyRoutingForInterface string
Expand All @@ -44,37 +42,43 @@ func (g *ApplyCommand) Init(args []string, ctx *AppContext) error {
return err
}

if g.SkipDnsmasq && g.SkipIpset && g.SkipRouting {
return fmt.Errorf("--skip-dnsmasq, --skip-ipset and --skip-routing are used, nothing to do")
if g.SkipIpset && g.SkipRouting {
return fmt.Errorf("--skip-ipset and --skip-routing are used, nothing to do")
}

if g.OnlyRoutingForInterface != "" && (g.SkipRouting || g.SkipIpset || g.SkipDnsmasq) {
if g.OnlyRoutingForInterface != "" && (g.SkipRouting || g.SkipIpset) {
return fmt.Errorf("--only-routing-for-interface and --skip-* can not be used together")
}

if cfg, err := loadAndValidateConfigOrFail(ctx.ConfigPath, ctx.Interfaces); err != nil {
if cfg, err := loadAndValidateConfigOrFail(ctx.ConfigPath); err != nil {
return err
} else {
g.cfg = cfg
}

if !g.SkipRouting || g.OnlyRoutingForInterface != "" {
if err := networking.ValidateInterfacesArePresent(g.cfg, ctx.Interfaces); err != nil {
return fmt.Errorf("failed to apply routing: %v", err)
}
}

return nil
}

func (g *ApplyCommand) Run() error {
if (!g.SkipIpset || !g.SkipDnsmasq) && g.OnlyRoutingForInterface == "" {
if err := lists.ApplyLists(g.cfg, g.SkipDnsmasq, g.SkipIpset); err != nil {
return fmt.Errorf("failed to apply configuration: %v", err)
if !g.SkipIpset && g.OnlyRoutingForInterface == "" {
if err := lists.ImportListsToIPSets(g.cfg); err != nil {
return fmt.Errorf("failed to apply lists: %v", err)
}
}

if !g.SkipRouting {
if appliedAtLeastOnce, err := networking.ApplyNetworkConfiguration(g.cfg, &g.OnlyRoutingForInterface); err != nil {
return fmt.Errorf("failed to apply configuration: %v", err)
return fmt.Errorf("failed to apply routing: %v", err)
} else {
if !appliedAtLeastOnce {
if g.FailIfNothingToApply {
log.Warnf("Nothing to apply, exiting with error code (5)")
log.Warnf("Nothing to apply, exiting with exit_code=5")
os.Exit(5)
} else {
log.Warnf("Nothing to apply")
Expand Down
7 changes: 2 additions & 5 deletions lib/commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ type AppContext struct {
Interfaces []networking.Interface
}

func loadAndValidateConfigOrFail(configPath string, interfaces []networking.Interface) (*config.Config, error) {
func loadAndValidateConfigOrFail(configPath string) (*config.Config, error) {
cfg, err := config.LoadConfig(configPath)
if err != nil {
return nil, fmt.Errorf("failed to load configuration: %v", err)
}

// Validate configuration
if err = cfg.ValidateConfig(); err != nil {
return nil, fmt.Errorf("failed to validate configuration: %v", err)
}
if err := networking.ValidateConfigInterfaces(cfg, interfaces); err != nil {
return nil, fmt.Errorf("failed to validate configuration: %v", err)
return nil, fmt.Errorf("configuration validation is failed: %v", err)
}
return cfg, nil
}
50 changes: 50 additions & 0 deletions lib/commands/dnsmasq_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package commands

import (
"flag"
"fmt"
"github.com/maksimkurb/keenetic-pbr/lib/config"
"github.com/maksimkurb/keenetic-pbr/lib/lists"
"github.com/maksimkurb/keenetic-pbr/lib/log"
)

func CreateDnsmasqConfigCommand() *DnsmasqConfigCommand {
gc := &DnsmasqConfigCommand{
fs: flag.NewFlagSet("print-dnsmasq-config", flag.ExitOnError),
}

return gc
}

type DnsmasqConfigCommand struct {
fs *flag.FlagSet
cfg *config.Config
}

func (g *DnsmasqConfigCommand) Name() string {
return g.fs.Name()
}

func (g *DnsmasqConfigCommand) Init(args []string, ctx *AppContext) error {
log.SetForceStdErr(true)

if err := g.fs.Parse(args); err != nil {
return err
}

if cfg, err := loadAndValidateConfigOrFail(ctx.ConfigPath); err != nil {
return err
} else {
g.cfg = cfg
}

return nil
}

func (g *DnsmasqConfigCommand) Run() error {
if err := lists.PrintDnsmasqConfig(g.cfg); err != nil {
return fmt.Errorf("failed to print dnsmasq config: %v", err)
}

return nil
}
2 changes: 1 addition & 1 deletion lib/commands/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (g *DownloadCommand) Init(args []string, ctx *AppContext) error {
return err
}

if cfg, err := loadAndValidateConfigOrFail(ctx.ConfigPath, ctx.Interfaces); err != nil {
if cfg, err := loadAndValidateConfigOrFail(ctx.ConfigPath); err != nil {
return err
} else {
g.cfg = cfg
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (g *InterfacesCommand) Init(args []string, ctx *AppContext) error {
return err
}

if cfg, err := loadAndValidateConfigOrFail(ctx.ConfigPath, ctx.Interfaces); err != nil {
if cfg, err := loadAndValidateConfigOrFail(ctx.ConfigPath); err != nil {
return err
} else {
g.cfg = cfg
Expand Down
47 changes: 27 additions & 20 deletions lib/commands/self_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ func (g *SelfCheckCommand) Init(args []string, ctx *AppContext) error {
return err
}

if cfg, err := loadAndValidateConfigOrFail(ctx.ConfigPath, ctx.Interfaces); err != nil {
if cfg, err := loadAndValidateConfigOrFail(ctx.ConfigPath); err != nil {
return err
} else {
g.cfg = cfg
}

if err := networking.ValidateInterfacesArePresent(g.cfg, ctx.Interfaces); err != nil {
log.Errorf("Configuration validation failed: %v", err)
networking.PrintMissingInterfacesHelp()
}

return nil
}

Expand All @@ -52,17 +57,17 @@ func (g *SelfCheckCommand) Run() error {
log.Errorf("Failed to serialize config: %v", err)
return err
} else {
if err := binary.Write(os.Stdout, binary.LittleEndian, cfg); err != nil {
if err := binary.Write(os.Stdout, binary.LittleEndian, cfg.Bytes()); err != nil {
log.Errorf("Failed to output config: %v", err)
return err
}
}

log.Infof("----------------- Configuration END ------------------")

for _, ipset := range g.cfg.Ipset {
for _, ipset := range g.cfg.IPSets {
if err := checkIpset(g.cfg, ipset); err != nil {
log.Errorf("Failed to check ipset routing configuration [%s]: %v", ipset.IpsetName, err)
log.Errorf("Failed to check ipset routing configuration [%s]: %v", ipset.IPSetName, err)
return err
}
}
Expand All @@ -71,27 +76,29 @@ func (g *SelfCheckCommand) Run() error {
return nil
}

func checkIpset(cfg *config.Config, ipset *config.IpsetConfig) error {
log.Infof("----------------- IPSet [%s] ------------------", ipset.IpsetName)
func checkIpset(cfg *config.Config, ipsetCfg *config.IPSetConfig) error {
log.Infof("----------------- IPSet [%s] ------------------", ipsetCfg.IPSetName)

if ipset.Routing.KillSwitch {
if ipsetCfg.Routing.KillSwitch {
log.Infof("Usage of kill-switch is enabled")
} else {
log.Infof("Usage of kill-switch is DISABLED")
}

if exists, err := networking.CheckIpsetExists(ipset); err != nil {
log.Errorf("Failed to check ipset presense [%s]: %v", ipset.IpsetName, err)
ipset := networking.BuildIPSet(ipsetCfg.IPSetName, ipsetCfg.IPVersion)

if exists, err := ipset.IsExists(); err != nil {
log.Errorf("Failed to check ipset presense [%s]: %v", ipsetCfg.IPSetName, err)
return err
} else {
if exists {
log.Infof("ipset [%s] is exists", ipset.IpsetName)
log.Infof("ipset [%s] is exists", ipsetCfg.IPSetName)
} else {
log.Errorf("ipset [%s] is NOT exists", ipset.IpsetName)
log.Errorf("ipset [%s] is NOT exists", ipsetCfg.IPSetName)
}
}

ipRule := networking.BuildIPRuleForIpset(ipset)
ipRule := networking.BuildIPRuleForIpset(ipsetCfg)
if exists, err := ipRule.IsExists(); err != nil {
log.Errorf("Failed to check IP rule [%v]: %v", ipRule, err)
return err
Expand All @@ -117,7 +124,7 @@ func checkIpset(cfg *config.Config, ipset *config.IpsetConfig) error {
log.Warnf("Usage of Keenetic API is DISABLED. This may lead to wrong interface selection if you are using multiple interfaces for ipset")
}

if chosenIface, err := networking.ChooseBestInterface(ipset, useKeeneticAPI, keeneticIfaces); err != nil {
if chosenIface, err := networking.ChooseBestInterface(ipsetCfg, useKeeneticAPI, keeneticIfaces); err != nil {
log.Errorf("Failed to choose best interface: %v", err)
return err
} else {
Expand All @@ -127,21 +134,21 @@ func checkIpset(cfg *config.Config, ipset *config.IpsetConfig) error {
log.Infof("Choosing interface %s", chosenIface.Attrs().Name)
}

if err := checkIpRoutes(ipset, chosenIface); err != nil {
if err := checkIpRoutes(ipsetCfg, chosenIface); err != nil {
log.Errorf("Failed to check IP routes: %v", err)
return err
}
if err := checkIpTables(ipset); err != nil {
if err := checkIpTables(ipsetCfg); err != nil {
log.Errorf("Failed to check iptable rules: %v", err)
return err
}
}

log.Infof("----------------- IPSet [%s] END ------------------", ipset.IpsetName)
log.Infof("----------------- IPSet [%s] END ------------------", ipsetCfg.IPSetName)
return nil
}

func checkIpTables(ipset *config.IpsetConfig) error {
func checkIpTables(ipset *config.IPSetConfig) error {
ipTableRules, err := networking.BuildIPTablesForIpset(ipset)
if err != nil {
log.Errorf("Failed to build iptable rules: %v", err)
Expand All @@ -165,7 +172,7 @@ func checkIpTables(ipset *config.IpsetConfig) error {
return nil
}

func checkIpRoutes(ipset *config.IpsetConfig, chosenIface *networking.Interface) error {
func checkIpRoutes(ipset *config.IPSetConfig, chosenIface *networking.Interface) error {
if routes, err := networking.ListRoutesInTable(ipset.Routing.IpRouteTable); err != nil {
log.Errorf("Failed to list IP routes in table %d: %v", ipset.Routing.IpRouteTable, err)
return err
Expand Down Expand Up @@ -194,7 +201,7 @@ func checkIpRoutes(ipset *config.IpsetConfig, chosenIface *networking.Interface)
}

if chosenIface != nil {
defaultIpRoute := networking.BuildDefaultRoute(ipset.IpVersion, *chosenIface, ipset.Routing.IpRouteTable)
defaultIpRoute := networking.BuildDefaultRoute(ipset.IPVersion, *chosenIface, ipset.Routing.IpRouteTable)
if exists, err := defaultIpRoute.IsExists(); err != nil {
log.Errorf("Failed to check default IP route [%v]: %v", defaultIpRoute, err)
return err
Expand All @@ -209,7 +216,7 @@ func checkIpRoutes(ipset *config.IpsetConfig, chosenIface *networking.Interface)
log.Infof("Default IP route check SKIPPED because no interface is connected")
}

blackholeIpRoute := networking.BuildBlackholeRoute(ipset.IpVersion, ipset.Routing.IpRouteTable)
blackholeIpRoute := networking.BuildBlackholeRoute(ipset.IPVersion, ipset.Routing.IpRouteTable)
if exists, err := blackholeIpRoute.IsExists(); err != nil {
log.Errorf("Failed to check blackhole IP route [%v]: %v", blackholeIpRoute, err)
return err
Expand Down
12 changes: 6 additions & 6 deletions lib/commands/undo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (g *UndoCommand) Init(args []string, ctx *AppContext) error {
return err
}

if cfg, err := loadAndValidateConfigOrFail(ctx.ConfigPath, ctx.Interfaces); err != nil {
if cfg, err := loadAndValidateConfigOrFail(ctx.ConfigPath); err != nil {
return err
} else {
g.cfg = cfg
Expand All @@ -43,9 +43,9 @@ func (g *UndoCommand) Init(args []string, ctx *AppContext) error {
func (g *UndoCommand) Run() error {
log.Infof("Removing all iptables rules, ip rules and ip routes...")

for _, ipset := range g.cfg.Ipset {
for _, ipset := range g.cfg.IPSets {
if err := undoIpset(ipset); err != nil {
log.Errorf("Failed to undo routing configuration for ipset [%s]: %v", ipset.IpsetName, err)
log.Errorf("Failed to undo routing configuration for ipset [%s]: %v", ipset.IPSetName, err)
return err
}
}
Expand All @@ -54,8 +54,8 @@ func (g *UndoCommand) Run() error {
return nil
}

func undoIpset(ipset *config.IpsetConfig) error {
log.Infof("----------------- IPSet [%s] ------------------", ipset.IpsetName)
func undoIpset(ipset *config.IPSetConfig) error {
log.Infof("----------------- IPSet [%s] ------------------", ipset.IPSetName)

log.Infof("Deleting IP route table %d", ipset.Routing.IpRouteTable)
if err := networking.DelIpRouteTable(ipset.Routing.IpRouteTable); err != nil {
Expand Down Expand Up @@ -88,6 +88,6 @@ func undoIpset(ipset *config.IpsetConfig) error {
}
}

log.Infof("----------------- IPSet [%s] END ------------------", ipset.IpsetName)
log.Infof("----------------- IPSet [%s] END ------------------", ipset.IPSetName)
return nil
}
Loading