Skip to content

Commit

Permalink
Set gateway for windows and linux
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Oct 26, 2024
1 parent 07278fb commit 1be1ea9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
22 changes: 22 additions & 0 deletions tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type Options struct {
Name string
Inet4Address []netip.Prefix
Inet6Address []netip.Prefix
Inet4Gateway netip.Addr
Inet6Gateway netip.Addr
MTU uint32
GSO bool
AutoRoute bool
Expand Down Expand Up @@ -82,6 +84,26 @@ type Options struct {
EXP_DisableDNSHijack bool
}

func (o *Options) Inet4GatewayAddr() netip.Addr {
if o.Inet4Gateway.IsValid() {
return o.Inet4Gateway
}
if len(o.Inet4Address) > 0 {
return o.Inet4Address[0].Addr()
}
return netip.IPv4Unspecified()
}

func (o *Options) Inet6GatewayAddr() netip.Addr {
if o.Inet6Gateway.IsValid() {
return o.Inet6Gateway
}
if len(o.Inet6Address) > 0 {
return o.Inet6Address[0].Addr()
}
return netip.IPv6Unspecified()
}

func CalculateInterfaceName(name string) (tunName string) {
if runtime.GOOS == "darwin" {
tunName = "utun"
Expand Down
5 changes: 3 additions & 2 deletions tun_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,12 @@ func configure(tunFd int, ifIndex int, name string, options Options) error {
if err != nil {
return err
}
gateway4, gateway6 := options.Inet4GatewayAddr(), options.Inet6GatewayAddr()
for _, routeRange := range routeRanges {
if routeRange.Addr().Is4() {
err = addRoute(routeRange, options.Inet4Address[0].Addr())
err = addRoute(routeRange, gateway4)
} else {
err = addRoute(routeRange, options.Inet6Address[0].Addr())
err = addRoute(routeRange, gateway6)
}
if err != nil {
return E.Cause(err, "add route: ", routeRange)
Expand Down
8 changes: 8 additions & 0 deletions tun_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,17 @@ func (t *NativeTun) routes(tunLink netlink.Link) ([]netlink.Route, error) {
if err != nil {
return nil, err
}
gateway4, gateway6 := t.options.Inet4GatewayAddr(), t.options.Inet6GatewayAddr()
return common.Map(routeRanges, func(it netip.Prefix) netlink.Route {
var gateway netip.Addr
if it.Addr().Is4() {
gateway = gateway4
} else {
gateway = gateway6
}
return netlink.Route{
Dst: prefixToIPNet(it),
Gw: gateway.AsSlice(),
LinkIndex: tunLink.Attrs().Index,
Table: t.options.IPRoute2TableIndex,
}
Expand Down
5 changes: 3 additions & 2 deletions tun_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,16 @@ func (t *NativeTun) configure() error {
_ = luid.DisableDNSRegistration()
}
if t.options.AutoRoute {
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, netip.IPv4Unspecified(), 0)
err = luid.AddRoute(routeRange, gateway4, 0)
} else {
err = luid.AddRoute(routeRange, netip.IPv6Unspecified(), 0)
err = luid.AddRoute(routeRange, gateway6, 0)
}
}
if err != nil {
Expand Down

0 comments on commit 1be1ea9

Please sign in to comment.