netmaker/netclient/ncutils/netclientutils_freebsd.go

75 lines
1.6 KiB
Go
Raw Normal View History

2021-11-17 00:03:21 +08:00
package ncutils
import (
"context"
2021-11-18 13:01:05 +08:00
"fmt"
2021-11-17 00:03:21 +08:00
"log"
"os/exec"
2021-11-18 13:01:05 +08:00
"strconv"
2021-11-17 00:03:21 +08:00
"strings"
"syscall"
"time"
2022-01-04 23:00:59 +08:00
"github.com/gravitl/netmaker/models"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
2021-11-17 00:03:21 +08:00
)
// RunCmdFormatted - run a command formatted for freebsd
2021-11-18 13:01:05 +08:00
func RunCmdFormatted(command string, printerr bool) (string, error) {
return "", nil
}
// GetEmbedded - if files required for freebsd, put here
func GetEmbedded() error {
return nil
}
2021-11-17 00:03:21 +08:00
// Runs Commands for FreeBSD
func RunCmd(command string, printerr bool) (string, error) {
args := strings.Fields(command)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
cmd := exec.Command(args[0], args[1:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
go func() {
<-ctx.Done()
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}()
out, err := cmd.CombinedOutput()
if err != nil && printerr {
log.Println("error running command:", command)
log.Println(strings.TrimSuffix(string(out), "\n"))
}
return string(out), err
}
2021-11-18 13:01:05 +08:00
2022-01-04 23:00:59 +08:00
// CreateWireGuardConf - creates a WireGuard conf string
2022-01-07 04:56:11 +08:00
func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, peers []wgtypes.PeerConfig) (string, error) {
2022-01-04 23:00:59 +08:00
peersString, err := parsePeers(node.PersistentKeepalive, peers)
2021-11-18 13:01:05 +08:00
var listenPortString string
2022-01-04 23:00:59 +08:00
if node.MTU <= 0 {
node.MTU = 1280
2021-11-18 13:01:05 +08:00
}
if listenPort != "" {
listenPortString += "ListenPort = " + listenPort
}
if err != nil {
return "", err
}
config := fmt.Sprintf(`[Interface]
Address = %s
PrivateKey = %s
MTU = %s
%s
%s
`,
2022-01-04 23:00:59 +08:00
node.Address+"/32",
2021-11-18 13:01:05 +08:00
privatekey,
2022-01-04 23:00:59 +08:00
strconv.Itoa(int(node.MTU)),
2021-11-18 13:01:05 +08:00
listenPortString,
peersString)
return config, nil
}