2023-06-09 18:27:09 +02:00
|
|
|
//go:build windows
|
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package routemanager
|
2023-06-09 19:15:39 +02:00
|
|
|
|
|
|
|
import (
|
2023-06-12 11:43:18 +02:00
|
|
|
"net"
|
2023-06-09 19:15:39 +02:00
|
|
|
"net/netip"
|
|
|
|
|
2023-06-09 19:17:26 +02:00
|
|
|
"github.com/yusufpapurcu/wmi"
|
2023-06-09 19:15:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Win32_IP4RouteTable struct {
|
2023-06-12 11:06:49 +02:00
|
|
|
Destination string
|
|
|
|
Mask string
|
2023-06-09 19:15:39 +02:00
|
|
|
}
|
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
func getRoutesFromTable() ([]netip.Prefix, error) {
|
2023-06-09 19:15:39 +02:00
|
|
|
var routes []Win32_IP4RouteTable
|
2023-06-12 16:22:53 +02:00
|
|
|
query := "SELECT Destination, Mask FROM Win32_IP4RouteTable"
|
2023-06-09 19:15:39 +02:00
|
|
|
|
|
|
|
err := wmi.Query(query, &routes)
|
|
|
|
if err != nil {
|
2023-11-24 11:31:22 +01:00
|
|
|
return nil, err
|
2023-06-09 19:15:39 +02:00
|
|
|
}
|
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
var prefixList []netip.Prefix
|
2023-06-09 19:15:39 +02:00
|
|
|
for _, route := range routes {
|
2023-11-24 11:31:22 +01:00
|
|
|
addr, err := netip.ParseAddr(route.Destination)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
maskSlice := net.ParseIP(route.Mask).To4()
|
|
|
|
if maskSlice == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mask := net.IPv4Mask(maskSlice[0], maskSlice[1], maskSlice[2], maskSlice[3])
|
2023-06-12 11:43:18 +02:00
|
|
|
cidr, _ := mask.Size()
|
2023-11-24 11:31:22 +01:00
|
|
|
|
|
|
|
routePrefix := netip.PrefixFrom(addr, cidr)
|
|
|
|
if routePrefix.IsValid() && routePrefix.Addr().Is4() {
|
|
|
|
prefixList = append(prefixList, routePrefix)
|
2023-06-12 11:43:18 +02:00
|
|
|
}
|
2023-06-09 19:15:39 +02:00
|
|
|
}
|
2023-11-24 11:31:22 +01:00
|
|
|
return prefixList, nil
|
2023-06-09 19:15:39 +02:00
|
|
|
}
|