refactor: tidy auto-route code
This commit is contained in:
parent
ce663a7b96
commit
c77993eed3
1 changed files with 12 additions and 16 deletions
|
@ -48,22 +48,11 @@ func configInterfaceRouting(interfaceName string, addr netip.Prefix) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_, err := cmd.ExecCmd(fmt.Sprintf("ip rule add from 0.0.0.0 iif lo uidrange 0-4294967294 lookup %d pref 9000", tableId))
|
execAddRuleCmd(fmt.Sprintf("from 0.0.0.0 iif lo uidrange 0-4294967294 lookup %d pref 9000", tableId))
|
||||||
if err != nil {
|
execAddRuleCmd(fmt.Sprintf("from %s iif lo uidrange 0-4294967294 lookup %d pref 9001", linkIP, tableId))
|
||||||
log.Warnln("%s", err)
|
execAddRuleCmd(fmt.Sprintf("from all iif %s lookup main suppress_prefixlength 0 pref 9002", interfaceName))
|
||||||
}
|
execAddRuleCmd(fmt.Sprintf("not from all iif lo lookup %d pref 9003", tableId))
|
||||||
_, err = cmd.ExecCmd(fmt.Sprintf("ip rule add from %s iif lo uidrange 0-4294967294 lookup %d pref 9001", linkIP, tableId))
|
|
||||||
if err != nil {
|
|
||||||
log.Warnln("%s", err)
|
|
||||||
}
|
|
||||||
_, err = cmd.ExecCmd(fmt.Sprintf("ip rule add from all iif %s lookup main suppress_prefixlength 0 pref 9002", interfaceName))
|
|
||||||
if err != nil {
|
|
||||||
log.Warnln("%s", err)
|
|
||||||
}
|
|
||||||
_, err = cmd.ExecCmd(fmt.Sprintf("ip rule add not from all iif lo lookup %d pref 9003", tableId))
|
|
||||||
if err != nil {
|
|
||||||
log.Warnln("%s", err)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
for _, route := range defaultRoutes {
|
for _, route := range defaultRoutes {
|
||||||
if err := execRouterCmd("add", route, interfaceName, linkIP.String(), "main"); err != nil {
|
if err := execRouterCmd("add", route, interfaceName, linkIP.String(), "main"); err != nil {
|
||||||
|
@ -77,6 +66,13 @@ func configInterfaceRouting(interfaceName string, addr netip.Prefix) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func execAddRuleCmd(rule string) {
|
||||||
|
_, err := cmd.ExecCmd("ip rule add " + rule)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnln("%s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func execRouterCmd(action, route, interfaceName, linkIP, table string) error {
|
func execRouterCmd(action, route, interfaceName, linkIP, table string) error {
|
||||||
cmdStr := fmt.Sprintf("ip route %s %s dev %s proto kernel scope link src %s table %s", action, route, interfaceName, linkIP, table)
|
cmdStr := fmt.Sprintf("ip route %s %s dev %s proto kernel scope link src %s table %s", action, route, interfaceName, linkIP, table)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue