mirror of
https://github.com/sigcn/pg.git
synced 2025-09-26 22:05:50 +08:00
netlink: add func AddrSubscribe on windows
This commit is contained in:
31
netlink/addr_windows.go
Normal file
31
netlink/addr_windows.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package netlink
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"slices"
|
||||
|
||||
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
||||
)
|
||||
|
||||
func AddrSubscribe(ctx context.Context, ch chan<- AddrUpdate) error {
|
||||
cb, err := winipcfg.RegisterUnicastAddressChangeCallback(func(notificationType winipcfg.MibNotificationType, addr *winipcfg.MibUnicastIPAddressRow) {
|
||||
if !slices.Contains([]winipcfg.MibNotificationType{winipcfg.MibAddInstance, winipcfg.MibDeleteInstance}, notificationType) {
|
||||
return
|
||||
}
|
||||
ch <- AddrUpdate{
|
||||
New: notificationType == winipcfg.MibAddInstance,
|
||||
Addr: net.IPNet{IP: net.ParseIP(addr.Address.Addr().String())},
|
||||
LinkIndex: int(addr.InterfaceIndex),
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
cb.Unregister()
|
||||
close(ch)
|
||||
}()
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user