Add tun.UpdateRouteOptions

This commit is contained in:
世界 2024-12-23 22:17:27 +08:00
parent d0887eabba
commit f457988090
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 67 additions and 7 deletions

View file

@ -178,9 +178,9 @@ func (t *NativeTun) Start() error {
} else {
err = luid.AddRoute(routeRange, gateway6, 0)
}
}
if err != nil {
return err
if err != nil {
return err
}
}
err = windnsapi.FlushResolverCache()
if err != nil {
@ -545,6 +545,38 @@ func (t *NativeTun) Close() error {
return err
}
func (t *NativeTun) UpdateRouteOptions(tunOptions Options) error {
t.options = tunOptions
if !t.options.AutoRoute {
return nil
}
gateway4, gateway6 := t.options.Inet4GatewayAddr(), t.options.Inet6GatewayAddr()
routeRanges, err := t.options.BuildAutoRouteRanges(false)
if err != nil {
return err
}
luid := winipcfg.LUID(t.adapter.LUID())
err = luid.FlushRoutes(windows.AF_UNSPEC)
if err != nil {
return err
}
for _, routeRange := range routeRanges {
if routeRange.Addr().Is4() {
err = luid.AddRoute(routeRange, gateway4, 0)
} else {
err = luid.AddRoute(routeRange, gateway6, 0)
}
if err != nil {
return err
}
}
err = windnsapi.FlushResolverCache()
if err != nil {
return err
}
return nil
}
func generateGUIDByDeviceName(name string) *windows.GUID {
hash := md5.New()
hash.Write([]byte("wintun"))