netclientutils_linux.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 - 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. // CreateUserSpaceConf - creates a user space WireGuard conf
  31. func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, dns string, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
  32. log.Println("---------- dns passed to create conf ", dns)
  33. peersString, err := parsePeers(perskeepalive, peers)
  34. var listenPortString string
  35. if mtu <= 0 {
  36. 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. DNS = %s
  47. PrivateKey = %s
  48. MTU = %s
  49. %s
  50. %s
  51. `,
  52. address+"/32",
  53. dns,
  54. privatekey,
  55. strconv.Itoa(int(mtu)),
  56. listenPortString,
  57. peersString)
  58. return config, nil
  59. }