netmaker/netclient/functions/join.go

259 lines
6.1 KiB
Go
Raw Normal View History

2021-05-26 00:48:04 +08:00
package functions
import (
2021-07-26 02:22:20 +08:00
"context"
2021-07-11 08:11:19 +08:00
"crypto/tls"
2021-05-26 00:48:04 +08:00
"errors"
2021-07-26 02:22:20 +08:00
"fmt"
2021-05-26 00:48:04 +08:00
"log"
2021-06-02 23:00:10 +08:00
"math/rand"
2021-07-26 02:22:20 +08:00
"net"
2021-06-02 23:00:10 +08:00
"time"
2021-07-26 02:22:20 +08:00
nodepb "github.com/gravitl/netmaker/grpc"
"github.com/gravitl/netmaker/netclient/config"
"github.com/gravitl/netmaker/netclient/local"
"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"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
2021-07-26 02:22:20 +08:00
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
2021-05-26 00:48:04 +08:00
//homedir "github.com/mitchellh/go-homedir"
)
func JoinNetwork(cfg config.ClientConfig) error {
hasnet := local.HasNetwork(cfg.Network)
2021-06-02 23:00:10 +08:00
if hasnet {
2021-07-26 02:22:20 +08:00
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.")
2021-05-26 00:48:04 +08:00
return err
}
2021-07-11 22:18:31 +08:00
log.Println("attempting to join " + cfg.Network + " at " + cfg.Server.GRPCAddress)
2021-05-26 00:48:04 +08:00
err := config.Write(&cfg, cfg.Network)
if err != nil {
return err
}
2021-07-26 02:22:20 +08:00
wgclient, err := wgctrl.New()
if err != nil {
2021-05-26 00:48:04 +08:00
return err
2021-07-26 02:22:20 +08:00
}
defer wgclient.Close()
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
if cfg.Node.LocalRange != "" {
2021-07-26 02:22:20 +08:00
if cfg.Node.LocalAddress == "" {
log.Println("local vpn, getting local address from range: " + cfg.Node.LocalRange)
ifaces, err := net.Interfaces()
if err != nil {
return err
2021-05-26 00:48:04 +08:00
}
2021-07-26 02:22:20 +08:00
_, localrange, err := net.ParseCIDR(cfg.Node.LocalRange)
2021-05-26 00:48:04 +08:00
if err != nil {
return err
}
2021-07-26 02:22:20 +08:00
var local string
found := false
for _, i := range ifaces {
if i.Flags&net.FlagUp == 0 {
continue // interface down
}
if i.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := i.Addrs()
if err != nil {
return err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
if !found {
ip = v.IP
local = ip.String()
if cfg.Node.IsLocal == "yes" {
found = localrange.Contains(ip)
} else {
found = true
}
2021-05-26 00:48:04 +08:00
}
2021-07-26 02:22:20 +08:00
case *net.IPAddr:
if !found {
ip = v.IP
local = ip.String()
if cfg.Node.IsLocal == "yes" {
found = localrange.Contains(ip)
2021-05-26 00:48:04 +08:00
2021-07-26 02:22:20 +08:00
} else {
found = true
}
2021-05-26 00:48:04 +08:00
}
}
}
}
2021-07-26 02:22:20 +08:00
cfg.Node.LocalAddress = local
2021-05-26 00:48:04 +08:00
}
}
2021-06-02 23:00:10 +08:00
if cfg.Node.Password == "" {
cfg.Node.Password = GenPass()
}
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 {
cfg.Node.Endpoint, err = getPublicIP()
if err != nil {
fmt.Println("Error setting cfg.Node.Endpoint.")
return err
}
}
2021-07-26 02:22:20 +08:00
} else {
cfg.Node.Endpoint = cfg.Node.Endpoint
}
2021-05-26 00:48:04 +08:00
if cfg.Node.PrivateKey == "" {
privatekey, err := wgtypes.GeneratePrivateKey()
if err != nil {
log.Fatal(err)
}
cfg.Node.PrivateKey = privatekey.String()
cfg.Node.PublicKey = privatekey.PublicKey().String()
}
if cfg.Node.MacAddress == "" {
macs, err := getMacAddr()
if err != nil {
return err
} else if len(macs) == 0 {
log.Fatal()
} else {
2021-07-26 02:22:20 +08:00
cfg.Node.MacAddress = macs[0]
2021-05-26 00:48:04 +08:00
}
}
2021-06-02 23:00:10 +08:00
if cfg.Node.Port == 0 {
cfg.Node.Port, err = GetFreePort(51821)
if err != nil {
fmt.Printf("Error retrieving port: %v", err)
}
}
2021-05-26 00:48:04 +08:00
var wcclient nodepb.NodeServiceClient
var requestOpts grpc.DialOption
2021-07-11 22:18:31 +08:00
requestOpts = grpc.WithInsecure()
if cfg.Server.GRPCSSL == "on" {
h2creds := credentials.NewTLS(&tls.Config{NextProtos: []string{"h2"}})
requestOpts = grpc.WithTransportCredentials(h2creds)
}
2021-07-11 08:11:19 +08:00
conn, err := grpc.Dial(cfg.Server.GRPCAddress, requestOpts)
2021-07-26 02:22:20 +08:00
if err != nil {
log.Fatalf("Unable to establish client connection to "+cfg.Server.GRPCAddress+": %v", err)
}
2021-07-11 08:11:19 +08:00
2021-07-26 02:22:20 +08:00
wcclient = nodepb.NewNodeServiceClient(conn)
2021-05-26 00:48:04 +08:00
2021-07-26 02:22:20 +08:00
postnode := &nodepb.Node{
Password: cfg.Node.Password,
Macaddress: cfg.Node.MacAddress,
Accesskey: cfg.Server.AccessKey,
Nodenetwork: cfg.Network,
Listenport: cfg.Node.Port,
Postup: cfg.Node.PostUp,
Postdown: cfg.Node.PostDown,
Keepalive: cfg.Node.KeepAlive,
2021-05-26 00:48:04 +08:00
Localaddress: cfg.Node.LocalAddress,
2021-07-26 02:22:20 +08:00
Interface: cfg.Node.Interface,
Publickey: cfg.Node.PublicKey,
Name: cfg.Node.Name,
Endpoint: cfg.Node.Endpoint,
2021-07-26 04:05:31 +08:00
Saveconfig: cfg.Node.SaveConfig,
Udpholepunch: cfg.Node.UDPHolePunch,
2021-07-26 02:22:20 +08:00
}
err = config.ModConfig(postnode)
if err != nil {
2021-05-26 00:48:04 +08:00
return err
2021-07-26 02:22:20 +08:00
}
2021-07-11 08:11:19 +08:00
2021-07-26 02:22:20 +08:00
res, err := wcclient.CreateNode(
context.TODO(),
&nodepb.CreateNodeReq{
Node: postnode,
},
)
if err != nil {
return err
}
2021-07-16 03:14:48 +08:00
log.Println("node created on remote server...updating configs")
2021-07-26 02:22:20 +08:00
node := res.Node
if err != nil {
return err
}
2021-05-26 00:48:04 +08:00
2021-07-26 02:22:20 +08:00
if node.Dnsoff == true {
2021-05-26 00:48:04 +08:00
cfg.Node.DNS = "yes"
}
if !(cfg.Node.IsLocal == "yes") && node.Islocal && node.Localrange != "" {
node.Localaddress, err = getLocalIP(node.Localrange)
if err != nil {
return err
}
node.Endpoint = node.Localaddress
}
2021-07-26 02:22:20 +08:00
err = config.ModConfig(node)
if err != nil {
return err
}
2021-05-26 00:48:04 +08:00
if node.Ispending {
fmt.Println("Node is marked as PENDING.")
fmt.Println("Awaiting approval from Admin before configuring WireGuard.")
2021-07-26 02:22:20 +08:00
if cfg.Daemon != "off" {
2021-05-26 00:48:04 +08:00
err = local.ConfigureSystemD(cfg.Network)
return err
}
}
2021-07-16 03:14:48 +08:00
log.Println("retrieving remote peers")
2021-05-26 04:09:49 +08:00
peers, hasGateway, gateways, err := server.GetPeers(node.Macaddress, cfg.Network, cfg.Server.GRPCAddress, node.Isdualstack, node.Isingressgateway)
2021-05-26 00:48:04 +08:00
if err != nil {
2021-07-16 03:14:48 +08:00
log.Println("failed to retrieve peers")
2021-07-26 02:22:20 +08:00
return err
}
2021-05-26 00:48:04 +08:00
err = wireguard.StorePrivKey(cfg.Node.PrivateKey, cfg.Network)
2021-07-26 02:22:20 +08:00
if err != nil {
return err
}
log.Println("starting wireguard")
2021-05-26 00:48:04 +08:00
err = wireguard.InitWireguard(node, cfg.Node.PrivateKey, peers, hasGateway, gateways)
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-05-26 00:48:04 +08:00
err = local.ConfigureSystemD(cfg.Network)
}
2021-07-26 02:22:20 +08:00
if err != nil {
return err
}
2021-05-26 00:48:04 +08:00
return err
}
2021-06-02 23:00:10 +08:00
//generate an access key value
func GenPass() string {
2021-07-26 02:22:20 +08:00
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
2021-06-02 23:00:10 +08:00
2021-07-26 02:22:20 +08:00
length := 16
charset := "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
2021-06-02 23:00:10 +08:00
2021-07-26 02:22:20 +08:00
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
2021-06-02 23:00:10 +08:00
}