wireguard.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package serverctl
  2. import (
  3. "os"
  4. "log"
  5. "context"
  6. "time"
  7. "net"
  8. "strconv"
  9. "errors"
  10. "github.com/vishvananda/netlink"
  11. "golang.zx2c4.com/wireguard/wgctrl"
  12. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  13. "github.com/gravitl/netmaker/servercfg"
  14. "github.com/gravitl/netmaker/functions"
  15. "github.com/gravitl/netmaker/models"
  16. "github.com/gravitl/netmaker/mongoconn"
  17. )
  18. func InitServerWireGuard() error {
  19. created, err := CreateCommsNetwork()
  20. if !created {
  21. return err
  22. }
  23. wgconfig := servercfg.GetWGConfig()
  24. if !(wgconfig.GRPCWireGuard == "on") {
  25. return errors.New("WireGuard not enabled on this server.")
  26. }
  27. ifaceSettings := netlink.NewLinkAttrs()
  28. if wgconfig.GRPCWGInterface == "" {
  29. return errors.New("No WireGuard Interface Name set.")
  30. }
  31. ifaceSettings.Name = wgconfig.GRPCWGInterface
  32. wglink := &models.WireGuardLink{LinkAttrs: &ifaceSettings}
  33. err = netlink.LinkAdd(wglink)
  34. if err != nil {
  35. if os.IsExist(err) {
  36. log.Println("interface " + ifaceSettings.Name + " already exists")
  37. log.Println("continuing setup using existing interface")
  38. } else {
  39. return err
  40. }
  41. }
  42. address, err := netlink.ParseAddr(wgconfig.GRPCWGAddress + "/24")
  43. if err != nil {
  44. return err
  45. }
  46. err = netlink.AddrAdd(wglink, address)
  47. if err != nil {
  48. if os.IsExist(err) {
  49. log.Println("address " + wgconfig.GRPCWGAddress + " already exists")
  50. log.Println("continuing with existing setup")
  51. } else {
  52. return err
  53. }
  54. }
  55. err = netlink.LinkSetUp(wglink)
  56. if err != nil {
  57. log.Println("could not bring up wireguard interface")
  58. return err
  59. }
  60. var client models.IntClient
  61. client.PrivateKey = wgconfig.GRPCWGPrivKey
  62. client.PublicKey = wgconfig.GRPCWGPubKey
  63. client.ServerEndpoint = wgconfig.GRPCWGEndpoint
  64. client.ServerAddress = wgconfig.GRPCWGAddress
  65. client.ServerPort = wgconfig.GRPCWGPort
  66. client.Address = wgconfig.GRPCWGAddress
  67. client.IsServer = "yes"
  68. client.Network = "comms"
  69. err = RegisterServer(client)
  70. return err
  71. }
  72. func RegisterServer(client models.IntClient) error {
  73. if client.PrivateKey == "" {
  74. privateKey, err := wgtypes.GeneratePrivateKey()
  75. if err != nil {
  76. return err
  77. }
  78. client.PrivateKey = privateKey.String()
  79. client.PublicKey = privateKey.PublicKey().String()
  80. }
  81. if client.Address == "" {
  82. newAddress, err := functions.UniqueAddress(client.Network)
  83. if err != nil {
  84. return err
  85. }
  86. if newAddress == "" {
  87. return errors.New("Could not retrieve address")
  88. }
  89. client.Address = newAddress
  90. }
  91. if client.Network == "" { client.Network = "comms" }
  92. client.ServerKey = client.PublicKey
  93. collection := mongoconn.Client.Database("netmaker").Collection("intclients")
  94. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  95. // insert our network into the network table
  96. _, err := collection.InsertOne(ctx, client)
  97. defer cancel()
  98. ReconfigureServerWireGuard()
  99. return err
  100. }
  101. func ReconfigureServerWireGuard() error {
  102. server, err := GetServerWGConf()
  103. if err != nil {
  104. return err
  105. }
  106. serverkey, err := wgtypes.ParseKey(server.PrivateKey)
  107. if err != nil {
  108. return err
  109. }
  110. serverport, err := strconv.Atoi(servercfg.GetGRPCWGPort())
  111. if err != nil {
  112. return err
  113. }
  114. peers, err := functions.GetIntPeersList()
  115. if err != nil {
  116. return err
  117. }
  118. wgserver, err := wgctrl.New()
  119. if err != nil {
  120. return err
  121. }
  122. var serverpeers []wgtypes.PeerConfig
  123. for _, peer := range peers {
  124. pubkey, err := wgtypes.ParseKey(peer.PublicKey)
  125. if err != nil {
  126. return err
  127. }
  128. var peercfg wgtypes.PeerConfig
  129. var allowedips []net.IPNet
  130. if peer.Address != "" {
  131. var peeraddr = net.IPNet{
  132. IP: net.ParseIP(peer.Address),
  133. Mask: net.CIDRMask(128, 128),
  134. }
  135. allowedips = append(allowedips, peeraddr)
  136. }
  137. if peer.Address6 != "" {
  138. var addr6 = net.IPNet{
  139. IP: net.ParseIP(peer.Address6),
  140. Mask: net.CIDRMask(128, 128),
  141. }
  142. allowedips = append(allowedips, addr6)
  143. }
  144. peercfg = wgtypes.PeerConfig{
  145. PublicKey: pubkey,
  146. ReplaceAllowedIPs: true,
  147. AllowedIPs: allowedips,
  148. }
  149. serverpeers = append(serverpeers, peercfg)
  150. }
  151. wgconf := wgtypes.Config{
  152. PrivateKey: &serverkey,
  153. ListenPort: &serverport,
  154. ReplacePeers: true,
  155. Peers: serverpeers,
  156. }
  157. err = wgserver.ConfigureDevice(servercfg.GetGRPCWGInterface(), wgconf)
  158. if err != nil {
  159. return err
  160. }
  161. return nil
  162. }