2021-03-26 00:17:52 +08:00
|
|
|
package functions
|
|
|
|
|
|
|
|
import (
|
2021-08-03 06:06:26 +08:00
|
|
|
"context"
|
2021-05-30 01:22:18 +08:00
|
|
|
"encoding/json"
|
2021-03-26 22:35:52 +08:00
|
|
|
"errors"
|
2021-08-03 06:06:26 +08:00
|
|
|
"fmt"
|
2021-03-26 00:17:52 +08:00
|
|
|
"log"
|
|
|
|
"net"
|
2021-09-20 02:03:47 +08:00
|
|
|
"os"
|
2021-03-26 00:17:52 +08:00
|
|
|
"os/exec"
|
2021-08-03 06:06:26 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
nodepb "github.com/gravitl/netmaker/grpc"
|
2021-10-06 03:02:09 +08:00
|
|
|
"github.com/gravitl/netmaker/logic"
|
2021-08-03 06:06:26 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
|
"github.com/gravitl/netmaker/netclient/auth"
|
|
|
|
"github.com/gravitl/netmaker/netclient/config"
|
2021-09-20 02:03:47 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/daemon"
|
|
|
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
|
|
|
"github.com/gravitl/netmaker/netclient/wireguard"
|
2021-03-26 00:17:52 +08:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
2021-08-03 06:06:26 +08:00
|
|
|
"google.golang.org/grpc"
|
2021-03-26 00:17:52 +08:00
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-08-03 06:06:26 +08:00
|
|
|
wcclient nodepb.NodeServiceClient
|
2021-03-26 00:17:52 +08:00
|
|
|
)
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// ListPorts - lists ports of WireGuard devices
|
2021-08-03 06:06:26 +08:00
|
|
|
func ListPorts() error {
|
2021-04-07 09:31:47 +08:00
|
|
|
wgclient, err := wgctrl.New()
|
2021-08-03 06:06:26 +08:00
|
|
|
if err != nil {
|
2021-04-07 09:31:47 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
devices, err := wgclient.Devices()
|
2021-08-03 06:06:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-07 09:31:47 +08:00
|
|
|
fmt.Println("Here are your ports:")
|
2021-08-03 06:06:26 +08:00
|
|
|
for _, i := range devices {
|
2021-04-07 09:31:47 +08:00
|
|
|
fmt.Println(i.ListenPort)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-31 03:58:23 +08:00
|
|
|
func getPrivateAddr() (string, error) {
|
2021-04-07 09:31:47 +08:00
|
|
|
|
2021-08-03 06:06:26 +08:00
|
|
|
var local string
|
2021-08-31 03:58:23 +08:00
|
|
|
conn, err := net.Dial("udp", "8.8.8.8:80")
|
2021-08-03 06:06:26 +08:00
|
|
|
if err != nil {
|
2021-08-31 03:58:23 +08:00
|
|
|
log.Fatal(err)
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
2021-08-31 03:58:23 +08:00
|
|
|
defer conn.Close()
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-08-31 03:58:23 +08:00
|
|
|
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
|
|
|
localIP := localAddr.IP
|
|
|
|
local = localIP.String()
|
2021-09-23 00:03:33 +08:00
|
|
|
if local == "" {
|
|
|
|
local, err = getPrivateAddrBackup()
|
|
|
|
}
|
2021-08-31 03:58:23 +08:00
|
|
|
if local == "" {
|
|
|
|
err = errors.New("could not find local ip")
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
|
|
|
return local, err
|
2021-03-26 22:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-09-23 00:03:33 +08:00
|
|
|
func getPrivateAddrBackup() (string, error) {
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
case *net.IPAddr:
|
|
|
|
if !found {
|
|
|
|
ip = v.IP
|
|
|
|
local = ip.String()
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
err := errors.New("Local Address Not Found.")
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return local, err
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:42:35 +08:00
|
|
|
func needInterfaceUpdate(ctx context.Context, mac string, network string, iface string) (bool, string, error) {
|
2021-08-03 06:06:26 +08:00
|
|
|
var header metadata.MD
|
|
|
|
req := &nodepb.Object{
|
|
|
|
Data: mac + "###" + network,
|
|
|
|
Type: nodepb.STRING_TYPE,
|
|
|
|
}
|
|
|
|
readres, err := wcclient.ReadNode(ctx, req, grpc.Header(&header))
|
|
|
|
if err != nil {
|
|
|
|
return false, "", err
|
|
|
|
}
|
|
|
|
var resNode models.Node
|
|
|
|
if err := json.Unmarshal([]byte(readres.Data), &resNode); err != nil {
|
|
|
|
return false, iface, err
|
|
|
|
}
|
|
|
|
oldiface := resNode.Interface
|
2021-03-26 12:04:04 +08:00
|
|
|
|
2021-08-03 06:06:26 +08:00
|
|
|
return iface != oldiface, oldiface, err
|
2021-03-26 12:04:04 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// GetNode - gets node locally
|
2021-08-03 06:06:26 +08:00
|
|
|
func GetNode(network string) models.Node {
|
2021-04-05 23:06:30 +08:00
|
|
|
|
2021-08-03 06:06:26 +08:00
|
|
|
modcfg, err := config.ReadConfig(network)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error: %v", err)
|
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-08-03 06:06:26 +08:00
|
|
|
return modcfg.Node
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// Uninstall - uninstalls networks from client
|
2021-05-30 01:22:18 +08:00
|
|
|
func Uninstall() error {
|
2021-10-06 03:02:09 +08:00
|
|
|
networks, err := ncutils.GetSystemNetworks()
|
2021-05-30 01:22:18 +08:00
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("unable to retrieve networks: "+err.Error(), 1)
|
|
|
|
ncutils.PrintLog("continuing uninstall without leaving networks", 1)
|
2021-05-30 01:22:18 +08:00
|
|
|
} else {
|
|
|
|
for _, network := range networks {
|
|
|
|
err = LeaveNetwork(network)
|
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("Encounter issue leaving network "+network+": "+err.Error(), 1)
|
2021-05-30 01:22:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-31 03:58:23 +08:00
|
|
|
// clean up OS specific stuff
|
2021-09-20 02:03:47 +08:00
|
|
|
if ncutils.IsWindows() {
|
|
|
|
daemon.CleanupWindows()
|
2021-09-22 02:01:52 +08:00
|
|
|
} else if ncutils.IsMac() {
|
2021-09-20 02:03:47 +08:00
|
|
|
daemon.CleanupMac()
|
2021-09-22 09:35:52 +08:00
|
|
|
} else if !ncutils.IsKernel() {
|
2021-09-23 21:04:33 +08:00
|
|
|
ncutils.PrintLog("manual cleanup required", 1)
|
2021-08-31 03:58:23 +08:00
|
|
|
}
|
2021-09-18 23:01:34 +08:00
|
|
|
|
2021-05-30 01:22:18 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// LeaveNetwork - client exits a network
|
2021-05-26 00:48:04 +08:00
|
|
|
func LeaveNetwork(network string) error {
|
2021-08-03 06:06:26 +08:00
|
|
|
cfg, err := config.ReadConfig(network)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-05 23:06:30 +08:00
|
|
|
servercfg := cfg.Server
|
2021-08-03 06:06:26 +08:00
|
|
|
node := cfg.Node
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-10-06 03:02:09 +08:00
|
|
|
if node.IsServer != "yes" {
|
|
|
|
var wcclient nodepb.NodeServiceClient
|
|
|
|
conn, err := grpc.Dial(cfg.Server.GRPCAddress,
|
|
|
|
ncutils.GRPCRequestOpts(cfg.Server.GRPCSSL))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to establish client connection to "+servercfg.GRPCAddress+": %v", err)
|
|
|
|
}
|
2021-09-23 21:04:33 +08:00
|
|
|
defer conn.Close()
|
2021-05-26 00:48:04 +08:00
|
|
|
wcclient = nodepb.NewNodeServiceClient(conn)
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-09-22 05:50:09 +08:00
|
|
|
ctx, err := auth.SetJWT(wcclient, network)
|
2021-05-26 00:48:04 +08:00
|
|
|
if err != nil {
|
2021-08-03 06:06:26 +08:00
|
|
|
log.Printf("Failed to authenticate: %v", err)
|
2021-10-06 03:02:09 +08:00
|
|
|
} else { // handle client side
|
2021-08-10 10:31:01 +08:00
|
|
|
node.SetID()
|
2021-05-26 00:48:04 +08:00
|
|
|
var header metadata.MD
|
|
|
|
_, err = wcclient.DeleteNode(
|
2021-08-03 06:06:26 +08:00
|
|
|
ctx,
|
|
|
|
&nodepb.Object{
|
|
|
|
Data: node.ID,
|
|
|
|
Type: nodepb.STRING_TYPE,
|
|
|
|
},
|
|
|
|
grpc.Header(&header),
|
2021-05-26 00:48:04 +08:00
|
|
|
)
|
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("encountered error deleting node: "+err.Error(), 1)
|
2021-05-26 00:48:04 +08:00
|
|
|
} else {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("removed machine from "+node.Network+" network on remote server", 1)
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-06 03:02:09 +08:00
|
|
|
} else { // handle server side
|
|
|
|
node.SetID()
|
|
|
|
if err = logic.DeleteNode(node.ID, true); err != nil {
|
|
|
|
ncutils.PrintLog("error removing server on network "+node.Network, 1)
|
|
|
|
} else {
|
|
|
|
ncutils.PrintLog("removed netmaker server instance on "+node.Network, 1)
|
|
|
|
}
|
2021-03-28 00:04:44 +08:00
|
|
|
}
|
2021-08-10 22:55:48 +08:00
|
|
|
return RemoveLocalInstance(cfg, network)
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// RemoveLocalInstance - remove all netclient files locally for a network
|
2021-08-10 22:55:48 +08:00
|
|
|
func RemoveLocalInstance(cfg *config.ClientConfig, networkName string) error {
|
2021-09-20 02:03:47 +08:00
|
|
|
err := WipeLocal(networkName)
|
2021-03-26 00:17:52 +08:00
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("unable to wipe local config", 1)
|
2021-06-02 23:00:10 +08:00
|
|
|
} else {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("removed "+networkName+" network locally", 1)
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
2021-06-02 00:23:05 +08:00
|
|
|
if cfg.Daemon != "off" {
|
2021-09-20 02:03:47 +08:00
|
|
|
if ncutils.IsWindows() {
|
2021-08-31 03:58:23 +08:00
|
|
|
// TODO: Remove job?
|
2021-09-20 02:03:47 +08:00
|
|
|
} else if ncutils.IsMac() {
|
|
|
|
//TODO: Delete mac daemon
|
2021-08-31 03:58:23 +08:00
|
|
|
} else {
|
2021-09-20 02:03:47 +08:00
|
|
|
err = daemon.RemoveSystemDServices(networkName)
|
2021-08-31 03:58:23 +08:00
|
|
|
}
|
2021-06-02 00:23:05 +08:00
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// DeleteInterface - delete an interface of a network
|
2021-08-03 06:06:26 +08:00
|
|
|
func DeleteInterface(ifacename string, postdown string) error {
|
2021-08-31 03:58:23 +08:00
|
|
|
var err error
|
2021-09-20 02:03:47 +08:00
|
|
|
if !ncutils.IsKernel() {
|
|
|
|
err = wireguard.RemoveConf(ifacename, true)
|
2021-08-31 03:58:23 +08:00
|
|
|
} else {
|
2021-09-18 01:28:32 +08:00
|
|
|
ipExec, errN := exec.LookPath("ip")
|
|
|
|
err = errN
|
2021-08-31 03:58:23 +08:00
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog(err.Error(), 1)
|
2021-08-31 03:58:23 +08:00
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
_, err = ncutils.RunCmd(ipExec+" link del "+ifacename, false)
|
2021-08-31 03:58:23 +08:00
|
|
|
if postdown != "" {
|
|
|
|
runcmds := strings.Split(postdown, "; ")
|
2021-09-20 02:03:47 +08:00
|
|
|
err = ncutils.RunCmds(runcmds, true)
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2021-03-26 10:29:36 +08:00
|
|
|
}
|
2021-05-30 01:22:18 +08:00
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// List - lists all networks on local machine
|
2021-08-03 06:06:26 +08:00
|
|
|
func List() error {
|
2021-05-30 01:22:18 +08:00
|
|
|
|
2021-10-06 03:02:09 +08:00
|
|
|
networks, err := ncutils.GetSystemNetworks()
|
2021-05-30 01:22:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, network := range networks {
|
|
|
|
cfg, err := config.ReadConfig(network)
|
|
|
|
if err == nil {
|
2021-08-03 06:06:26 +08:00
|
|
|
jsoncfg, _ := json.Marshal(
|
|
|
|
map[string]string{
|
|
|
|
"Name": cfg.Node.Name,
|
|
|
|
"Interface": cfg.Node.Interface,
|
|
|
|
"PrivateIPv4": cfg.Node.Address,
|
|
|
|
"PrivateIPv6": cfg.Node.Address6,
|
|
|
|
"PublicEndpoint": cfg.Node.Endpoint,
|
|
|
|
})
|
2021-09-20 02:03:47 +08:00
|
|
|
fmt.Println(network + ": " + string(jsoncfg))
|
2021-05-30 01:22:18 +08:00
|
|
|
} else {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog(network+": Could not retrieve network configuration.", 1)
|
2021-05-30 01:22:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// WipeLocal - wipes local instance
|
2021-09-20 02:03:47 +08:00
|
|
|
func WipeLocal(network string) error {
|
|
|
|
cfg, err := config.ReadConfig(network)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nodecfg := cfg.Node
|
|
|
|
ifacename := nodecfg.Interface
|
|
|
|
if ifacename != "" {
|
|
|
|
if !ncutils.IsKernel() {
|
|
|
|
if err = wireguard.RemoveConf(ifacename, true); err == nil {
|
|
|
|
ncutils.PrintLog("removed WireGuard interface: "+ifacename, 1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ipExec, err := exec.LookPath("ip")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out, err := ncutils.RunCmd(ipExec+" link del "+ifacename, false)
|
|
|
|
dontprint := strings.Contains(out, "does not exist") || strings.Contains(out, "Cannot find device")
|
|
|
|
if err != nil && !dontprint {
|
|
|
|
ncutils.PrintLog("error running command: "+ipExec+" link del "+ifacename, 1)
|
|
|
|
ncutils.PrintLog(out, 1)
|
|
|
|
}
|
|
|
|
if nodecfg.PostDown != "" {
|
|
|
|
runcmds := strings.Split(nodecfg.PostDown, "; ")
|
|
|
|
_ = ncutils.RunCmds(runcmds, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
home := ncutils.GetNetclientPathSpecific()
|
|
|
|
if ncutils.FileExists(home + "netconfig-" + network) {
|
|
|
|
_ = os.Remove(home + "netconfig-" + network)
|
|
|
|
}
|
|
|
|
if ncutils.FileExists(home + "nettoken-" + network) {
|
|
|
|
_ = os.Remove(home + "nettoken-" + network)
|
|
|
|
}
|
|
|
|
if ncutils.FileExists(home + "secret-" + network) {
|
|
|
|
_ = os.Remove(home + "secret-" + network)
|
|
|
|
}
|
|
|
|
if ncutils.FileExists(home + "wgkey-" + network) {
|
|
|
|
_ = os.Remove(home + "wgkey-" + network)
|
|
|
|
}
|
|
|
|
if ncutils.FileExists(home + "nm-" + network + ".conf") {
|
|
|
|
_ = os.Remove(home + "nm-" + network + ".conf")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLocalIP(node models.Node) string {
|
|
|
|
|
|
|
|
var local string
|
|
|
|
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return local
|
|
|
|
}
|
|
|
|
_, localrange, err := net.ParseCIDR(node.LocalRange)
|
|
|
|
if err != nil {
|
|
|
|
return local
|
|
|
|
}
|
|
|
|
|
|
|
|
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 local
|
|
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
var ip net.IP
|
|
|
|
switch v := addr.(type) {
|
|
|
|
case *net.IPNet:
|
|
|
|
if !found {
|
|
|
|
ip = v.IP
|
|
|
|
local = ip.String()
|
|
|
|
if node.IsLocal == "yes" {
|
|
|
|
found = localrange.Contains(ip)
|
|
|
|
} else {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *net.IPAddr:
|
|
|
|
if !found {
|
|
|
|
ip = v.IP
|
|
|
|
local = ip.String()
|
|
|
|
if node.IsLocal == "yes" {
|
|
|
|
found = localrange.Contains(ip)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return local
|
|
|
|
}
|