netclientutils_linux.go 1.4 KB

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