netmaker/netclient/local/routes.go

80 lines
2.5 KiB
Go
Raw Normal View History

2022-02-03 11:04:30 +08:00
package local
import (
"net"
"github.com/gravitl/netmaker/logger"
2022-02-03 11:04:30 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
// TODO handle ipv6 in future
2022-02-03 22:51:16 +08:00
// SetPeerRoutes - sets/removes ip routes for each peer on a network
2022-04-20 04:18:03 +08:00
func SetPeerRoutes(iface string, oldPeers map[string][]net.IPNet, newPeers []wgtypes.PeerConfig) {
2022-02-03 11:04:30 +08:00
// traverse through all recieved peers
for _, peer := range newPeers {
// if pubkey found in existing peers, check against existing peer
currPeerAllowedIPs := oldPeers[peer.PublicKey.String()]
if currPeerAllowedIPs != nil {
// traverse IPs, check to see if old peer contains each IP
for _, allowedIP := range peer.AllowedIPs { // compare new ones (if any) to old ones
if !ncutils.IPNetSliceContains(currPeerAllowedIPs, allowedIP) {
2022-02-04 08:55:12 +08:00
if err := setRoute(iface, &allowedIP, allowedIP.IP.String()); err != nil {
logger.Log(1, err.Error())
2022-02-03 11:04:30 +08:00
}
}
}
for _, allowedIP := range currPeerAllowedIPs { // compare old ones (if any) to new ones
if !ncutils.IPNetSliceContains(peer.AllowedIPs, allowedIP) {
2022-02-04 08:55:12 +08:00
if err := deleteRoute(iface, &allowedIP, allowedIP.IP.String()); err != nil {
logger.Log(1, err.Error())
2022-02-03 11:04:30 +08:00
}
}
}
2022-02-03 22:51:16 +08:00
delete(oldPeers, peer.PublicKey.String()) // remove peer as it was found and processed
2022-02-03 11:04:30 +08:00
} else {
2022-02-03 22:51:16 +08:00
for _, allowedIP := range peer.AllowedIPs { // add all routes as peer doesn't exist
2022-02-04 08:55:12 +08:00
if err := setRoute(iface, &allowedIP, allowedIP.String()); err != nil {
logger.Log(1, err.Error())
2022-02-03 11:04:30 +08:00
}
}
}
}
2022-02-03 22:51:16 +08:00
// traverse through all remaining existing peers
2022-02-03 11:04:30 +08:00
for _, allowedIPs := range oldPeers {
for _, allowedIP := range allowedIPs {
2022-02-04 08:55:12 +08:00
deleteRoute(iface, &allowedIP, allowedIP.IP.String())
}
}
}
// SetCurrentPeerRoutes - sets all the current peers
func SetCurrentPeerRoutes(iface, currentAddr string, peers []wgtypes.PeerConfig) {
2022-02-04 08:55:12 +08:00
for _, peer := range peers {
for _, allowedIP := range peer.AllowedIPs {
setRoute(iface, &allowedIP, currentAddr)
2022-02-03 11:04:30 +08:00
}
}
}
2022-02-06 04:00:26 +08:00
// FlushPeerRoutes - removes all current peer routes
func FlushPeerRoutes(iface, currentAddr string, peers []wgtypes.Peer) {
for _, peer := range peers {
for _, allowedIP := range peer.AllowedIPs {
deleteRoute(iface, &allowedIP, currentAddr)
}
}
}
// SetCIDRRoute - sets the CIDR route, used on join and restarts
func SetCIDRRoute(iface, currentAddr string, cidr *net.IPNet) {
setCidr(iface, currentAddr, cidr)
}
2022-02-06 04:00:26 +08:00
// RemoveCIDRRoute - removes a static cidr route
func RemoveCIDRRoute(iface, currentAddr string, cidr *net.IPNet) {
removeCidr(iface, cidr, currentAddr)
}