diff --git a/core/nylon_wireguard.go b/core/nylon_wireguard.go index 81c48fb..90214c8 100644 --- a/core/nylon_wireguard.go +++ b/core/nylon_wireguard.go @@ -5,7 +5,6 @@ import ( "cmp" "encoding/hex" "fmt" - "net/netip" "slices" "github.com/encodeous/nylon/polyamide/conn" @@ -87,13 +86,8 @@ listen_port=%d // configure self selfSvc := make(map[state.ServiceId]struct{}) - var defaultAddr *netip.Addr for _, svc := range s.GetRouter(s.Id).Services { prefix := s.GetSvcPrefix(svc) - if defaultAddr == nil { - addr := prefix.Addr() - defaultAddr = &addr - } selfSvc[svc] = struct{}{} err = ConfigureAlias(itfName, prefix) if err != nil { @@ -101,7 +95,7 @@ listen_port=%d } } - if defaultAddr == nil { + if len(s.GetRouter(s.Id).Services) == 0 { return fmt.Errorf("no address configured for self") } @@ -116,7 +110,7 @@ listen_port=%d if _, ok := selfSvc[svc]; ok { continue } - err = ConfigureRoute(n.Tun, itfName, prefix, *defaultAddr) + err = ConfigureRoute(n.Tun, itfName, prefix) if err != nil { return err } diff --git a/core/sys_darwin.go b/core/sys_darwin.go index 889159e..8953196 100644 --- a/core/sys_darwin.go +++ b/core/sys_darwin.go @@ -2,13 +2,14 @@ package core import ( "fmt" - "github.com/encodeous/nylon/polyamide/ipc" - "github.com/encodeous/nylon/polyamide/tun" - "github.com/encodeous/nylon/state" "net" "net/netip" "os/exec" "strings" + + "github.com/encodeous/nylon/polyamide/ipc" + "github.com/encodeous/nylon/polyamide/tun" + "github.com/encodeous/nylon/state" ) func VerifyForwarding() error { @@ -46,6 +47,6 @@ func ConfigureAlias(ifName string, prefix netip.Prefix) error { } } -func ConfigureRoute(dev tun.Device, itfName string, route netip.Prefix, via netip.Addr) error { - return Exec("/sbin/route", "-n", "add", "-net", route.String(), via.String()) +func ConfigureRoute(dev tun.Device, itfName string, route netip.Prefix) error { + return Exec("/sbin/route", "-n", "add", "-net", route.String(), "-interface", itfName) } diff --git a/core/sys_linux.go b/core/sys_linux.go index c3d3079..95971ab 100644 --- a/core/sys_linux.go +++ b/core/sys_linux.go @@ -45,10 +45,10 @@ func ConfigureAlias(ifName string, prefix netip.Prefix) error { return Exec("ip", "addr", "add", prefix.String(), "dev", ifName) } -func ConfigureRoute(dev tun.Device, itfName string, route netip.Prefix, via netip.Addr) error { +func ConfigureRoute(dev tun.Device, itfName string, route netip.Prefix) error { err := Exec("ip", "route", "flush", route.String()) if err != nil { return err } - return Exec("ip", "route", "add", route.String(), "via", via.String(), "dev", itfName) + return Exec("ip", "route", "add", route.String(), "dev", itfName) } diff --git a/core/sys_windows.go b/core/sys_windows.go index 6648788..09e1cab 100644 --- a/core/sys_windows.go +++ b/core/sys_windows.go @@ -2,14 +2,15 @@ package core import ( "fmt" - "github.com/encodeous/nylon/polyamide/ipc" - "github.com/encodeous/nylon/polyamide/tun" - "github.com/encodeous/nylon/state" - "github.com/kmahyyg/go-network-compo/wintypes" "net" "net/netip" "strconv" "strings" + + "github.com/encodeous/nylon/polyamide/ipc" + "github.com/encodeous/nylon/polyamide/tun" + "github.com/encodeous/nylon/state" + "github.com/kmahyyg/go-network-compo/wintypes" ) func VerifyForwarding() error { @@ -39,7 +40,7 @@ func ConfigureAlias(ifName string, prefix netip.Prefix) error { return Exec("netsh", "interface", "ip", "add", "address", ifName, addr.String(), maskStr) } -func ConfigureRoute(dev tun.Device, itfName string, route netip.Prefix, via netip.Addr) error { +func ConfigureRoute(dev tun.Device, itfName string, route netip.Prefix) error { addr := route.Addr() _, mask, _ := net.ParseCIDR(route.String()) maskStr := net.IP(mask.Mask).String() @@ -48,5 +49,5 @@ func ConfigureRoute(dev tun.Device, itfName string, route netip.Prefix, via neti if err != nil { return err } - return Exec("route", "add", addr.String(), "mask", maskStr, via.String(), "IF", strconv.FormatUint(uint64(itf.InterfaceIndex), 10)) + return Exec("route", "add", addr.String(), "mask", maskStr, "0.0.0.0", "IF", strconv.FormatUint(uint64(itf.InterfaceIndex), 10)) }