Merge remote-tracking branch 'origin/master' into add-tests

This commit is contained in:
Matthew R Kasun 2021-03-28 05:31:36 -04:00
commit 0a1722e3b1
4 changed files with 103 additions and 45 deletions

View file

@ -203,7 +203,6 @@ func readConfig() *ClientConfig {
f, err := os.Open(file)
if err != nil {
nofile = true
fmt.Println("Could not access " + home + "/.netconfig, proceeding...")
}
defer f.Close()

View file

@ -98,18 +98,10 @@ func Install(accesskey string, password string, server string, group string, noa
var wginterface string
if nodecfg.Endpoint == "" {
resp, err := http.Get("https://ifconfig.me")
endpoint, err = getPublicIP()
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
endpoint = string(bodyBytes)
}
} else {
endpoint = nodecfg.Endpoint
}
@ -315,6 +307,34 @@ func Install(accesskey string, password string, server string, group string, noa
return err
}
func getPublicIP() (string, error) {
iplist := []string{"https://ifconfig.me", "http://api.ipify.org", "http://ipinfo.io/ip"}
endpoint := ""
var err error
for _, ipserver := range iplist {
resp, err := http.Get(ipserver)
if err != nil {
continue
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
continue
}
endpoint = string(bodyBytes)
break
}
}
if err == nil && endpoint == "" {
err = errors.New("Public Address Not Found.")
}
return endpoint, err
}
func modConfig(node *nodepb.Node) error{
modconfig := config.Config
modconfig.ReadConfig()
@ -548,23 +568,6 @@ func retrievePrivKey() (string, error) {
return string(dat), err
}
func getPublicAddr() (string, error) {
resp, err := http.Get("https://ifconfig.me")
if err != nil {
return "", err
}
defer resp.Body.Close()
endpoint := ""
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
endpoint = string(bodyBytes)
}
return endpoint, err
}
func getPrivateAddr() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
@ -620,7 +623,7 @@ func CheckIn() error {
if !nodecfg.RoamingOff {
fmt.Println("Checking to see if addresses have changed")
extIP, err := getPublicAddr()
extIP, err := getPublicIP()
if err != nil {
fmt.Printf("Error encountered checking ip addresses: %v", err)
}
@ -866,17 +869,17 @@ func Remove() error {
conn, err := grpc.Dial(servercfg.Address, requestOpts)
if err != nil {
log.Printf("Unable to establish client connection to " + servercfg.Address + ": %v", err)
return err
}
//return err
}else {
wcclient = nodepb.NewNodeServiceClient(conn)
ctx := context.Background()
fmt.Println("Authenticating with GRPC Server")
ctx, err = SetJWT(wcclient)
if err != nil {
return err
log.Fatalf("Failed to authenticate: %v", err)
}
//return err
log.Printf("Failed to authenticate: %v", err)
} else {
fmt.Println("Authenticated")
var header metadata.MD
@ -890,23 +893,24 @@ func Remove() error {
grpc.Header(&header),
)
if err != nil {
fmt.Println("Encountered error deleting node.")
log.Printf("Encountered error deleting node: %v", err)
fmt.Println(err)
//return err
//log.Fatalf("Unable to process Delete request: %v", err)
}
fmt.Println("Deleted node " + node.MacAddress)
} else {
fmt.Println("Deleted node " + node.MacAddress)
}
}
}
err = WipeLocal()
if err != nil {
//return err
log.Printf("Unable to wipe local config: %v", err)
}
err = RemoveSystemDServices()
if err != nil {
return err
log.Fatalf("Unable to remove systemd services: %v", err)
log.Printf("Unable to remove systemd services: %v", err)
}
fmt.Printf("Please investigate any stated errors to ensure proper removal.")
fmt.Printf("Failure to delete node from server via gRPC will mean node still exists and needs to be manually deleted by administrator.")
return nil
}

View file

@ -4,6 +4,7 @@ import (
//"github.com/davecgh/go-spew/spew"
"fmt"
"io/ioutil"
"path/filepath"
"io"
"log"
"os"
@ -11,16 +12,23 @@ import (
)
func ConfigureSystemD() error {
/*
path, err := os.Getwd()
if err != nil {
log.Println(err)
return err
}
*/
//binarypath := path + "/netclient"
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return err
}
binarypath := dir + "/netclient"
binarypath := path + "/netclient"
fmt.Println("Installing Binary from Path: " + binarypath)
_, err = os.Stat("/etc/netclient")
_, err = os.Stat("/etc/netclient")
if os.IsNotExist(err) {
os.Mkdir("/etc/netclient", 744)
} else if err != nil {

View file

@ -7,6 +7,9 @@ import (
nodepb "github.com/gravitl/netmaker/grpc"
"flag"
"os"
"os/exec"
"strconv"
"strings"
"log"
)
@ -34,19 +37,61 @@ func main() {
taccesskey := flag.String("k", "badkey", "an access key generated by the server and used for one-time access (install only)")
tserver := flag.String("s", "localhost:50051", "The location (including port) of the remote gRPC server.")
tgroup := flag.String("g", "badgroup", "The node group you are attempting to join.")
tnoauto := flag.Bool("na", false, "No auto mode. If true, netmaker will not be installed as a system service and you will have to retrieve updates manually via checkin command.")
tnoauto := flag.Bool("na", false, "No auto mode. If true, netmclient will not be installed as a system service and you will have to retrieve updates manually via checkin command.")
tnoforward := flag.Bool("nf", false, "No Forward mode. If true, netclient will not check for IP forwarding. This may break functionality")
command := flag.String("c", "required", "The command to run")
flag.Parse()
getID := exec.Command("id", "-u")
out, err := getID.Output()
if err != nil {
log.Fatal(err)
}
id, err := strconv.Atoi(string(out[:len(out)-1]))
if err != nil {
log.Fatal(err)
}
if id != 0 {
log.Fatal("This program must be run with elevated privileges (sudo). This program installs a SystemD service and configures WireGuard and networking rules. Please re-run with sudo/root.")
}
_, err = exec.LookPath("wg")
if err != nil {
log.Println(err)
log.Fatal("WireGuard not installed. Please install WireGuard (wireguard-tools) and try again.")
}
switch *command {
case "required":
fmt.Println("command flag 'c' is required. Pick one of |install|checkin|update|remove|")
os.Exit(1)
log.Fatal("Exiting")
case "install":
if !*tnoforward {
forward := exec.Command("sysctl", "net.ipv4.ip_forward")
out, err := forward.Output()
if err != nil {
log.Fatal(err)
}
//s := strings.Split(string(out), " ", "\n")
s := strings.Fields(string(out))
if err != nil {
log.Fatal(err)
}
if s[2] != "1" {
log.Fatal("It is recommended to enable IP Forwarding. Current status is: " + s[2] + ", but should be 1. if you would like to run without IP Forwarding, re-run with flag '-nf true'")
}
}
fmt.Println("Beginning agent installation.")
err := functions.Install(*taccesskey, *tpassword, *tserver, *tgroup, *tnoauto)
if err != nil {
@ -92,6 +137,7 @@ func main() {
fmt.Println("Beginning node cleanup.")
err := functions.Remove()
if err != nil {
/*
fmt.Println("Error uninstalling: ", err)
fmt.Println("Wiping local.")
err = functions.WipeLocal()
@ -102,6 +148,7 @@ func main() {
if err != nil {
fmt.Println("Error removing services: ", err)
}
*/
fmt.Println("Error deleting node: ", err)
os.Exit(1)
}