netmaker/netclient/server/grpc.go

331 lines
9.7 KiB
Go
Raw Normal View History

2021-05-26 00:48:04 +08:00
package server
2021-05-20 01:59:10 +08:00
import (
2021-08-03 06:06:26 +08:00
"encoding/json"
2021-05-26 00:48:04 +08:00
"log"
"net"
2021-08-03 06:06:26 +08:00
"strconv"
2021-08-07 02:22:21 +08:00
"strings"
2021-05-26 00:48:04 +08:00
"time"
2021-08-03 06:06:26 +08:00
nodepb "github.com/gravitl/netmaker/grpc"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/netclient/auth"
2021-05-26 00:48:04 +08:00
"github.com/gravitl/netmaker/netclient/config"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
2021-08-03 06:06:26 +08:00
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"google.golang.org/grpc"
2021-05-26 00:48:04 +08:00
"google.golang.org/grpc/metadata"
2021-05-20 01:59:10 +08:00
)
2021-10-09 03:07:12 +08:00
// RELAY_KEEPALIVE_MARKER - sets the relay keepalive marker
const RELAY_KEEPALIVE_MARKER = "20007ms"
2021-08-03 06:06:26 +08:00
func getGrpcClient(cfg *config.ClientConfig) (nodepb.NodeServiceClient, error) {
var wcclient nodepb.NodeServiceClient
// == GRPC SETUP ==
2021-09-20 02:03:47 +08:00
conn, err := grpc.Dial(cfg.Server.GRPCAddress,
ncutils.GRPCRequestOpts(cfg.Server.GRPCSSL))
2021-08-03 06:06:26 +08:00
if err != nil {
return nil, err
2021-05-26 00:48:04 +08:00
}
2021-09-23 21:04:33 +08:00
defer conn.Close()
2021-08-03 06:06:26 +08:00
wcclient = nodepb.NewNodeServiceClient(conn)
return wcclient, nil
2021-05-26 00:48:04 +08:00
}
2021-10-09 03:07:12 +08:00
// CheckIn - checkin for node on a network
2021-08-03 06:06:26 +08:00
func CheckIn(network string) (*models.Node, error) {
cfg, err := config.ReadConfig(network)
if err != nil {
return nil, err
}
node := cfg.Node
if cfg.Node.IsServer != "yes" {
wcclient, err := getGrpcClient(cfg)
if err != nil {
return nil, err
}
// == run client action ==
var header metadata.MD
ctx, err := auth.SetJWT(wcclient, network)
nodeData, err := json.Marshal(&node)
if err != nil {
return nil, err
}
response, err := wcclient.ReadNode(
ctx,
&nodepb.Object{
Data: string(nodeData),
Type: nodepb.NODE_TYPE,
},
grpc.Header(&header),
)
if err != nil {
log.Printf("Encountered error checking in node: %v", err)
}
if err = json.Unmarshal([]byte(response.GetData()), &node); err != nil {
return nil, err
}
2021-08-03 06:06:26 +08:00
}
return &node, err
}
2021-05-26 00:48:04 +08:00
2021-10-09 03:07:12 +08:00
// GetPeers - gets the peers for a node
func GetPeers(macaddress string, network string, server string, dualstack bool, isIngressGateway bool, isServer bool) ([]wgtypes.PeerConfig, bool, []string, error) {
2021-08-03 06:06:26 +08:00
hasGateway := false
2021-10-13 03:44:19 +08:00
var err error
2021-08-03 06:06:26 +08:00
var gateways []string
var peers []wgtypes.PeerConfig
2021-10-13 03:44:19 +08:00
var nodecfg models.Node
var nodes []models.Node // fill above fields from server or client
if !isServer { // set peers client side
cfg, err := config.ReadConfig(network)
if err != nil {
log.Fatalf("Issue retrieving config for network: "+network+". Please investigate: %v", err)
}
nodecfg = cfg.Node
var wcclient nodepb.NodeServiceClient
conn, err := grpc.Dial(cfg.Server.GRPCAddress,
ncutils.GRPCRequestOpts(cfg.Server.GRPCSSL))
2021-05-20 01:59:10 +08:00
if err != nil {
log.Fatalf("Unable to establish client connection to localhost:50051: %v", err)
}
defer conn.Close()
// Instantiate the BlogServiceClient with our client connection to the server
wcclient = nodepb.NewNodeServiceClient(conn)
2021-07-16 03:14:48 +08:00
req := &nodepb.Object{
Data: macaddress + "###" + network,
Type: nodepb.STRING_TYPE,
}
2021-05-20 01:59:10 +08:00
ctx, err := auth.SetJWT(wcclient, network)
if err != nil {
log.Println("Failed to authenticate.")
return peers, hasGateway, gateways, err
}
var header metadata.MD
2021-08-06 04:46:23 +08:00
response, err := wcclient.GetPeers(ctx, req, grpc.Header(&header))
if err != nil {
log.Println("Error retrieving peers")
log.Println(err)
return nil, hasGateway, gateways, err
}
if err := json.Unmarshal([]byte(response.GetData()), &nodes); err != nil {
log.Println("Error unmarshaling data for peers")
return nil, hasGateway, gateways, err
}
2021-10-13 03:44:19 +08:00
}
keepalive := nodecfg.PersistentKeepalive
keepalivedur, err := time.ParseDuration(strconv.FormatInt(int64(keepalive), 10) + "s")
keepaliveserver, err := time.ParseDuration(strconv.FormatInt(int64(5), 10) + "s")
if err != nil {
log.Fatalf("Issue with format of keepalive value. Please update netconfig: %v", err)
2021-08-03 06:06:26 +08:00
}
2021-05-20 01:59:10 +08:00
2021-08-03 06:06:26 +08:00
for _, node := range nodes {
pubkey, err := wgtypes.ParseKey(node.PublicKey)
if err != nil {
2021-08-07 02:09:42 +08:00
log.Println("error parsing key")
2021-08-03 06:06:26 +08:00
return peers, hasGateway, gateways, err
}
2021-05-20 01:59:10 +08:00
2021-08-03 06:06:26 +08:00
if nodecfg.PublicKey == node.PublicKey {
continue
}
if nodecfg.Endpoint == node.Endpoint {
if nodecfg.LocalAddress != node.LocalAddress && node.LocalAddress != "" {
node.Endpoint = node.LocalAddress
2021-06-03 12:55:39 +08:00
} else {
continue
}
2021-08-03 06:06:26 +08:00
}
2021-05-20 01:59:10 +08:00
2021-08-03 06:06:26 +08:00
var peer wgtypes.PeerConfig
var peeraddr = net.IPNet{
IP: net.ParseIP(node.Address),
Mask: net.CIDRMask(32, 32),
}
var allowedips []net.IPNet
allowedips = append(allowedips, peeraddr)
// handle manually set peers
for _, allowedIp := range node.AllowedIPs {
if _, ipnet, err := net.ParseCIDR(allowedIp); err == nil {
nodeEndpointArr := strings.Split(node.Endpoint, ":")
2021-09-15 05:51:08 +08:00
if !ipnet.Contains(net.IP(nodeEndpointArr[0])) && ipnet.IP.String() != node.Address { // don't need to add an allowed ip that already exists..
allowedips = append(allowedips, *ipnet)
}
2021-09-15 05:51:08 +08:00
} else if appendip := net.ParseIP(allowedIp); appendip != nil && allowedIp != node.Address {
2021-09-20 02:03:47 +08:00
ipnet := net.IPNet{
IP: net.ParseIP(allowedIp),
Mask: net.CIDRMask(32, 32),
}
allowedips = append(allowedips, ipnet)
}
}
// handle egress gateway peers
2021-08-03 06:06:26 +08:00
if node.IsEgressGateway == "yes" {
hasGateway = true
ranges := node.EgressGatewayRanges
2021-08-12 21:01:35 +08:00
for _, iprange := range ranges { // go through each cidr for egress gateway
_, ipnet, err := net.ParseCIDR(iprange) // confirming it's valid cidr
if err != nil {
2021-09-23 02:31:31 +08:00
ncutils.PrintLog("could not parse gateway IP range. Not adding "+iprange, 1)
2021-08-12 21:01:35 +08:00
continue // if can't parse CIDR
}
2021-09-23 02:48:44 +08:00
nodeEndpointArr := strings.Split(node.Endpoint, ":") // getting the public ip of node
2021-09-23 21:04:33 +08:00
if ipnet.Contains(net.ParseIP(nodeEndpointArr[0])) { // ensuring egress gateway range does not contain public ip of node
2021-09-23 02:48:44 +08:00
ncutils.PrintLog("egress IP range of "+iprange+" overlaps with "+node.Endpoint+", omitting", 2)
2021-09-22 21:36:12 +08:00
continue // skip adding egress range if overlaps with node's ip
2021-08-07 03:29:34 +08:00
}
2021-09-23 21:04:33 +08:00
if ipnet.Contains(net.ParseIP(nodecfg.LocalAddress)) { // ensuring egress gateway range does not contain public ip of node
2021-09-23 02:48:44 +08:00
ncutils.PrintLog("egress IP range of "+iprange+" overlaps with "+nodecfg.LocalAddress+", omitting", 2)
continue // skip adding egress range if overlaps with node's local ip
}
2021-08-07 03:29:34 +08:00
gateways = append(gateways, iprange)
2021-08-03 06:06:26 +08:00
if err != nil {
2021-08-07 02:09:42 +08:00
log.Println("ERROR ENCOUNTERED SETTING GATEWAY")
2021-08-03 06:06:26 +08:00
} else {
2021-08-07 03:29:34 +08:00
allowedips = append(allowedips, *ipnet)
2021-08-03 06:06:26 +08:00
}
}
}
if node.Address6 != "" && dualstack {
var addr6 = net.IPNet{
IP: net.ParseIP(node.Address6),
Mask: net.CIDRMask(128, 128),
}
allowedips = append(allowedips, addr6)
}
2021-10-09 03:07:12 +08:00
if nodecfg.IsServer == "yes" && !(node.IsServer == "yes") {
2021-08-03 06:06:26 +08:00
peer = wgtypes.PeerConfig{
PublicKey: pubkey,
PersistentKeepaliveInterval: &keepaliveserver,
ReplaceAllowedIPs: true,
AllowedIPs: allowedips,
2021-07-27 22:48:58 +08:00
}
2021-07-27 01:57:50 +08:00
} else if keepalive != 0 {
2021-08-03 06:06:26 +08:00
peer = wgtypes.PeerConfig{
PublicKey: pubkey,
PersistentKeepaliveInterval: &keepalivedur,
Endpoint: &net.UDPAddr{
IP: net.ParseIP(node.Endpoint),
Port: int(node.ListenPort),
},
ReplaceAllowedIPs: true,
AllowedIPs: allowedips,
}
} else {
peer = wgtypes.PeerConfig{
PublicKey: pubkey,
Endpoint: &net.UDPAddr{
IP: net.ParseIP(node.Endpoint),
Port: int(node.ListenPort),
},
ReplaceAllowedIPs: true,
AllowedIPs: allowedips,
}
}
peers = append(peers, peer)
}
if isIngressGateway {
extPeers, err := GetExtPeers(macaddress, network, server, dualstack)
if err == nil {
peers = append(peers, extPeers...)
} else {
2021-09-20 02:03:47 +08:00
log.Println("ERROR RETRIEVING EXTERNAL PEERS", err)
2021-08-03 06:06:26 +08:00
}
}
return peers, hasGateway, gateways, err
2021-05-20 01:59:10 +08:00
}
2021-10-09 03:07:12 +08:00
// GetExtPeers - gets the extpeers for a client
2021-05-26 00:48:04 +08:00
func GetExtPeers(macaddress string, network string, server string, dualstack bool) ([]wgtypes.PeerConfig, error) {
2021-08-03 06:06:26 +08:00
var peers []wgtypes.PeerConfig
2021-10-13 03:44:19 +08:00
var nodecfg models.Node
var extPeers []models.Node
2021-10-13 03:44:19 +08:00
var err error
// fill above fields from either client or server
if nodecfg.IsServer != "yes" { // fill extPeers with client side logic
2021-10-13 03:44:19 +08:00
var cfg *config.ClientConfig
cfg, err = config.ReadConfig(network)
if err != nil {
log.Fatalf("Issue retrieving config for network: "+network+". Please investigate: %v", err)
}
nodecfg = cfg.Node
var wcclient nodepb.NodeServiceClient
2021-05-20 01:59:10 +08:00
conn, err := grpc.Dial(cfg.Server.GRPCAddress,
ncutils.GRPCRequestOpts(cfg.Server.GRPCSSL))
if err != nil {
log.Fatalf("Unable to establish client connection to localhost:50051: %v", err)
}
defer conn.Close()
// Instantiate the BlogServiceClient with our client connection to the server
wcclient = nodepb.NewNodeServiceClient(conn)
2021-05-20 01:59:10 +08:00
req := &nodepb.Object{
Data: macaddress + "###" + network,
Type: nodepb.STRING_TYPE,
}
ctx, err := auth.SetJWT(wcclient, network)
if err != nil {
log.Println("Failed to authenticate.")
return peers, err
}
var header metadata.MD
2021-05-20 01:59:10 +08:00
responseObject, err := wcclient.GetExtPeers(ctx, req, grpc.Header(&header))
if err != nil {
log.Println("Error retrieving peers")
log.Println(err)
return nil, err
}
if err = json.Unmarshal([]byte(responseObject.Data), &extPeers); err != nil {
return nil, err
}
2021-08-03 06:06:26 +08:00
}
for _, extPeer := range extPeers {
pubkey, err := wgtypes.ParseKey(extPeer.PublicKey)
if err != nil {
2021-08-07 02:09:42 +08:00
log.Println("error parsing key")
2021-08-03 06:06:26 +08:00
return peers, err
}
2021-05-20 01:59:10 +08:00
2021-08-03 06:06:26 +08:00
if nodecfg.PublicKey == extPeer.PublicKey {
continue
}
2021-05-20 01:59:10 +08:00
2021-08-03 06:06:26 +08:00
var peer wgtypes.PeerConfig
var peeraddr = net.IPNet{
IP: net.ParseIP(extPeer.Address),
Mask: net.CIDRMask(32, 32),
}
var allowedips []net.IPNet
allowedips = append(allowedips, peeraddr)
2021-05-20 01:59:10 +08:00
2021-08-03 06:06:26 +08:00
if extPeer.Address6 != "" && dualstack {
var addr6 = net.IPNet{
IP: net.ParseIP(extPeer.Address6),
Mask: net.CIDRMask(128, 128),
}
allowedips = append(allowedips, addr6)
}
peer = wgtypes.PeerConfig{
PublicKey: pubkey,
ReplaceAllowedIPs: true,
AllowedIPs: allowedips,
}
peers = append(peers, peer)
}
return peers, err
2021-05-20 01:59:10 +08:00
}