Skip to content

Commit

Permalink
Replace underNetworkExtension param with `Exp_AutoRouteUseSubRanges…
Browse files Browse the repository at this point in the history
…ByDefault`
  • Loading branch information
nekohasekai committed Feb 4, 2024
1 parent 38c945f commit 1e354f0
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 10 deletions.
3 changes: 2 additions & 1 deletion tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ type Options struct {
Logger logger.Logger

// No work for TCP, do not use.
_TXChecksumOffload bool
_TXChecksumOffload bool
Exp_AutoRouteUseSubRangesByDefault bool
}

func CalculateInterfaceName(name string) (tunName string) {
Expand Down
2 changes: 1 addition & 1 deletion tun_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func configure(tunFd int, ifIndex int, name string, options Options) error {
}
if options.AutoRoute {
var routeRanges []netip.Prefix
routeRanges, err = options.BuildAutoRouteRanges(false)
routeRanges, err = options.BuildAutoRouteRanges()
for _, routeRange := range routeRanges {
if routeRange.Addr().Is4() {
err = addRoute(routeRange, options.Inet4Address[0].Addr())
Expand Down
2 changes: 1 addition & 1 deletion tun_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func prefixToIPNet(prefix netip.Prefix) *net.IPNet {
}

func (t *NativeTun) routes(tunLink netlink.Link) ([]netlink.Route, error) {
routeRanges, err := t.options.BuildAutoRouteRanges(false)
routeRanges, err := t.options.BuildAutoRouteRanges()
if err != nil {
return nil, err
}
Expand Down
9 changes: 3 additions & 6 deletions tun_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"net/netip"
"os"
"runtime"
"sort"
"strconv"

Expand Down Expand Up @@ -101,15 +100,13 @@ func buildExcludedRanges(includeRanges []ranges.Range[uint32], excludeRanges []r
return ranges.Merge(uidRanges)
}

const autoRouteUseSubRanges = runtime.GOOS == "darwin"

func (o *Options) BuildAutoRouteRanges(underNetworkExtension bool) ([]netip.Prefix, error) {
func (o *Options) BuildAutoRouteRanges() ([]netip.Prefix, error) {
var routeRanges []netip.Prefix
if o.AutoRoute && len(o.Inet4Address) > 0 {
var inet4Ranges []netip.Prefix
if len(o.Inet4RouteAddress) > 0 {
inet4Ranges = o.Inet4RouteAddress
} else if autoRouteUseSubRanges && !underNetworkExtension {
} else if o.Exp_AutoRouteUseSubRangesByDefault {
inet4Ranges = []netip.Prefix{
netip.PrefixFrom(netip.AddrFrom4([4]byte{0: 1}), 8),
netip.PrefixFrom(netip.AddrFrom4([4]byte{0: 2}), 7),
Expand Down Expand Up @@ -144,7 +141,7 @@ func (o *Options) BuildAutoRouteRanges(underNetworkExtension bool) ([]netip.Pref
var inet6Ranges []netip.Prefix
if len(o.Inet6RouteAddress) > 0 {
inet6Ranges = o.Inet6RouteAddress
} else if autoRouteUseSubRanges && !underNetworkExtension {
} else if o.Exp_AutoRouteUseSubRangesByDefault {
inet6Ranges = []netip.Prefix{
netip.PrefixFrom(netip.AddrFrom16([16]byte{0: 1}), 8),
netip.PrefixFrom(netip.AddrFrom16([16]byte{0: 2}), 7),
Expand Down
2 changes: 1 addition & 1 deletion tun_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (t *NativeTun) configure() error {
_ = luid.DisableDNSRegistration()
}
if t.options.AutoRoute {
routeRanges, err := t.options.BuildAutoRouteRanges(false)
routeRanges, err := t.options.BuildAutoRouteRanges()
if err != nil {
return err
}
Expand Down

0 comments on commit 1e354f0

Please sign in to comment.