noquick.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package wireguard
  2. import (
  3. "errors"
  4. "os"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  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. "golang.zx2c4.com/wireguard/wgctrl"
  13. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  14. )
  15. // ApplyWithoutWGQuick - Function for running the equivalent of "wg-quick up" for linux if wg-quick is missing
  16. func ApplyWithoutWGQuick(node *models.Node, ifacename string, confPath string) error {
  17. ipExec, err := exec.LookPath("ip")
  18. if err != nil {
  19. return err
  20. }
  21. wgclient, err := wgctrl.New()
  22. if err != nil {
  23. return err
  24. }
  25. defer wgclient.Close()
  26. privkey, err := RetrievePrivKey(node.Network)
  27. if err != nil {
  28. return err
  29. }
  30. key, err := wgtypes.ParseKey(privkey)
  31. if err != nil {
  32. return err
  33. }
  34. conf := wgtypes.Config{}
  35. nodeport := int(node.ListenPort)
  36. if node.UDPHolePunch == "yes" &&
  37. node.IsServer == "no" &&
  38. node.IsIngressGateway != "yes" &&
  39. node.IsStatic != "yes" {
  40. conf = wgtypes.Config{
  41. PrivateKey: &key,
  42. }
  43. } else {
  44. conf = wgtypes.Config{
  45. PrivateKey: &key,
  46. ListenPort: &nodeport,
  47. }
  48. }
  49. netmaskArr := strings.Split(node.NetworkSettings.AddressRange, "/")
  50. var netmask = "32"
  51. if len(netmaskArr) == 2 {
  52. netmask = netmaskArr[1]
  53. }
  54. setKernelDevice(ifacename, node.Address, netmask)
  55. _, err = wgclient.Device(ifacename)
  56. if err != nil {
  57. if !os.IsNotExist(err) {
  58. return errors.New("Unknown config error: " + err.Error())
  59. }
  60. }
  61. err = wgclient.ConfigureDevice(ifacename, conf)
  62. if err != nil {
  63. if os.IsNotExist(err) {
  64. ncutils.PrintLog("Could not configure device: "+err.Error(), 0)
  65. }
  66. }
  67. if _, err := ncutils.RunCmd(ipExec+" link set down dev "+ifacename, false); err != nil {
  68. logger.Log(2, "attempted to remove interface before editing")
  69. return err
  70. }
  71. if node.PostDown != "" {
  72. runcmds := strings.Split(node.PostDown, "; ")
  73. _ = ncutils.RunCmds(runcmds, false)
  74. }
  75. // set MTU of node interface
  76. if _, err := ncutils.RunCmd(ipExec+" link set mtu "+strconv.Itoa(int(node.MTU))+" up dev "+ifacename, true); err != nil {
  77. logger.Log(2, "failed to create interface with mtu", strconv.Itoa(int(node.MTU)), "-", ifacename)
  78. return err
  79. }
  80. if node.PostUp != "" {
  81. runcmds := strings.Split(node.PostUp, "; ")
  82. _ = ncutils.RunCmds(runcmds, true)
  83. }
  84. if node.Address6 != "" && node.IsDualStack == "yes" {
  85. logger.Log(1, "adding address:", node.Address6)
  86. _, _ = ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+node.Address6+"/64", true)
  87. }
  88. return nil
  89. }
  90. // RemoveWithoutWGQuick - Function for running the equivalent of "wg-quick down" for linux if wg-quick is missing
  91. func RemoveWithoutWGQuick(ifacename string) error {
  92. ipExec, err := exec.LookPath("ip")
  93. if err != nil {
  94. return err
  95. }
  96. out, err := ncutils.RunCmd(ipExec+" link del "+ifacename, false)
  97. dontprint := strings.Contains(out, "does not exist") || strings.Contains(out, "Cannot find device")
  98. if err != nil && !dontprint {
  99. logger.Log(1, "error running command:", ipExec, "link del", ifacename)
  100. logger.Log(1, out)
  101. }
  102. network := strings.ReplaceAll(ifacename, "nm-", "")
  103. nodeconf, err := config.ReadConfig(network)
  104. if nodeconf != nil && err == nil {
  105. if nodeconf.Node.PostDown != "" {
  106. runcmds := strings.Split(nodeconf.Node.PostDown, "; ")
  107. _ = ncutils.RunCmds(runcmds, false)
  108. }
  109. } else if err != nil {
  110. ncutils.PrintLog("error retrieving config: "+err.Error(), 1)
  111. }
  112. return err
  113. }
  114. func setKernelDevice(ifacename, address, mask string) error {
  115. ipExec, err := exec.LookPath("ip")
  116. if err != nil {
  117. return err
  118. }
  119. // == best effort ==
  120. ncutils.RunCmd("ip link delete dev "+ifacename, false)
  121. ncutils.RunCmd(ipExec+" link add dev "+ifacename+" type wireguard", true)
  122. ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+address+"/"+mask, true) // this was a bug waiting to happen
  123. return nil
  124. }