Skip to content

Commit

Permalink
Add tun.UpdateRouteOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 23, 2024
1 parent d0887ea commit f457988
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
1 change: 1 addition & 0 deletions tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Tun interface {
Name() (string, error)
Start() error
Close() error
UpdateRouteOptions(tunOptions Options) error
}

type WinTun interface {
Expand Down
16 changes: 12 additions & 4 deletions tun_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type NativeTun struct {
options Options
inet4Address [4]byte
inet6Address [16]byte
routerSet bool
routeSet bool
}

func (t *NativeTun) Name() (string, error) {
Expand Down Expand Up @@ -258,9 +258,17 @@ func configure(tunFd int, ifIndex int, name string, options Options) error {
return nil
}

func (t *NativeTun) UpdateRouteOptions(tunOptions Options) error {
err := t.unsetRoutes()
if err != nil {
return err
}
t.options = tunOptions
return t.setRoutes()
}

func (t *NativeTun) setRoutes() error {
if t.options.AutoRoute && t.options.FileDescriptor == 0 {

routeRanges, err := t.options.BuildAutoRouteRanges(false)
if err != nil {
return err
Expand Down Expand Up @@ -298,13 +306,13 @@ func (t *NativeTun) setRoutes() error {
}
}
flushDNSCache()
t.routerSet = true
t.routeSet = true
}
return nil
}

func (t *NativeTun) unsetRoutes() error {
if !t.routerSet {
if !t.routeSet {
return nil
}
routeRanges, err := t.options.BuildAutoRouteRanges(false)
Expand Down
19 changes: 19 additions & 0 deletions tun_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,25 @@ func prefixToIPNet(prefix netip.Prefix) *net.IPNet {
}
}

func (t *NativeTun) UpdateRouteOptions(tunOptions Options) error {
if t.options.FileDescriptor > 0 {
return nil
} else if !t.options.AutoRoute {
t.options = tunOptions
return nil
}
tunLink, err := netlink.LinkByName(t.options.Name)
if err != nil {
return err
}
err = t.unsetRoute0(tunLink)
if err != nil {
return err
}
t.options = tunOptions
return t.setRoute(tunLink)
}

func (t *NativeTun) routes(tunLink netlink.Link) ([]netlink.Route, error) {
routeRanges, err := t.options.BuildAutoRouteRanges(false)
if err != nil {
Expand Down
38 changes: 35 additions & 3 deletions tun_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit f457988

Please sign in to comment.