Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 29, 2024
1 parent f457988 commit 5caf4f2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
6 changes: 6 additions & 0 deletions monitor_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tun

import (
"sync"
"sync/atomic"

"github.com/sagernet/sing-tun/internal/winipcfg"
E "github.com/sagernet/sing/common/exceptions"
Expand All @@ -26,8 +27,13 @@ func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error)
}, nil
}

var disableRouteUpdateMonitor atomic.Bool

func (m *networkUpdateMonitor) Start() error {
routeListener, err := winipcfg.RegisterRouteChangeCallback(func(notificationType winipcfg.MibNotificationType, route *winipcfg.MibIPforwardRow2) {
if disableRouteUpdateMonitor.Load() {
return
}
m.emit()
})
if err != nil {
Expand Down
60 changes: 43 additions & 17 deletions tun_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,18 @@ func (t *NativeTun) Start() error {
return nil
}
luid := winipcfg.LUID(t.adapter.LUID())
err := luid.FlushRoutes(windows.AF_UNSPEC)
if err != nil {
return err
}
gateway4, gateway6 := t.options.Inet4GatewayAddr(), t.options.Inet6GatewayAddr()
routeRanges, err := t.options.BuildAutoRouteRanges(false)
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 = addRouteList(luid, routeRanges, gateway4, gateway6, 0)
if err != nil {
return err
}
err = windnsapi.FlushResolverCache()
if err != nil {
Expand Down Expand Up @@ -560,20 +558,48 @@ func (t *NativeTun) UpdateRouteOptions(tunOptions Options) error {
if err != nil {
return err
}
for _, routeRange := range routeRanges {
if routeRange.Addr().Is4() {
err = luid.AddRoute(routeRange, gateway4, 0)
err = addRouteList(luid, routeRanges, gateway4, gateway6, 0)
if err != nil {
return err
}
err = windnsapi.FlushResolverCache()
if err != nil {
return err
}
return nil
}

func addRouteList(luid winipcfg.LUID, destinations []netip.Prefix, gateway4 netip.Addr, gateway6 netip.Addr, metric uint32) error {
disableRouteUpdateMonitor.Store(true)
defer disableRouteUpdateMonitor.Store(false)

row := winipcfg.MibIPforwardRow2{}
row.Init()
row.InterfaceLUID = luid
row.Metric = metric
nextHop4 := row.NextHop
nextHop6 := row.NextHop
if gateway4.IsValid() {
nextHop4.SetAddr(gateway4)
}
if gateway6.IsValid() {
nextHop6.SetAddr(gateway6)
}
for _, destination := range destinations {
err := row.DestinationPrefix.SetPrefix(destination)
if err != nil {
return err
}
if destination.Addr().Is4() {
row.NextHop = nextHop4
} else {
err = luid.AddRoute(routeRange, gateway6, 0)
row.NextHop = nextHop6
}
err = row.Create()
if err != nil {
return err
}
}
err = windnsapi.FlushResolverCache()
if err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit 5caf4f2

Please sign in to comment.