Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/gravitl/netclient into N…
Browse files Browse the repository at this point in the history
…ET-1061
  • Loading branch information
abhishek9686 committed May 6, 2024
2 parents 3db596e + 5718700 commit bf09bd5
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cache/iface_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ type EndpointCacheValue struct {

// ServerAddrCache - server addresses mapped to server names
var ServerAddrCache sync.Map // config.Server.Name -> []net.IP

// EgressRouteCache - Egress Route in local cache
var EgressRouteCache sync.Map
3 changes: 3 additions & 0 deletions functions/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func closeRoutines(closers []context.CancelFunc, wg *sync.WaitGroup) {
// clear cache
cache.EndpointCache = sync.Map{}
cache.SkipEndpointCache = sync.Map{}
cache.EgressRouteCache = sync.Map{}
signalThrottleCache = sync.Map{}
slog.Info("closing netmaker interface")
iface := wireguard.GetInterface()
Expand Down Expand Up @@ -221,6 +222,8 @@ func startGoRoutines(wg *sync.WaitGroup) context.CancelFunc {
wireguard.SetPeers(true)
if len(pullresp.EgressRoutes) > 0 {
wireguard.SetEgressRoutes(pullresp.EgressRoutes)
} else {
wireguard.RemoveEgressRoutes()
}
if pullErr == nil && pullresp.EndpointDetection {
go handleEndpointDetection(pullresp.Peers, pullresp.HostNetworkInfo)
Expand Down
4 changes: 4 additions & 0 deletions functions/mqhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ func HostPeerUpdate(client mqtt.Client, msg mqtt.Message) {
_ = wireguard.SetPeers(peerUpdate.ReplacePeers)
if len(peerUpdate.EgressRoutes) > 0 {
wireguard.SetEgressRoutes(peerUpdate.EgressRoutes)
} else {
wireguard.RemoveEgressRoutes()
}
if peerUpdate.EndpointDetection {
go handleEndpointDetection(peerUpdate.Peers, peerUpdate.HostNetworkInfo)
Expand Down Expand Up @@ -586,6 +588,8 @@ func mqFallbackPull(pullResponse models.HostPull, resetInterface, replacePeers b
_ = wireguard.SetPeers(replacePeers)
if len(pullResponse.EgressRoutes) > 0 {
wireguard.SetEgressRoutes(pullResponse.EgressRoutes)
} else {
wireguard.RemoveEgressRoutes()
}
if pullResponse.EndpointDetection {
go handleEndpointDetection(pullResponse.Peers, pullResponse.HostNetworkInfo)
Expand Down
12 changes: 12 additions & 0 deletions functions/use_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ func downloadVersion(version string) error {
freebsd := strings.Trim(freebsdVersion[0], "\"")
url = fmt.Sprintf("https://github.com/gravitl/netclient/releases/download/%s/netclient-%s%s-%s", version, runtime.GOOS, freebsd, runtime.GOARCH)
}
if runtime.GOARCH == "arm" && runtime.GOOS == "linux" {
out, err := ncutils.RunCmd("cat /proc/cpuinfo | grep architecture | head -1 | grep -o -E '[0-9]+'", false)
if err != nil {
return fmt.Errorf("get arm version %w", err)
}
if strings.Contains(out, "\r") {
out = strings.ReplaceAll(out, "\r", "")
} else if strings.Contains(out, "\n") {
out = strings.ReplaceAll(out, "\n", "")
}
url = fmt.Sprintf("https://github.com/gravitl/netclient/releases/download/%s/netclient-%s-%sv%s", version, runtime.GOOS, runtime.GOARCH, strings.TrimSpace(out))
}
res, err := http.Get(url)
if err != nil {
return err
Expand Down
44 changes: 43 additions & 1 deletion wireguard/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package wireguard
import (
"fmt"
"net"
"sort"
"sync"

"github.com/gravitl/netclient/cache"
"github.com/gravitl/netclient/config"
"github.com/gravitl/netclient/ncutils"
"github.com/gravitl/netmaker/logger"
Expand Down Expand Up @@ -102,7 +104,17 @@ func (n *NCIface) Configure() error {
return apply(&n.Config)
}

func RemoveEgressRoutes() {
if addrs, ok := cache.EgressRouteCache.Load(config.Netclient().Host.ID.String()); ok {
RemoveRoutes(addrs.([]ifaceAddress))
}

cache.EgressRouteCache = sync.Map{}
}

func SetEgressRoutes(egressRoutes []models.EgressNetworkRoutes) {
wgMutex.Lock()
defer wgMutex.Unlock()
addrs := []ifaceAddress{}
for _, egressRoute := range egressRoutes {
for _, egressRange := range egressRoute.EgressRanges {
Expand All @@ -125,7 +137,37 @@ func SetEgressRoutes(egressRoutes []models.EgressNetworkRoutes) {
}

}
SetRoutes(addrs)

if addrs1, ok := cache.EgressRouteCache.Load(config.Netclient().Host.ID.String()); ok {
isSame := checkEgressRoutes(addrs, addrs1.([]ifaceAddress))

if !isSame {
RemoveRoutes(addrs1.([]ifaceAddress))
SetRoutes(addrs)
cache.EgressRouteCache.Store(config.Netclient().Host.ID.String(), addrs)
}
} else {
SetRoutes(addrs)
cache.EgressRouteCache.Store(config.Netclient().Host.ID.String(), addrs)
}
}

// checkEgressRoutes - check if the addr are the same ones
func checkEgressRoutes(addrs, addrs1 []ifaceAddress) bool {
if len(addrs) != len(addrs1) {
return false
}

sort.Slice(addrs, func(i, j int) bool { return addrs[i].IP.String() < addrs[j].IP.String() })
sort.Slice(addrs1, func(i, j int) bool { return addrs1[i].IP.String() < addrs1[j].IP.String() })

for i := range addrs {
if addrs[i].IP.String() != addrs1[i].IP.String() || addrs[i].Network.String() != addrs1[i].Network.String() {
return false
}
}

return true
}

func GetInterface() *NCIface {
Expand Down
25 changes: 25 additions & 0 deletions wireguard/wireguard_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ func (nc *NCIface) ApplyAddrs() error {
return nil
}

// RemoveRoutes - remove routes to the interface
func RemoveRoutes(addrs []ifaceAddress) {
for _, addr := range addrs {
if addr.IP == nil || addr.Network.IP == nil || addr.Network.String() == "0.0.0.0/0" ||
addr.Network.String() == "::/0" {
continue
}

if addr.Network.IP.To4() != nil {
cmd := exec.Command("route", "delete", "-net", "-inet", addr.Network.String(), addr.IP.String())
if out, err := cmd.CombinedOutput(); err != nil {
slog.Error("failed to delete route with", "command", cmd.String(), "error", string(out))
continue
}
} else {
cmd := exec.Command("route", "delete", "-net", "-inet6", addr.Network.String(), addr.IP.String())
if out, err := cmd.CombinedOutput(); err != nil {
slog.Error("failed to delete route with", "command", cmd.String(), "error", string(out))
continue
}
}

}
}

// SetRoutes - sets additional routes to the interface
func SetRoutes(addrs []ifaceAddress) {
for _, addr := range addrs {
Expand Down
24 changes: 24 additions & 0 deletions wireguard/wireguard_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ func (nc *NCIface) ApplyAddrs() error {
return nil
}

// RemoveRoutes - Remove routes to the interface
func RemoveRoutes(addrs []ifaceAddress) {
l, err := netlink.LinkByName(ncutils.GetInterfaceName())
if err != nil {
slog.Error("failed to get link to interface", "error", err)
return
}

for _, addr := range addrs {
if addr.IP == nil || addr.Network.IP == nil || addr.Network.String() == IPv4Network ||
addr.Network.String() == IPv6Network {
continue
}
slog.Info("removing route to interface", "route", fmt.Sprintf("%s -> %s", addr.IP.String(), addr.Network.String()))
if err := netlink.RouteDel(&netlink.Route{
LinkIndex: l.Attrs().Index,
Src: addr.IP,
Dst: &addr.Network,
}); err != nil {
slog.Error("error removing route", "error", err.Error())
}
}
}

// SetRoutes - sets additional routes to the interface
func SetRoutes(addrs []ifaceAddress) {
l, err := netlink.LinkByName(ncutils.GetInterfaceName())
Expand Down
27 changes: 27 additions & 0 deletions wireguard/wireguard_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,33 @@ func (nc *NCIface) ApplyAddrs() error {
return adapter.(*driver.Adapter).LUID().SetIPAddresses(prefixAddrs)
}

// RemoveRoutes - remove routes to the interface
func RemoveRoutes(addrs []ifaceAddress) {
for _, addr := range addrs {
if addr.IP == nil || addr.Network.IP == nil || addr.Network.String() == IPv4Network ||
addr.Network.String() == IPv6Network {
continue
}
if addr.Network.IP.To4() != nil {
slog.Info("removing ipv4 route to interface", "route", fmt.Sprintf("%s -> %s", addr.IP.String(), addr.Network.String()))
cmd := fmt.Sprintf("netsh int ipv4 delete route %s interface=%s nexthop=%s store=%s",
addr.Network.String(), ncutils.GetInterfaceName(), "0.0.0.0", "active")
_, err := ncutils.RunCmd(cmd, false)
if err != nil {
slog.Error("failed to apply", "ipv4 egress range", addr.Network.String(), err.Error())
}
} else {
slog.Info("removing ipv6 route to interface", "route", fmt.Sprintf("%s -> %s", addr.IP.String(), addr.Network.String()))
cmd := fmt.Sprintf("netsh int ipv6 delete route %s interface=%s nexthop=%s store=%s",
addr.Network.String(), ncutils.GetInterfaceName(), "::", "active")
_, err := ncutils.RunCmd(cmd, false)
if err != nil {
slog.Error("failed to apply", "ipv6 egress range", addr.Network.String(), err.Error())
}
}
}
}

// SetRoutes - sets additional routes to the interface
func SetRoutes(addrs []ifaceAddress) {
for _, addr := range addrs {
Expand Down

0 comments on commit bf09bd5

Please sign in to comment.