Browse Source

update routes to use iplib

Matthew R. Kasun 3 years ago
parent
commit
62b8b9bb8e
6 changed files with 14 additions and 23 deletions
  1. 0 2
      go.mod
  2. 0 4
      go.sum
  3. 5 5
      logic/server.go
  4. 3 4
      netclient/local/routes_darwin.go
  5. 3 4
      netclient/local/routes_freebsd.go
  6. 3 4
      netclient/local/routes_linux.go

+ 0 - 2
go.mod

@@ -37,7 +37,6 @@ require (
 	github.com/guumaster/hostctl v1.1.2
 	github.com/kr/pretty v0.3.0
 	github.com/posthog/posthog-go v0.0.0-20211028072449-93c17c49e2b0
-	github.com/seancfoley/ipaddress-go v1.1.2
 )
 
 require (
@@ -74,7 +73,6 @@ require (
 	github.com/pmezard/go-difflib v1.0.0 // indirect
 	github.com/rogpeppe/go-internal v1.8.0 // indirect
 	github.com/russross/blackfriday/v2 v2.1.0 // indirect
-	github.com/seancfoley/bintree v1.0.1 // indirect
 	github.com/spf13/afero v1.3.2 // indirect
 	github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564 // indirect
 	github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 // indirect

+ 0 - 4
go.sum

@@ -202,10 +202,6 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
 github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
-github.com/seancfoley/bintree v1.0.1 h1:rCb5DEugf2B2DtkrxJ80CP6HT24yohDEcXPOqkQRizo=
-github.com/seancfoley/bintree v1.0.1/go.mod h1:CtE6qO6/n9H3V2CAGEC0lpaYr6/OijhNaMG/dt7P70c=
-github.com/seancfoley/ipaddress-go v1.1.2 h1:zeAUfL7foAPe1pIlT2agp17tgpwzU6YKuEAa2VrRKOw=
-github.com/seancfoley/ipaddress-go v1.1.2/go.mod h1:gR/Gr3Sx+pzusadtM9s98e/tZjvL4YnumYTPcKoHWec=
 github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
 github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
 github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=

+ 5 - 5
logic/server.go

@@ -10,13 +10,13 @@ import (
 	"strings"
 	"time"
 
+	"github.com/c-robinson/iplib"
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/logic/acls"
 	"github.com/gravitl/netmaker/logic/acls/nodeacls"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/netclient/ncutils"
 	"github.com/gravitl/netmaker/servercfg"
-	"github.com/seancfoley/ipaddress-go/ipaddr"
 	"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
 )
 
@@ -261,8 +261,7 @@ func GetServerPeers(serverNode *models.Node) ([]wgtypes.PeerConfig, bool, []stri
 
 		// handle manually set peers
 		for _, allowedIp := range node.AllowedIPs {
-			currentIP := ipaddr.NewIPAddressString(allowedIp).GetAddress()
-			if currentIP.IsIPv4() {
+			if iplib.Version(net.ParseIP(allowedIp)) == 4 {
 				if _, ipnet, err := net.ParseCIDR(allowedIp); err == nil {
 					nodeEndpointArr := strings.Split(node.Endpoint, ":")
 					if !ipnet.Contains(net.IP(nodeEndpointArr[0])) && ipnet.IP.String() != node.Address { // don't need to add an allowed ip that already exists..
@@ -275,9 +274,10 @@ func GetServerPeers(serverNode *models.Node) ([]wgtypes.PeerConfig, bool, []stri
 					}
 					allowedips = append(allowedips, ipnet)
 				}
-			} else if currentIP.IsIPv6() {
+			} else if iplib.Version(net.ParseIP(allowedIp)) == 6 {
+				//ipnet : = iplib.Net6FromStr(allowedIp).IP()
 				ipnet := net.IPNet{
-					IP:   currentIP.GetNetIP(),
+					IP:   iplib.Net6FromStr(allowedIp).IP(),
 					Mask: net.CIDRMask(128, 128),
 				}
 				allowedips = append(allowedips, ipnet)

+ 3 - 4
netclient/local/routes_darwin.go

@@ -4,9 +4,9 @@ import (
 	"net"
 	"strings"
 
+	"github.com/c-robinson/iplib"
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/netclient/ncutils"
-	"github.com/seancfoley/ipaddress-go/ipaddr"
 )
 
 // route -n add -net 10.0.0.0/8 192.168.0.254
@@ -35,10 +35,9 @@ func deleteRoute(iface string, addr *net.IPNet, address string) error {
 }
 
 func setCidr(iface, address string, addr *net.IPNet) {
-	cidr := ipaddr.NewIPAddressString(addr.String()).GetAddress()
-	if cidr.IsIPv4() {
+	if iplib.Version(addr.IP) == 4 {
 		ncutils.RunCmd("route -q -n add -net "+addr.String()+" "+address, false)
-	} else if cidr.IsIPv6() {
+	} else if iplib.Version(addr.IP) == 6 {
 		ncutils.RunCmd("route -A inet6 -q -n add -net "+addr.String()+" "+address, false)
 	} else {
 		logger.Log(1, "could not parse address: "+addr.String())

+ 3 - 4
netclient/local/routes_freebsd.go

@@ -3,9 +3,9 @@ package local
 import (
 	"net"
 
+	"github.com/c-robinson/iplib"
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/netclient/ncutils"
-	"github.com/seancfoley/ipaddress-go/ipaddr"
 )
 
 func setRoute(iface string, addr *net.IPNet, address string) error {
@@ -21,10 +21,9 @@ func deleteRoute(iface string, addr *net.IPNet, address string) error {
 }
 
 func setCidr(iface, address string, addr *net.IPNet) {
-	cidr := ipaddr.NewIPAddressString(addr.String()).GetAddress()
-	if cidr.IsIPv4() {
+	if iplib.Version(addr.IP) == 4 {
 		ncutils.RunCmd("route add -net "+addr.String()+" -interface "+iface, false)
-	} else if cidr.IsIPv6() {
+	} else if iplib.Version(addr.IP) == 6 {
 		ncutils.RunCmd("route add -net -inet6 "+addr.String()+" -interface "+iface, false)
 	} else {
 		logger.Log(1, "could not parse address: "+addr.String())

+ 3 - 4
netclient/local/routes_linux.go

@@ -7,9 +7,9 @@ import (
 	"net"
 	"strings"
 
+	"github.com/c-robinson/iplib"
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/netclient/ncutils"
-	"github.com/seancfoley/ipaddress-go/ipaddr"
 )
 
 func setRoute(iface string, addr *net.IPNet, address string) error {
@@ -30,10 +30,9 @@ func deleteRoute(iface string, addr *net.IPNet, address string) error {
 }
 
 func setCidr(iface, address string, addr *net.IPNet) {
-	cidr := ipaddr.NewIPAddressString(addr.String()).GetAddress()
-	if cidr.IsIPv4() {
+	if iplib.Version(addr.IP) == 4 {
 		ncutils.RunCmd("ip -4 route add "+addr.String()+" dev "+iface, false)
-	} else if cidr.IsIPv6() {
+	} else if iplib.Version(addr.IP) == 6 {
 		ncutils.RunCmd("ip -6 route add "+addr.String()+" dev "+iface, false)
 	} else {
 		logger.Log(1, "could not parse address: "+addr.String())