User Ip as next hop for default routes on darwin

This commit is contained in:
Maycon Santos 2024-05-10 14:45:02 +02:00
parent 272ade07a8
commit b0b755b08b
3 changed files with 9 additions and 4 deletions

View File

@ -53,6 +53,7 @@ type DefaultManager struct {
}
func NewManager(ctx context.Context, pubKey string, wgInterface *iface.WGIface, statusRecorder *peer.Status, initialRoutes []*route.Route) *DefaultManager {
tunIP = netip.MustParseAddr(wgInterface.Address().IP.String())
mCTX, cancel := context.WithCancel(ctx)
dm := &DefaultManager{
ctx: mCTX,

View File

@ -28,6 +28,8 @@ var splitDefaultv6_2 = netip.PrefixFrom(netip.AddrFrom16([16]byte{0x80}), 1)
var ErrRouteNotFound = errors.New("route not found")
var ErrRouteNotAllowed = errors.New("route not allowed")
var tunIP netip.Addr
// TODO: fix: for default our wg address now appears as the default gw
func addRouteForCurrentDefaultGateway(prefix netip.Prefix) error {
addr := netip.IPv4Unspecified()
@ -196,10 +198,10 @@ func addRouteToNonVPNIntf(prefix netip.Prefix, vpnIntf *iface.WGIface, initialNe
// in two /1 prefixes to avoid replacing the existing default route
func genericAddVPNRoute(prefix netip.Prefix, intf *net.Interface) error {
if prefix == defaultv4 {
if err := addToRouteTable(splitDefaultv4_1, netip.Addr{}, intf); err != nil {
if err := addToRouteTable(splitDefaultv4_1, tunIP, intf); err != nil {
return err
}
if err := addToRouteTable(splitDefaultv4_2, netip.Addr{}, intf); err != nil {
if err := addToRouteTable(splitDefaultv4_2, tunIP, intf); err != nil {
if err2 := removeFromRouteTable(splitDefaultv4_1, netip.Addr{}, intf); err2 != nil {
log.Warnf("Failed to rollback route addition: %s", err2)
}
@ -266,10 +268,10 @@ func addNonExistingRoute(prefix netip.Prefix, intf *net.Interface) error {
func genericRemoveVPNRoute(prefix netip.Prefix, intf *net.Interface) error {
if prefix == defaultv4 {
var result *multierror.Error
if err := removeFromRouteTable(splitDefaultv4_1, netip.Addr{}, intf); err != nil {
if err := removeFromRouteTable(splitDefaultv4_1, tunIP, intf); err != nil {
result = multierror.Append(result, err)
}
if err := removeFromRouteTable(splitDefaultv4_2, netip.Addr{}, intf); err != nil {
if err := removeFromRouteTable(splitDefaultv4_2, tunIP, intf); err != nil {
result = multierror.Append(result, err)
}

View File

@ -52,8 +52,10 @@ func routeCmd(action string, prefix netip.Prefix, nexthop netip.Addr, intf *net.
args := []string{"-n", action, inet, network}
if nexthop.IsValid() {
log.Debugf("route %s %s via %s", action, prefix, nexthop)
args = append(args, nexthop.Unmap().String())
} else if intf != nil {
log.Debugf("route %s %s via %s, iptun: %s", action, prefix, intf.Name, tunIP)
args = append(args, "-interface", intf.Name)
}