добавлена фича защиты маршрутов

This commit is contained in:
kirillius 2026-07-15 16:44:28 +03:00
parent 81083c61ac
commit 9c0e840c09
3 changed files with 14 additions and 6 deletions

View File

@ -13,6 +13,7 @@ type Gateway struct {
type Config struct {
Controller string `json:"controller"`
Gateway []Gateway `json:"gateway"`
Protected []string `json:"protected"`
Metric int `json:"metric"`
Timeout int `json:"timeout"`
Interval int `json:"interval"`

View File

@ -103,12 +103,12 @@ func hotswapUpdate(cfg *Config, gatewayStates map[string]bool, subnets []string)
}
if !gatewayStates[name] {
log.Printf("Gateway %q is down, deleting route %s via %s", name, r.Subnet, r.Gateway)
delRoute(r.Subnet, r.Gateway)
delRoute(r.Subnet, r.Gateway, cfg.Protected)
continue
}
if !subnetsContains(subnets, r.Subnet) {
log.Printf("Subnet %s not in subnets list, deleting route via %s", r.Subnet, r.Gateway)
delRoute(r.Subnet, r.Gateway)
delRoute(r.Subnet, r.Gateway, cfg.Protected)
}
}
@ -150,11 +150,11 @@ func failoverUpdate(cfg *Config, gatewayStates map[string]bool, subnets []string
continue
}
log.Printf("Subnet %s not in subnets, deleting primary route via %s", r.Subnet, r.Gateway)
delRoute(r.Subnet, r.Gateway)
delRoute(r.Subnet, r.Gateway, cfg.Protected)
continue
}
log.Printf("Deleting route %s via %s (gateway %q)", r.Subnet, r.Gateway, name)
delRoute(r.Subnet, r.Gateway)
delRoute(r.Subnet, r.Gateway, cfg.Protected)
}
if primaryGW == nil {
@ -171,7 +171,11 @@ func failoverUpdate(cfg *Config, gatewayStates map[string]bool, subnets []string
}
}
func delRoute(subnet, gw string) {
func delRoute(subnet, gw string, protected []string) {
if subnetsContains(protected, subnet) {
log.Printf("Route %s is protected, skipping delete", subnet)
return
}
cmd := exec.Command("ip", "route", "del", subnet, "via", gw)
if err := cmd.Run(); err != nil {
log.Printf("Failed to delete route %s via %s: %v", subnet, gw, err)

View File

@ -8,5 +8,8 @@
"timeout": 3,
"interval": 10,
"port": 8089,
"hotswap": false
"hotswap": false,
"protected":[
"2.2.2.2/32"
]
}