netclientutils_darwin.go 1.4 KB

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