netclientutils_freebsd.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package ncutils
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "os/exec"
  7. "strconv"
  8. "strings"
  9. "syscall"
  10. "time"
  11. "github.com/gravitl/netmaker/models"
  12. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  13. )
  14. // RunCmdFormatted - run a command formatted for freebsd
  15. func RunCmdFormatted(command string, printerr bool) (string, error) {
  16. return "", nil
  17. }
  18. // GetEmbedded - if files required for freebsd, put here
  19. func GetEmbedded() error {
  20. return nil
  21. }
  22. // Runs Commands for FreeBSD
  23. func RunCmd(command string, printerr bool) (string, error) {
  24. args := strings.Fields(command)
  25. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  26. defer cancel()
  27. cmd := exec.Command(args[0], args[1:]...)
  28. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  29. go func() {
  30. <-ctx.Done()
  31. _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
  32. }()
  33. out, err := cmd.CombinedOutput()
  34. if err != nil && printerr {
  35. log.Println("error running command:", command)
  36. log.Println(strings.TrimSuffix(string(out), "\n"))
  37. }
  38. return string(out), err
  39. }
  40. // CreateWireGuardConf - creates a WireGuard conf string
  41. func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, peers []wgtypes.PeerConfig) (string, error) {
  42. peersString, err := parsePeers(node.PersistentKeepalive, peers)
  43. var listenPortString string
  44. if node.MTU <= 0 {
  45. node.MTU = 1280
  46. }
  47. if listenPort != "" {
  48. listenPortString += "ListenPort = " + listenPort
  49. }
  50. if err != nil {
  51. return "", err
  52. }
  53. config := fmt.Sprintf(`[Interface]
  54. Address = %s
  55. PrivateKey = %s
  56. MTU = %s
  57. %s
  58. %s
  59. `,
  60. node.Address+"/32",
  61. privatekey,
  62. strconv.Itoa(int(node.MTU)),
  63. listenPortString,
  64. peersString)
  65. return config, nil
  66. }