netlink: fix RouteUpdate on win and linux

This commit is contained in:
rkonfj
2024-07-19 19:33:29 +08:00
parent 25222d4b99
commit 3fa897e06e
2 changed files with 8 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ package netlink
import ( import (
"context" "context"
"log/slog" "log/slog"
"slices"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
) )
@@ -29,14 +30,13 @@ func RouteSubscribe(ctx context.Context, ch chan<- RouteUpdate) error {
Dst: e.Dst, Dst: e.Dst,
Via: e.Gw, Via: e.Gw,
} }
if e.Type == 24 { if !slices.Contains([]uint16{24, 25}, e.Type) {
ru.Type = 1
} else if e.Type == 25 {
ru.Type = 2
} else {
slog.Debug("DropUnsupportRouteEvent") slog.Debug("DropUnsupportRouteEvent")
continue continue
} }
if e.Type == 24 {
ru.New = true
}
ch <- ru ch <- ru
} }
} }

View File

@@ -11,7 +11,7 @@ func RouteSubscribe(ctx context.Context, ch chan<- RouteUpdate) error {
cb, err := winipcfg.RegisterRouteChangeCallback(func(notificationType winipcfg.MibNotificationType, route *winipcfg.MibIPforwardRow2) { cb, err := winipcfg.RegisterRouteChangeCallback(func(notificationType winipcfg.MibNotificationType, route *winipcfg.MibIPforwardRow2) {
dst := route.DestinationPrefix.Prefix() dst := route.DestinationPrefix.Prefix()
ch <- RouteUpdate{ ch <- RouteUpdate{
Type: uint16(notificationType), New: notificationType == 1,
Dst: &net.IPNet{IP: net.IP(dst.Addr().AsSlice()), Mask: net.CIDRMask(dst.Bits(), dst.Addr().BitLen())}, Dst: &net.IPNet{IP: net.IP(dst.Addr().AsSlice()), Mask: net.CIDRMask(dst.Bits(), dst.Addr().BitLen())},
Via: net.IP(route.NextHop.Addr().AsSlice()), Via: net.IP(route.NextHop.Addr().AsSlice()),
} }