mirror of
https://github.com/sigcn/pg.git
synced 2025-10-16 13:41:35 +08:00
29 lines
678 B
Go
29 lines
678 B
Go
package netlink
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
|
)
|
|
|
|
func RouteSubscribe(ctx context.Context, ch chan<- RouteUpdate) error {
|
|
cb, err := winipcfg.RegisterRouteChangeCallback(func(notificationType winipcfg.MibNotificationType, route *winipcfg.MibIPforwardRow2) {
|
|
dst := route.DestinationPrefix.Prefix()
|
|
ch <- RouteUpdate{
|
|
New: notificationType == 1,
|
|
Dst: &net.IPNet{IP: net.IP(dst.Addr().AsSlice()), Mask: net.CIDRMask(dst.Bits(), dst.Addr().BitLen())},
|
|
Via: net.IP(route.NextHop.Addr().AsSlice()),
|
|
}
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
go func() {
|
|
<-ctx.Done()
|
|
cb.Unregister()
|
|
close(ch)
|
|
}()
|
|
return nil
|
|
}
|