noquick.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if node.Address != "" {
  50. netmaskArr := strings.Split(node.NetworkSettings.AddressRange, "/")
  51. var netmask = "32"
  52. if len(netmaskArr) == 2 {
  53. netmask = netmaskArr[1]
  54. }
  55. setKernelDevice(ifacename, node.Address, netmask)
  56. }
  57. if node.Address6 != "" {
  58. netmaskArr := strings.Split(node.NetworkSettings.AddressRange6, "/")
  59. var netmask = "128"
  60. if len(netmaskArr) == 2 {
  61. netmask = netmaskArr[1]
  62. }
  63. setKernelDevice(ifacename, node.Address6, netmask)
  64. }
  65. _, err = wgclient.Device(ifacename)
  66. if err != nil {
  67. if !os.IsNotExist(err) {
  68. return errors.New("Unknown config error: " + err.Error())
  69. }
  70. }
  71. err = wgclient.ConfigureDevice(ifacename, conf)
  72. if err != nil {
  73. if os.IsNotExist(err) {
  74. logger.Log(0, "Could not configure device: ", err.Error())
  75. }
  76. }
  77. if _, err := ncutils.RunCmd(ipExec+" link set down dev "+ifacename, false); err != nil {
  78. logger.Log(1, "attempted to remove interface before editing")
  79. return err
  80. }
  81. if node.PostDown != "" {
  82. runcmds := strings.Split(node.PostDown, "; ")
  83. _ = ncutils.RunCmds(runcmds, false)
  84. }
  85. // set MTU of node interface
  86. if _, err := ncutils.RunCmd(ipExec+" link set mtu "+strconv.Itoa(int(node.MTU))+" up dev "+ifacename, true); err != nil {
  87. logger.Log(1, "failed to create interface with mtu ", strconv.Itoa(int(node.MTU)), "-", ifacename)
  88. return err
  89. }
  90. if node.PostUp != "" {
  91. runcmds := strings.Split(node.PostUp, "; ")
  92. _ = ncutils.RunCmds(runcmds, true)
  93. }
  94. if node.Address6 != "" {
  95. logger.Log(1, "adding address: ", node.Address6)
  96. netmaskArr := strings.Split(node.NetworkSettings.AddressRange6, "/")
  97. var netmask = "64"
  98. if len(netmaskArr) == 2 {
  99. netmask = netmaskArr[1]
  100. }
  101. ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+node.Address6+"/"+netmask, true)
  102. }
  103. return nil
  104. }
  105. // RemoveWithoutWGQuick - Function for running the equivalent of "wg-quick down" for linux if wg-quick is missing
  106. func RemoveWithoutWGQuick(ifacename string) error {
  107. ipExec, err := exec.LookPath("ip")
  108. if err != nil {
  109. return err
  110. }
  111. out, err := ncutils.RunCmd(ipExec+" link del "+ifacename, false)
  112. dontprint := strings.Contains(out, "does not exist") || strings.Contains(out, "Cannot find device")
  113. if err != nil && !dontprint {
  114. logger.Log(1, "error running command: ", ipExec, " link del ", ifacename)
  115. logger.Log(1, out)
  116. }
  117. network := strings.ReplaceAll(ifacename, "nm-", "")
  118. nodeconf, err := config.ReadConfig(network)
  119. if nodeconf != nil && err == nil {
  120. if nodeconf.Node.PostDown != "" {
  121. runcmds := strings.Split(nodeconf.Node.PostDown, "; ")
  122. _ = ncutils.RunCmds(runcmds, false)
  123. }
  124. } else if err != nil {
  125. logger.Log(1, "error retrieving config: ", err.Error())
  126. }
  127. return err
  128. }
  129. func setKernelDevice(ifacename, address, mask string) error {
  130. ipExec, err := exec.LookPath("ip")
  131. if err != nil {
  132. return err
  133. }
  134. // == best effort ==
  135. ncutils.RunCmd("ip link delete dev "+ifacename, false)
  136. ncutils.RunCmd(ipExec+" link add dev "+ifacename+" type wireguard", true)
  137. ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+address+"/"+mask, true) // this was a bug waiting to happen
  138. return nil
  139. }