netclientutils_darwin.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package ncutils
  2. import (
  3. "fmt"
  4. "log"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  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.Println("error running command:", command)
  18. log.Println(strings.TrimSuffix(string(out), "\n"))
  19. }
  20. return string(out), err
  21. }
  22. // RunCmdFormatted - run a command formatted for MacOS
  23. func RunCmdFormatted(command string, printerr bool) (string, error) {
  24. return "", nil
  25. }
  26. // GetEmbedded - if files required for MacOS, put here
  27. func GetEmbedded() error {
  28. return nil
  29. }
  30. // CreateUserSpaceConf - creates a user space WireGuard conf
  31. func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
  32. peersString, err := parsePeers(perskeepalive, peers)
  33. var listenPortString string
  34. if mtu <= 0 {
  35. mtu = 1280
  36. }
  37. if listenPort != "" {
  38. listenPortString += "ListenPort = " + listenPort
  39. }
  40. if err != nil {
  41. return "", err
  42. }
  43. config := fmt.Sprintf(`[Interface]
  44. Address = %s
  45. PrivateKey = %s
  46. MTU = %s
  47. %s
  48. %s
  49. `,
  50. address+"/32",
  51. privatekey,
  52. strconv.Itoa(int(mtu)),
  53. listenPortString,
  54. peersString)
  55. return config, nil
  56. }