netmaker/netclient/ncutils/netclientutils_linux.go

77 lines
1.6 KiB
Go
Raw Normal View History

2021-11-17 09:10:11 +08:00
package ncutils
import (
2021-11-18 13:01:05 +08:00
"fmt"
2021-11-17 09:10:11 +08:00
"os/exec"
2021-11-18 13:01:05 +08:00
"strconv"
2021-11-17 09:10:11 +08:00
"strings"
2021-11-18 13:01:05 +08:00
2022-01-04 08:09:19 +08:00
"github.com/gravitl/netmaker/models"
2021-11-18 13:01:05 +08:00
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
2021-11-17 09:10:11 +08:00
)
// RunCmd - runs a local command
func RunCmd(command string, printerr bool) (string, error) {
args := strings.Fields(command)
cmd := exec.Command(args[0], args[1:]...)
cmd.Wait()
out, err := cmd.CombinedOutput()
if err != nil && printerr {
2022-01-02 23:30:07 +08:00
Log(fmt.Sprintf("error running command: %s", command))
Log(strings.TrimSuffix(string(out), "\n"))
2021-11-17 09:10:11 +08:00
}
return string(out), err
}
2021-11-18 10:57:27 +08:00
// RunCmdFormatted - does nothing for linux
2021-11-18 13:01:05 +08:00
func RunCmdFormatted(command string, printerr bool) (string, error) {
return "", nil
}
// GetEmbedded - if files required for linux, put here
func GetEmbedded() error {
return nil
}
2022-01-02 23:30:07 +08:00
// CreateWireGuardConf - creates a user space WireGuard conf
2022-01-21 04:41:06 +08:00
func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, peers []wgtypes.PeerConfig) (string, error) {
2022-01-04 08:09:19 +08:00
peersString, err := parsePeers(node.PersistentKeepalive, peers)
var listenPortString, postDownString, postUpString string
if node.MTU <= 0 {
node.MTU = 1280
2021-11-18 10:57:27 +08:00
}
2022-01-04 08:09:19 +08:00
if node.PostDown != "" {
postDownString = fmt.Sprintf("PostDown = %s", node.PostDown)
}
if node.PostUp != "" {
postUpString = fmt.Sprintf("PostUp = %s", node.PostUp)
}
2021-11-18 10:57:27 +08:00
if listenPort != "" {
2022-01-04 08:09:19 +08:00
listenPortString = fmt.Sprintf("ListenPort = %s", listenPort)
2021-11-18 10:57:27 +08:00
}
2022-01-04 08:09:19 +08:00
2021-11-18 10:57:27 +08:00
if err != nil {
return "", err
}
config := fmt.Sprintf(`[Interface]
Address = %s
PrivateKey = %s
MTU = %s
%s
2022-01-04 08:09:19 +08:00
%s
%s
2021-11-18 10:57:27 +08:00
%s
`,
2022-01-04 08:09:19 +08:00
node.Address+"/32",
2021-11-18 10:57:27 +08:00
privatekey,
2022-01-04 08:09:19 +08:00
strconv.Itoa(int(node.MTU)),
postDownString,
postUpString,
2021-11-18 10:57:27 +08:00
listenPortString,
peersString)
return config, nil
}