freebsd.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package wireguard
  2. import (
  3. "os"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/gravitl/netmaker/logger"
  9. "github.com/gravitl/netmaker/models"
  10. "github.com/gravitl/netmaker/netclient/config"
  11. "github.com/gravitl/netmaker/netclient/ncutils"
  12. )
  13. // ApplyWithoutWGQuick - Function for running the equivalent of "wg-quick up" for linux if wg-quick is missing
  14. func ApplyWithoutWGQuickFreeBSD(node *models.Node, ifacename string, confPath string) error {
  15. netmaskArr := strings.Split(node.NetworkSettings.AddressRange, "/")
  16. var netmask = "32"
  17. if len(netmaskArr) == 2 {
  18. netmask = netmaskArr[1]
  19. }
  20. setKernelDeviceFreeBSD(ifacename, node.Address, netmask)
  21. setConfFreeBSD(ifacename, confPath)
  22. addAddressFreeBSD(ifacename, node.Address6+"/64", node.Address+"/"+netmask)
  23. if _, err := ncutils.RunCmd("ifconfig "+ifacename+" mtu "+strconv.Itoa(int(node.MTU))+" up", true); err != nil {
  24. logger.Log(2, "failed to create interface with mtu", strconv.Itoa(int(node.MTU)), "-", ifacename)
  25. return err
  26. }
  27. if node.PostUp != "" {
  28. runcmds := strings.Split(node.PostUp, "; ")
  29. _ = ncutils.RunCmds(runcmds, true)
  30. }
  31. return nil
  32. }
  33. // RemoveWithoutWGQuickFreeBSD - Function for running the equivalent of "wg-quick down" for linux if wg-quick is missing
  34. func RemoveWithoutWGQuickFreeBSD(ifacename string) error {
  35. delInterface(ifacename)
  36. network := strings.ReplaceAll(ifacename, "nm-", "")
  37. nodeconf, err := config.ReadConfig(network)
  38. if nodeconf != nil && err == nil {
  39. if nodeconf.Node.PostDown != "" {
  40. runcmds := strings.Split(nodeconf.Node.PostDown, "; ")
  41. _ = ncutils.RunCmds(runcmds, false)
  42. }
  43. } else if err != nil {
  44. ncutils.PrintLog("error retrieving config: "+err.Error(), 1)
  45. }
  46. return err
  47. }
  48. func setKernelDeviceFreeBSD(ifacename, address, mask string) error {
  49. // == best effort ==
  50. delInterface(ifacename)
  51. addInterfaceFreeBSD(ifacename)
  52. return nil
  53. }
  54. func delInterface(ifacename string) {
  55. ncutils.RunCmd("rm -f /var/run/wireguard/"+ifacename+".sock", false)
  56. ncutils.RunCmd("ifconfig "+ifacename+" destroy", false)
  57. output, _ := ncutils.RunCmd("wg", false)
  58. starttime := time.Now()
  59. ifaceGone := !strings.Contains(output, ifacename)
  60. for !ifaceGone && !(time.Now().After(starttime.Add(time.Second << 4))) {
  61. output, _ = ncutils.RunCmd("wg", false)
  62. time.Sleep(time.Second)
  63. ifaceGone = !strings.Contains(output, ifacename)
  64. }
  65. }
  66. func addInterfaceFreeBSD(ifacename string) {
  67. ncutils.RunCmd("ifconfig wg create name "+ifacename, false)
  68. output, _ := ncutils.RunCmd("wg", false)
  69. starttime := time.Now()
  70. ifaceReady := strings.Contains(output, ifacename)
  71. for !ifaceReady && !(time.Now().After(starttime.Add(time.Second << 4))) {
  72. output, _ = ncutils.RunCmd("wg", false)
  73. time.Sleep(time.Second)
  74. ifaceReady = strings.Contains(output, ifacename)
  75. }
  76. }
  77. func addAddressFreeBSD(ifacename, inet6, inet string) {
  78. if inet6 != "" && inet6[0:1] != "/" {
  79. ncutils.RunCmd("ifconfig "+ifacename+" inet6 "+inet6+" alias", false)
  80. }
  81. if inet != "" && inet[0:1] != "/" {
  82. ncutils.RunCmd("ifconfig "+ifacename+" inet "+inet+" alias", false)
  83. }
  84. }
  85. func setConfFreeBSD(iface string, confPath string) error {
  86. var tmpConf = confPath + ".sync.tmp"
  87. //var confCmd = "wg-quick strip "
  88. confCmd := "grep -v -e Address -e MTU -e PostUp -e PostDown "
  89. confRaw, err := ncutils.RunCmd(confCmd+confPath, false)
  90. if err != nil {
  91. return err
  92. }
  93. regex := regexp.MustCompile(".*Warning.*\n")
  94. conf := regex.ReplaceAllString(confRaw, "")
  95. err = os.WriteFile(tmpConf, []byte(conf), 0600)
  96. if err != nil {
  97. return err
  98. }
  99. _, err = ncutils.RunCmd("wg setconf "+iface+" "+tmpConf, true)
  100. errN := os.Remove(tmpConf)
  101. if errN != nil {
  102. ncutils.Log(errN.Error())
  103. }
  104. return err
  105. }