2021-05-26 00:48:04 +08:00
package functions
import (
2021-07-26 02:22:20 +08:00
"context"
2021-08-03 06:06:26 +08:00
"encoding/json"
2021-05-26 00:48:04 +08:00
"errors"
2021-07-26 02:22:20 +08:00
"fmt"
2021-11-04 03:43:10 +08:00
"log"
"os/exec"
2022-01-11 23:44:49 +08:00
"github.com/google/uuid"
2021-07-26 02:22:20 +08:00
nodepb "github.com/gravitl/netmaker/grpc"
2021-08-03 06:06:26 +08:00
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/netclient/auth"
2021-07-26 02:22:20 +08:00
"github.com/gravitl/netmaker/netclient/config"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/netclient/daemon"
2021-07-26 02:22:20 +08:00
"github.com/gravitl/netmaker/netclient/local"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
2021-07-26 02:22:20 +08:00
"github.com/gravitl/netmaker/netclient/server"
"github.com/gravitl/netmaker/netclient/wireguard"
2021-05-26 00:48:04 +08:00
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
2021-07-26 02:22:20 +08:00
"google.golang.org/grpc"
2021-05-26 00:48:04 +08:00
)
2021-10-09 03:07:12 +08:00
// JoinNetwork - helps a client join a network
2021-08-03 06:06:26 +08:00
func JoinNetwork ( cfg config . ClientConfig , privateKey string ) error {
2021-06-02 23:00:10 +08:00
if cfg . Node . Network == "" {
return errors . New ( "no network provided" )
}
2021-05-26 00:48:04 +08:00
2021-10-13 03:44:19 +08:00
var err error
if cfg . Node . IsServer != "yes" {
if local . HasNetwork ( cfg . Network ) {
err := errors . New ( "ALREADY_INSTALLED. Netclient appears to already be installed for " + cfg . Network + ". To re-install, please remove by executing 'sudo netclient leave -n " + cfg . Network + "'. Then re-run the install command." )
return err
}
2021-11-12 21:53:50 +08:00
2021-10-13 03:44:19 +08:00
err = config . Write ( & cfg , cfg . Network )
if err != nil {
return err
}
if cfg . Node . Password == "" {
cfg . Node . Password = ncutils . GenPass ( )
}
auth . StoreSecret ( cfg . Node . Password , cfg . Node . Network )
}
2021-09-11 03:48:18 +08:00
if cfg . Node . LocalRange != "" && cfg . Node . LocalAddress == "" {
log . Println ( "local vpn, getting local address from range: " + cfg . Node . LocalRange )
cfg . Node . LocalAddress = getLocalIP ( cfg . Node )
2021-05-26 00:48:04 +08:00
}
2021-09-11 03:48:18 +08:00
2021-09-20 02:03:47 +08:00
// set endpoint if blank. set to local if local net, retrieve from function if not
2021-07-26 02:22:20 +08:00
if cfg . Node . Endpoint == "" {
2021-05-26 00:48:04 +08:00
if cfg . Node . IsLocal == "yes" && cfg . Node . LocalAddress != "" {
cfg . Node . Endpoint = cfg . Node . LocalAddress
} else {
2021-09-20 02:03:47 +08:00
cfg . Node . Endpoint , err = ncutils . GetPublicIP ( )
2021-09-11 03:48:18 +08:00
}
if err != nil || cfg . Node . Endpoint == "" {
2021-09-20 02:03:47 +08:00
ncutils . Log ( "Error setting cfg.Node.Endpoint." )
2021-09-11 03:48:18 +08:00
return err
2021-05-26 00:48:04 +08:00
}
2021-07-26 02:22:20 +08:00
}
2021-09-11 03:48:18 +08:00
// Generate and set public/private WireGuard Keys
2021-08-03 06:06:26 +08:00
if privateKey == "" {
wgPrivatekey , err := wgtypes . GeneratePrivateKey ( )
2021-05-26 00:48:04 +08:00
if err != nil {
log . Fatal ( err )
}
2021-08-03 06:06:26 +08:00
privateKey = wgPrivatekey . String ( )
cfg . Node . PublicKey = wgPrivatekey . PublicKey ( ) . String ( )
2021-05-26 00:48:04 +08:00
}
2021-09-11 03:48:18 +08:00
// Find and set node MacAddress
2021-05-26 00:48:04 +08:00
if cfg . Node . MacAddress == "" {
2021-09-20 02:03:47 +08:00
macs , err := ncutils . GetMacAddr ( )
2021-05-26 00:48:04 +08:00
if err != nil {
return err
} else {
2021-07-26 02:22:20 +08:00
cfg . Node . MacAddress = macs [ 0 ]
2021-05-26 00:48:04 +08:00
}
}
2022-01-11 23:44:49 +08:00
if cfg . Node . ID == "" {
cfg . Node . ID = uuid . NewString ( )
}
2021-10-20 23:06:54 +08:00
if ncutils . IsLinux ( ) {
2022-01-06 04:17:28 +08:00
_ , err := exec . LookPath ( "resolvconf" )
2021-10-20 23:06:54 +08:00
if err != nil {
2022-01-06 04:17:28 +08:00
ncutils . PrintLog ( "resolvconf not present" , 2 )
2021-10-26 04:51:26 +08:00
ncutils . PrintLog ( "unable to configure DNS automatically, disabling automated DNS management" , 2 )
2021-10-20 23:06:54 +08:00
cfg . Node . DNSOn = "no"
2021-10-20 23:45:11 +08:00
}
2021-10-20 23:06:54 +08:00
}
2021-11-11 00:59:03 +08:00
if ncutils . IsFreeBSD ( ) {
cfg . Node . UDPHolePunch = "no"
}
2021-11-16 00:42:52 +08:00
// make sure name is appropriate, if not, give blank name
cfg . Node . Name = formatName ( cfg . Node )
2021-10-06 03:02:09 +08:00
// differentiate between client/server here
var node models . Node // fill this node with appropriate calls
2021-08-10 23:52:50 +08:00
postnode := & models . Node {
Password : cfg . Node . Password ,
2022-01-11 23:44:49 +08:00
ID : cfg . Node . ID ,
2021-08-10 23:52:50 +08:00
MacAddress : cfg . Node . MacAddress ,
AccessKey : cfg . Server . AccessKey ,
2021-11-11 00:59:03 +08:00
IsStatic : cfg . Node . IsStatic ,
2021-11-29 17:34:11 +08:00
Roaming : cfg . Node . Roaming ,
2021-08-10 23:52:50 +08:00
Network : cfg . Network ,
ListenPort : cfg . Node . ListenPort ,
PostUp : cfg . Node . PostUp ,
PostDown : cfg . Node . PostDown ,
PersistentKeepalive : cfg . Node . PersistentKeepalive ,
LocalAddress : cfg . Node . LocalAddress ,
Interface : cfg . Node . Interface ,
PublicKey : cfg . Node . PublicKey ,
2021-10-06 03:02:09 +08:00
DNSOn : cfg . Node . DNSOn ,
2021-08-10 23:52:50 +08:00
Name : cfg . Node . Name ,
Endpoint : cfg . Node . Endpoint ,
SaveConfig : cfg . Node . SaveConfig ,
UDPHolePunch : cfg . Node . UDPHolePunch ,
}
2021-08-19 06:59:17 +08:00
2021-10-06 03:02:09 +08:00
if cfg . Node . IsServer != "yes" {
ncutils . Log ( "joining " + cfg . Network + " at " + cfg . Server . GRPCAddress )
var wcclient nodepb . NodeServiceClient
2021-08-03 06:06:26 +08:00
2021-10-06 03:02:09 +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 " + cfg . Server . GRPCAddress + ": %v" , err )
}
defer conn . Close ( )
wcclient = nodepb . NewNodeServiceClient ( conn )
if err = config . ModConfig ( postnode ) ; err != nil {
return err
}
data , err := json . Marshal ( postnode )
if err != nil {
return err
}
// Create node on server
res , err := wcclient . CreateNode (
context . TODO ( ) ,
& nodepb . Object {
Data : string ( data ) ,
Type : nodepb . NODE_TYPE ,
} ,
)
if err != nil {
return err
}
ncutils . PrintLog ( "node created on remote server...updating configs" , 1 )
nodeData := res . Data
if err = json . Unmarshal ( [ ] byte ( nodeData ) , & node ) ; err != nil {
return err
}
2021-07-26 02:22:20 +08:00
}
2021-05-26 00:48:04 +08:00
2021-09-11 03:48:18 +08:00
// get free port based on returned default listen port
2021-09-20 02:03:47 +08:00
node . ListenPort , err = ncutils . GetFreePort ( node . ListenPort )
2021-09-11 03:48:18 +08:00
if err != nil {
fmt . Printf ( "Error retrieving port: %v" , err )
2021-05-26 00:48:04 +08:00
}
2021-09-20 02:03:47 +08:00
2021-09-11 03:48:18 +08:00
// safety check. If returned node from server is local, but not currently configured as local, set to local addr
if cfg . Node . IsLocal != "yes" && node . IsLocal == "yes" && node . LocalRange != "" {
2021-09-20 02:03:47 +08:00
node . LocalAddress , err = ncutils . GetLocalIP ( node . LocalRange )
2021-05-26 00:48:04 +08:00
if err != nil {
return err
}
2021-08-03 06:06:26 +08:00
node . Endpoint = node . LocalAddress
2021-05-26 00:48:04 +08:00
}
2021-11-11 00:59:03 +08:00
if ncutils . IsFreeBSD ( ) {
node . UDPHolePunch = "no"
cfg . Node . IsStatic = "yes"
}
2021-10-13 03:44:19 +08:00
if node . IsServer != "yes" { // == handle client side ==
err = config . ModConfig ( & node )
if err != nil {
return err
}
err = wireguard . StorePrivKey ( privateKey , cfg . Network )
if err != nil {
return err
}
if node . IsPending == "yes" {
ncutils . Log ( "Node is marked as PENDING." )
ncutils . Log ( "Awaiting approval from Admin before configuring WireGuard." )
if cfg . Daemon != "off" {
return daemon . InstallDaemon ( cfg )
}
}
// pushing any local changes to server before starting wireguard
err = Push ( cfg . Network )
if err != nil {
return err
2021-05-26 00:48:04 +08:00
}
2021-11-04 03:43:10 +08:00
// attempt to make backup
if err = config . SaveBackup ( node . Network ) ; err != nil {
ncutils . Log ( "failed to make backup, node will not auto restore if config is corrupted" )
}
2021-05-26 00:48:04 +08:00
}
2021-09-11 03:48:18 +08:00
2021-10-06 03:02:09 +08:00
ncutils . Log ( "retrieving peers" )
peers , hasGateway , gateways , err := server . GetPeers ( node . MacAddress , cfg . Network , cfg . Server . GRPCAddress , node . IsDualStack == "yes" , node . IsIngressGateway == "yes" , node . IsServer == "yes" )
2021-09-20 02:03:47 +08:00
if err != nil && ! ncutils . IsEmptyRecord ( err ) {
ncutils . Log ( "failed to retrieve peers" )
2021-07-26 02:22:20 +08:00
return err
}
2021-08-10 22:55:48 +08:00
2021-09-20 02:03:47 +08:00
ncutils . Log ( "starting wireguard" )
2021-11-13 00:24:29 +08:00
err = wireguard . InitWireguard ( & node , privateKey , peers , hasGateway , gateways , false )
2021-07-26 02:22:20 +08:00
if err != nil {
return err
}
2021-06-02 00:23:05 +08:00
if cfg . Daemon != "off" {
2021-09-20 02:03:47 +08:00
err = daemon . InstallDaemon ( cfg )
2021-05-26 00:48:04 +08:00
}
2021-07-26 02:22:20 +08:00
if err != nil {
return err
}
2021-05-26 00:48:04 +08:00
return err
}
2021-11-16 00:42:52 +08:00
// format name appropriately. Set to blank on failure
func formatName ( node models . Node ) string {
// Logic to properly format name
if ! node . NameInNodeCharSet ( ) {
node . Name = ncutils . DNSFormatString ( node . Name )
}
if len ( node . Name ) > models . MAX_NAME_LENGTH {
node . Name = ncutils . ShortenString ( node . Name , models . MAX_NAME_LENGTH )
}
if ! node . NameInNodeCharSet ( ) || len ( node . Name ) > models . MAX_NAME_LENGTH {
ncutils . PrintLog ( "could not properly format name: " + node . Name , 1 )
ncutils . PrintLog ( "setting name to blank" , 1 )
node . Name = ""
}
return node . Name
}