mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-06 05:04:27 +08:00
update routes to use iplib
This commit is contained in:
parent
ef298fa343
commit
62b8b9bb8e
6 changed files with 14 additions and 23 deletions
2
go.mod
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
|
||||
|
|
4
go.sum
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=
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,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())
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Add table
Reference in a new issue