netclientutils_linux.go 1.7 KB

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