2021-03-26 00:17:52 +08:00
|
|
|
package functions
|
|
|
|
|
|
|
|
import (
|
2022-04-20 18:21:33 +08:00
|
|
|
"bytes"
|
2022-01-11 08:36:13 +08:00
|
|
|
"encoding/json"
|
2021-03-26 22:35:52 +08:00
|
|
|
"errors"
|
2021-08-03 06:06:26 +08:00
|
|
|
"fmt"
|
2022-04-20 19:52:31 +08:00
|
|
|
"io/ioutil"
|
2021-03-26 00:17:52 +08:00
|
|
|
"log"
|
|
|
|
"net"
|
2022-04-20 18:21:33 +08:00
|
|
|
"net/http"
|
2021-09-20 02:03:47 +08:00
|
|
|
"os"
|
2022-01-12 23:03:23 +08:00
|
|
|
"strings"
|
2021-08-03 06:06:26 +08:00
|
|
|
|
2022-03-20 23:12:05 +08:00
|
|
|
"github.com/gravitl/netmaker/logger"
|
2021-08-03 06:06:26 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
|
"github.com/gravitl/netmaker/netclient/config"
|
2021-09-20 02:03:47 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/daemon"
|
2022-02-06 04:00:26 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/local"
|
2021-09-20 02:03:47 +08:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2022-04-19 05:19:26 +08:00
|
|
|
// LINUX_APP_DATA_PATH - linux path
|
|
|
|
const LINUX_APP_DATA_PATH = "/etc/netmaker"
|
|
|
|
|
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
|
|
|
|
}
|
2021-12-11 04:01:10 +08:00
|
|
|
defer wgclient.Close()
|
2021-04-07 09:31:47 +08:00
|
|
|
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")
|
2022-02-02 13:02:36 +08:00
|
|
|
if err == nil {
|
|
|
|
defer conn.Close()
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2022-02-02 13:02:36 +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()
|
|
|
|
}
|
2022-02-02 13:02:36 +08:00
|
|
|
|
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
|
|
|
}
|
2022-02-02 13:02:36 +08:00
|
|
|
|
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 {
|
2022-01-02 23:30:07 +08:00
|
|
|
err := errors.New("local ip address not found")
|
2021-09-23 00:03:33 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return local, err
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "unable to retrieve networks: ", err.Error())
|
|
|
|
logger.Log(1, "continuing uninstall without leaving networks")
|
2021-05-30 01:22:18 +08:00
|
|
|
} else {
|
|
|
|
for _, network := range networks {
|
2022-02-21 07:09:32 +08:00
|
|
|
err = LeaveNetwork(network, true)
|
2021-05-30 01:22:18 +08:00
|
|
|
if err != nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "Encounter issue leaving network ", network, ": ", err.Error())
|
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-10-20 03:53:01 +08:00
|
|
|
} else if ncutils.IsLinux() {
|
|
|
|
daemon.CleanupLinux()
|
2022-02-09 11:31:17 +08:00
|
|
|
} else if ncutils.IsFreeBSD() {
|
2022-02-09 11:06:41 +08:00
|
|
|
daemon.CleanupFreebsd()
|
2021-09-22 09:35:52 +08:00
|
|
|
} else if !ncutils.IsKernel() {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "manual cleanup required")
|
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
|
2022-02-21 07:09:32 +08:00
|
|
|
func LeaveNetwork(network string, force bool) error {
|
2021-08-03 06:06:26 +08:00
|
|
|
cfg, err := config.ReadConfig(network)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node := cfg.Node
|
2021-10-06 03:02:09 +08:00
|
|
|
if node.IsServer != "yes" {
|
2022-04-20 19:52:31 +08:00
|
|
|
token, err := Authenticate(cfg)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err != nil {
|
2022-04-20 19:52:31 +08:00
|
|
|
return fmt.Errorf("unable to authenticate %w", err)
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
2022-04-20 19:52:31 +08:00
|
|
|
url := "https://" + cfg.Server.API + "/api/nodes/" + cfg.Network + "/" + cfg.Node.ID
|
|
|
|
response, err := API("", http.MethodDelete, url, token)
|
2021-05-26 00:48:04 +08:00
|
|
|
if err != nil {
|
2022-04-20 19:52:31 +08:00
|
|
|
return fmt.Errorf("error deleting node on server %w", err)
|
|
|
|
}
|
|
|
|
if response.StatusCode == http.StatusOK {
|
|
|
|
logger.Log(0, "deleted node", cfg.Node.Name, " on network ", cfg.Network)
|
|
|
|
} else {
|
|
|
|
bodybytes, _ := ioutil.ReadAll(response.Body)
|
|
|
|
defer response.Body.Close()
|
|
|
|
return fmt.Errorf("error deleting node on server %s %s", response.Status, string(bodybytes))
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
2021-03-28 00:04:44 +08:00
|
|
|
}
|
2022-02-06 04:00:26 +08:00
|
|
|
wgClient, wgErr := wgctrl.New()
|
|
|
|
if wgErr == nil {
|
2022-02-08 00:30:15 +08:00
|
|
|
removeIface := cfg.Node.Interface
|
2022-04-20 04:18:03 +08:00
|
|
|
queryAddr := cfg.Node.PrimaryAddress()
|
2022-02-08 00:30:15 +08:00
|
|
|
if ncutils.IsMac() {
|
|
|
|
var macIface string
|
2022-04-20 04:18:03 +08:00
|
|
|
macIface, wgErr = local.GetMacIface(queryAddr)
|
2022-02-08 00:30:15 +08:00
|
|
|
if wgErr == nil && removeIface != "" {
|
|
|
|
removeIface = macIface
|
|
|
|
}
|
|
|
|
wgErr = nil
|
|
|
|
}
|
|
|
|
dev, devErr := wgClient.Device(removeIface)
|
2022-02-06 04:00:26 +08:00
|
|
|
if devErr == nil {
|
2022-04-20 04:18:03 +08:00
|
|
|
local.FlushPeerRoutes(removeIface, queryAddr, dev.Peers[:])
|
2022-02-06 04:00:26 +08:00
|
|
|
_, cidr, cidrErr := net.ParseCIDR(cfg.NetworkSettings.AddressRange)
|
|
|
|
if cidrErr == nil {
|
2022-04-20 04:18:03 +08:00
|
|
|
local.RemoveCIDRRoute(removeIface, queryAddr, cidr)
|
2022-02-06 04:00:26 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "could not flush peer routes when leaving network, ", cfg.Node.Network)
|
2021-11-15 08:17:30 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-02 23:37:05 +08:00
|
|
|
|
2022-02-05 00:31:22 +08:00
|
|
|
err = WipeLocal(node.Network)
|
|
|
|
if err != nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "unable to wipe local config")
|
2022-02-05 00:31:22 +08:00
|
|
|
} else {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "removed ", node.Network, " network locally")
|
2022-02-05 00:31:22 +08:00
|
|
|
}
|
|
|
|
|
2022-02-02 23:37:05 +08:00
|
|
|
currentNets, err := ncutils.GetSystemNetworks()
|
|
|
|
if err != nil || len(currentNets) <= 1 {
|
2022-02-08 23:43:46 +08:00
|
|
|
daemon.Stop() // stop system daemon if last network
|
2022-02-02 23:37:05 +08:00
|
|
|
return RemoveLocalInstance(cfg, network)
|
|
|
|
}
|
|
|
|
return daemon.Restart()
|
2021-08-10 22:55:48 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-02-05 00:31:22 +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
|
2022-02-09 11:31:17 +08:00
|
|
|
} else if ncutils.IsFreeBSD() {
|
2022-02-09 11:06:41 +08:00
|
|
|
daemon.RemoveFreebsdDaemon()
|
2021-08-31 03:58:23 +08:00
|
|
|
} else {
|
2022-02-05 00:31:22 +08:00
|
|
|
daemon.RemoveSystemDServices()
|
2021-08-31 03:58:23 +08:00
|
|
|
}
|
2021-06-02 00:23:05 +08:00
|
|
|
}
|
2022-02-05 00:31:22 +08:00
|
|
|
return nil
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
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-12-30 20:11:01 +08:00
|
|
|
return wireguard.RemoveConf(ifacename, true)
|
2021-03-26 10:29:36 +08:00
|
|
|
}
|
2021-05-30 01:22:18 +08:00
|
|
|
|
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 != "" {
|
2021-12-30 20:11:01 +08:00
|
|
|
if err = wireguard.RemoveConf(ifacename, true); err == nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "removed WireGuard interface: ", ifacename)
|
2022-01-12 23:03:23 +08:00
|
|
|
} else if strings.Contains(err.Error(), "does not exist") {
|
|
|
|
err = nil
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-30 20:11:01 +08:00
|
|
|
|
2021-09-20 02:03:47 +08:00
|
|
|
home := ncutils.GetNetclientPathSpecific()
|
|
|
|
if ncutils.FileExists(home + "netconfig-" + network) {
|
2022-02-08 04:37:59 +08:00
|
|
|
err = os.Remove(home + "netconfig-" + network)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error removing netconfig:")
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
2021-11-09 01:15:20 +08:00
|
|
|
if ncutils.FileExists(home + "backup.netconfig-" + network) {
|
2022-02-08 04:37:59 +08:00
|
|
|
err = os.Remove(home + "backup.netconfig-" + network)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error removing backup netconfig:")
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2021-11-09 01:15:20 +08:00
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
if ncutils.FileExists(home + "nettoken-" + network) {
|
2022-02-08 04:37:59 +08:00
|
|
|
err = os.Remove(home + "nettoken-" + network)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error removing nettoken:")
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
|
|
|
if ncutils.FileExists(home + "secret-" + network) {
|
2022-02-08 04:37:59 +08:00
|
|
|
err = os.Remove(home + "secret-" + network)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error removing secret:")
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
2022-01-29 07:27:21 +08:00
|
|
|
if ncutils.FileExists(home + "traffic-" + network) {
|
2022-02-08 04:37:59 +08:00
|
|
|
err = os.Remove(home + "traffic-" + network)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error removing traffic key:")
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2022-01-29 07:27:21 +08:00
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
if ncutils.FileExists(home + "wgkey-" + network) {
|
2022-02-08 04:37:59 +08:00
|
|
|
err = os.Remove(home + "wgkey-" + network)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error removing wgkey:")
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
2022-02-08 11:56:03 +08:00
|
|
|
if ncutils.FileExists(home + ifacename + ".conf") {
|
|
|
|
err = os.Remove(home + ifacename + ".conf")
|
2022-02-08 04:37:59 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error removing .conf:")
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 05:19:26 +08:00
|
|
|
|
|
|
|
// GetNetmakerPath - gets netmaker path locally
|
|
|
|
func GetNetmakerPath() string {
|
|
|
|
return LINUX_APP_DATA_PATH
|
|
|
|
}
|
2022-04-20 18:21:33 +08:00
|
|
|
|
|
|
|
//API function to interact with netmaker api endpoints. response from endpoint is returned
|
|
|
|
func API(data any, method, url, authorization string) (*http.Response, error) {
|
|
|
|
var request *http.Request
|
|
|
|
var err error
|
|
|
|
if data != "" {
|
|
|
|
payload, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error encoding data %w", err)
|
|
|
|
}
|
|
|
|
request, err = http.NewRequest(method, url, bytes.NewBuffer(payload))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating http request %w", err)
|
|
|
|
}
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
} else {
|
|
|
|
request, err = http.NewRequest(method, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating http request %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if authorization != "" {
|
|
|
|
request.Header.Set("authorization", "Bearer "+authorization)
|
|
|
|
}
|
|
|
|
client := http.Client{}
|
|
|
|
return client.Do(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authenticate authenticates with api to permit subsequent interactions with the api
|
|
|
|
func Authenticate(cfg *config.ClientConfig) (string, error) {
|
|
|
|
|
|
|
|
pass, err := os.ReadFile(ncutils.GetNetclientPathSpecific() + "/secret-" + cfg.Network)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not read secrets file %w", err)
|
|
|
|
}
|
|
|
|
data := models.AuthParams{
|
|
|
|
MacAddress: cfg.Node.MacAddress,
|
|
|
|
ID: cfg.Node.ID,
|
|
|
|
Password: string(pass),
|
|
|
|
}
|
|
|
|
url := "https://" + cfg.Server.API + "/api/nodes/adm/" + cfg.Network + "/authenticate"
|
|
|
|
response, err := API(data, http.MethodPost, url, "")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-04-21 03:44:33 +08:00
|
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
bodybytes, _ := ioutil.ReadAll(response.Body)
|
|
|
|
return "", fmt.Errorf("failed to authenticate %s %s", response.Status, string(bodybytes))
|
|
|
|
}
|
2022-04-20 18:21:33 +08:00
|
|
|
resp := models.SuccessResponse{}
|
|
|
|
if err := json.NewDecoder(response.Body).Decode(&resp); err != nil {
|
2022-04-21 03:44:33 +08:00
|
|
|
return "", fmt.Errorf("error decoding respone %w", err)
|
2022-04-20 18:21:33 +08:00
|
|
|
}
|
2022-04-21 03:44:33 +08:00
|
|
|
tokenData := resp.Response.(map[string]interface{})
|
2022-04-20 18:21:33 +08:00
|
|
|
token := tokenData["AuthToken"]
|
|
|
|
return token.(string), nil
|
|
|
|
}
|