Browse Source

changed exec stuff and maybe fixed listenport

worker-9 4 years ago
parent
commit
3a77f4df14

+ 0 - 5
database/sqlite.go

@@ -3,7 +3,6 @@ package database
 import (
 import (
 	"database/sql"
 	"database/sql"
 	"errors"
 	"errors"
-	"log"
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
 
 
@@ -29,12 +28,10 @@ var SQLITE_FUNCTIONS = map[string]interface{}{
 func initSqliteDB() error {
 func initSqliteDB() error {
 	// == create db file if not present ==
 	// == create db file if not present ==
 	if _, err := os.Stat("data"); os.IsNotExist(err) {
 	if _, err := os.Stat("data"); os.IsNotExist(err) {
-		log.Println("Could not find data directory, creating it.")
 		os.Mkdir("data", 0644)
 		os.Mkdir("data", 0644)
 	}
 	}
 	dbFilePath := filepath.Join("data", dbFilename)
 	dbFilePath := filepath.Join("data", dbFilename)
 	if _, err := os.Stat(dbFilePath); os.IsNotExist(err) {
 	if _, err := os.Stat(dbFilePath); os.IsNotExist(err) {
-		log.Println("Could not get database file, creating it.")
 		os.Create(dbFilePath)
 		os.Create(dbFilePath)
 	}
 	}
 	// == "connect" the database ==
 	// == "connect" the database ==
@@ -55,7 +52,6 @@ func sqliteCreateTable(tableName string) error {
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	log.Println(tableName, "table created")
 	return nil
 	return nil
 }
 }
 
 
@@ -70,7 +66,6 @@ func sqliteInsert(key string, value string, tableName string) error {
 		if err != nil {
 		if err != nil {
 			return err
 			return err
 		}
 		}
-		log.Println("inserted", key, ":", value, "into ", tableName)
 		return nil
 		return nil
 	} else {
 	} else {
 		return errors.New("invalid insert " + key + " : " + value)
 		return errors.New("invalid insert " + key + " : " + value)

+ 4 - 9
netclient/functions/common.go

@@ -10,7 +10,6 @@ import (
 	"log"
 	"log"
 	"net"
 	"net"
 	"net/http"
 	"net/http"
-	"os"
 	"os/exec"
 	"os/exec"
 	"strings"
 	"strings"
 
 
@@ -314,17 +313,13 @@ func RemoveLocalInstance(cfg *config.ClientConfig, networkName string) error {
 
 
 func DeleteInterface(ifacename string, postdown string) error {
 func DeleteInterface(ifacename string, postdown string) error {
 	ipExec, err := exec.LookPath("ip")
 	ipExec, err := exec.LookPath("ip")
-
-	cmdIPLinkDel := &exec.Cmd{
-		Path:   ipExec,
-		Args:   []string{ipExec, "link", "del", ifacename},
-		Stdout: os.Stdout,
-		Stderr: os.Stdout,
-	}
-	err = cmdIPLinkDel.Run()
 	if err != nil {
 	if err != nil {
 		log.Println(err)
 		log.Println(err)
 	}
 	}
+	out, err := local.RunCmd(ipExec + " link del " + ifacename)
+	if err != nil {
+		log.Println(out, err)
+	}
 	if postdown != "" {
 	if postdown != "" {
 		runcmds := strings.Split(postdown, "; ")
 		runcmds := strings.Split(postdown, "; ")
 		err = local.RunCmds(runcmds)
 		err = local.RunCmds(runcmds)

+ 9 - 6
netclient/functions/join.go

@@ -136,12 +136,7 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string) error {
 			cfg.Node.MacAddress = macs[0]
 			cfg.Node.MacAddress = macs[0]
 		}
 		}
 	}
 	}
-	if cfg.Node.ListenPort == 0 {
-		cfg.Node.ListenPort, err = GetFreePort(51821)
-		if err != nil {
-			fmt.Printf("Error retrieving port: %v", err)
-		}
-	}
+
 	var wcclient nodepb.NodeServiceClient
 	var wcclient nodepb.NodeServiceClient
 	var requestOpts grpc.DialOption
 	var requestOpts grpc.DialOption
 	requestOpts = grpc.WithInsecure()
 	requestOpts = grpc.WithInsecure()
@@ -174,6 +169,7 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string) error {
 		SaveConfig:          cfg.Node.SaveConfig,
 		SaveConfig:          cfg.Node.SaveConfig,
 		UDPHolePunch:        cfg.Node.UDPHolePunch,
 		UDPHolePunch:        cfg.Node.UDPHolePunch,
 	}
 	}
+
 	if err = config.ModConfig(postnode); err != nil {
 	if err = config.ModConfig(postnode); err != nil {
 		return err
 		return err
 	}
 	}
@@ -200,6 +196,13 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string) error {
 		return err
 		return err
 	}
 	}
 
 
+	if node.ListenPort == 0 {
+		node.ListenPort, err = GetFreePort(51821)
+		if err != nil {
+			fmt.Printf("Error retrieving port: %v", err)
+		}
+	}
+
 	if node.DNSOn == "yes" {
 	if node.DNSOn == "yes" {
 		cfg.Node.DNSOn = "yes"
 		cfg.Node.DNSOn = "yes"
 	}
 	}

+ 5 - 9
netclient/local/local.go

@@ -279,17 +279,13 @@ func WipeLocal(network string) error {
 	}
 	}
 
 
 	ipExec, err := exec.LookPath("ip")
 	ipExec, err := exec.LookPath("ip")
-
+	if err != nil {
+		return err
+	}
 	if ifacename != "" {
 	if ifacename != "" {
-		cmdIPLinkDel := &exec.Cmd{
-			Path:   ipExec,
-			Args:   []string{ipExec, "link", "del", ifacename},
-			Stdout: os.Stdout,
-			Stderr: os.Stdout,
-		}
-		err = cmdIPLinkDel.Run()
+		out, err := RunCmd(ipExec + " link del " + ifacename)
 		if err != nil {
 		if err != nil {
-			log.Println(err)
+			log.Println(out, err)
 		}
 		}
 		if nodecfg.PostDown != "" {
 		if nodecfg.PostDown != "" {
 			runcmds := strings.Split(nodecfg.PostDown, "; ")
 			runcmds := strings.Split(nodecfg.PostDown, "; ")

+ 5 - 15
netclient/wireguard/kernel.go

@@ -123,21 +123,11 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 		_ = local.UpdateDNS(ifacename, network, nameserver)
 		_ = local.UpdateDNS(ifacename, network, nameserver)
 	}
 	}
 	//=========End DNS Setup=======\\
 	//=========End DNS Setup=======\\
-
-	cmdIPLinkUp := &exec.Cmd{
-		Path:   ipExec,
-		Args:   []string{ipExec, "link", "set", "up", "dev", ifacename},
-		Stdout: os.Stdout,
-		Stderr: os.Stdout,
+	if ipLinkDownOut, err := local.RunCmd(ipExec + " link set down dev " + ifacename); err != nil {
+		log.Println(ipLinkDownOut, err)
+		return err
 	}
 	}
 
 
-	cmdIPLinkDown := &exec.Cmd{
-		Path:   ipExec,
-		Args:   []string{ipExec, "link", "set", "down", "dev", ifacename},
-		Stdout: os.Stdout,
-		Stderr: os.Stdout,
-	}
-	err = cmdIPLinkDown.Run()
 	if nodecfg.PostDown != "" {
 	if nodecfg.PostDown != "" {
 		runcmds := strings.Split(nodecfg.PostDown, "; ")
 		runcmds := strings.Split(nodecfg.PostDown, "; ")
 		err = local.RunCmds(runcmds)
 		err = local.RunCmds(runcmds)
@@ -146,8 +136,8 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 		}
 		}
 	}
 	}
 
 
-	err = cmdIPLinkUp.Run()
-	if err != nil {
+	if ipLinkUpOut, err := local.RunCmd(ipExec + " link set up dev " + ifacename); err != nil {
+		log.Println(ipLinkUpOut, err)
 		return err
 		return err
 	}
 	}