netclientutils_freebsd.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package ncutils
  2. import (
  3. "context"
  4. "fmt"
  5. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  6. "log"
  7. "os/exec"
  8. "strconv"
  9. "strings"
  10. "syscall"
  11. "time"
  12. )
  13. func RunCmdFormatted(command string, printerr bool) (string, error) {
  14. return "", nil
  15. }
  16. // Runs Commands for FreeBSD
  17. func RunCmd(command string, printerr bool) (string, error) {
  18. args := strings.Fields(command)
  19. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  20. defer cancel()
  21. cmd := exec.Command(args[0], args[1:]...)
  22. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  23. go func() {
  24. <-ctx.Done()
  25. _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
  26. }()
  27. out, err := cmd.CombinedOutput()
  28. if err != nil && printerr {
  29. log.Println("error running command:", command)
  30. log.Println(strings.TrimSuffix(string(out), "\n"))
  31. }
  32. return string(out), err
  33. }
  34. // CreateUserSpaceConf - creates a user space WireGuard conf
  35. func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, fwmark int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
  36. peersString, err := parsePeers(perskeepalive, peers)
  37. var listenPortString string
  38. var fwmarkString string
  39. if mtu <= 0 {
  40. mtu = 1280
  41. }
  42. if listenPort != "" {
  43. listenPortString += "ListenPort = " + listenPort
  44. }
  45. if fwmark != 0 {
  46. fwmarkString += "FWMark = " + strconv.Itoa(int(fwmark))
  47. }
  48. if err != nil {
  49. return "", err
  50. }
  51. config := fmt.Sprintf(`[Interface]
  52. Address = %s
  53. PrivateKey = %s
  54. MTU = %s
  55. %s
  56. %s
  57. %s
  58. `,
  59. address+"/32",
  60. privatekey,
  61. strconv.Itoa(int(mtu)),
  62. listenPortString,
  63. fwmarkString,
  64. peersString)
  65. return config, nil
  66. }