common.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. package wireguard
  2. import (
  3. "fmt"
  4. "log"
  5. "net"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/gravitl/netmaker/models"
  11. "github.com/gravitl/netmaker/netclient/config"
  12. "github.com/gravitl/netmaker/netclient/local"
  13. "github.com/gravitl/netmaker/netclient/ncutils"
  14. "github.com/gravitl/netmaker/netclient/server"
  15. "golang.zx2c4.com/wireguard/wgctrl"
  16. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  17. "gopkg.in/ini.v1"
  18. )
  19. const (
  20. section_interface = "Interface"
  21. section_peers = "Peer"
  22. )
  23. // SetPeers - sets peers on a given WireGuard interface
  24. func SetPeers(iface string, node *models.Node, peers []wgtypes.PeerConfig) error {
  25. var devicePeers []wgtypes.Peer
  26. var currentNodeAddr = node.Address
  27. var keepalive = node.PersistentKeepalive
  28. var oldPeerAllowedIps = make(map[string][]net.IPNet, len(peers))
  29. var err error
  30. client, err := wgctrl.New()
  31. if err != nil {
  32. ncutils.PrintLog("failed to start wgctrl", 0)
  33. return err
  34. }
  35. defer client.Close()
  36. device, err := client.Device(iface)
  37. if err != nil {
  38. ncutils.PrintLog("failed to parse interface", 0)
  39. return err
  40. }
  41. devicePeers = device.Peers
  42. if len(devicePeers) > 1 && len(peers) == 0 {
  43. ncutils.PrintLog("no peers pulled", 1)
  44. return err
  45. }
  46. for _, peer := range peers {
  47. for _, currentPeer := range devicePeers {
  48. if currentPeer.AllowedIPs[0].String() == peer.AllowedIPs[0].String() &&
  49. currentPeer.PublicKey.String() != peer.PublicKey.String() {
  50. _, err := ncutils.RunCmd("wg set "+iface+" peer "+currentPeer.PublicKey.String()+" remove", true)
  51. if err != nil {
  52. log.Println("error removing peer", peer.Endpoint.String())
  53. }
  54. }
  55. }
  56. udpendpoint := peer.Endpoint.String()
  57. var allowedips string
  58. var iparr []string
  59. for _, ipaddr := range peer.AllowedIPs {
  60. iparr = append(iparr, ipaddr.String())
  61. }
  62. allowedips = strings.Join(iparr, ",")
  63. keepAliveString := strconv.Itoa(int(keepalive))
  64. if keepAliveString == "0" {
  65. keepAliveString = "15"
  66. }
  67. if node.IsHub == "yes" || peer.Endpoint == nil {
  68. _, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
  69. " persistent-keepalive "+keepAliveString+
  70. " allowed-ips "+allowedips, true)
  71. } else {
  72. _, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
  73. " endpoint "+udpendpoint+
  74. " persistent-keepalive "+keepAliveString+
  75. " allowed-ips "+allowedips, true)
  76. }
  77. if err != nil {
  78. log.Println("error setting peer", peer.PublicKey.String())
  79. }
  80. }
  81. for _, currentPeer := range devicePeers {
  82. shouldDelete := true
  83. for _, peer := range peers {
  84. if peer.AllowedIPs[0].String() == currentPeer.AllowedIPs[0].String() {
  85. shouldDelete = false
  86. }
  87. // re-check this if logic is not working, added in case of allowedips not working
  88. if peer.PublicKey.String() == currentPeer.PublicKey.String() {
  89. shouldDelete = false
  90. }
  91. }
  92. if shouldDelete {
  93. output, err := ncutils.RunCmd("wg set "+iface+" peer "+currentPeer.PublicKey.String()+" remove", true)
  94. if err != nil {
  95. log.Println(output, "error removing peer", currentPeer.PublicKey.String())
  96. }
  97. }
  98. oldPeerAllowedIps[currentPeer.PublicKey.String()] = currentPeer.AllowedIPs
  99. }
  100. if ncutils.IsMac() {
  101. err = SetMacPeerRoutes(iface)
  102. return err
  103. } else if ncutils.IsLinux() {
  104. local.SetPeerRoutes(iface, currentNodeAddr, oldPeerAllowedIps, peers)
  105. }
  106. return nil
  107. }
  108. // Initializes a WireGuard interface
  109. func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig, hasGateway bool, gateways []string, syncconf bool) error {
  110. key, err := wgtypes.ParseKey(privkey)
  111. if err != nil {
  112. return err
  113. }
  114. wgclient, err := wgctrl.New()
  115. if err != nil {
  116. return err
  117. }
  118. defer wgclient.Close()
  119. modcfg, err := config.ReadConfig(node.Network)
  120. if err != nil {
  121. return err
  122. }
  123. nodecfg := modcfg.Node
  124. var ifacename string
  125. if nodecfg.Interface != "" {
  126. ifacename = nodecfg.Interface
  127. } else if node.Interface != "" {
  128. ifacename = node.Interface
  129. } else {
  130. return fmt.Errorf("no interface to configure")
  131. }
  132. if node.Address == "" {
  133. return fmt.Errorf("no address to configure")
  134. }
  135. if node.UDPHolePunch == "yes" {
  136. node.ListenPort = 0
  137. }
  138. if err := WriteWgConfig(&modcfg.Node, key.String(), peers); err != nil {
  139. ncutils.PrintLog("error writing wg conf file: "+err.Error(), 1)
  140. return err
  141. }
  142. // spin up userspace / windows interface + apply the conf file
  143. confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
  144. var deviceiface = ifacename
  145. if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
  146. deviceiface, err = local.GetMacIface(node.Address)
  147. if err != nil || deviceiface == "" {
  148. deviceiface = ifacename
  149. }
  150. }
  151. // ensure you clear any existing interface first
  152. d, _ := wgclient.Device(deviceiface)
  153. for d != nil && d.Name == deviceiface {
  154. if err = RemoveConf(deviceiface, false); err != nil { // remove interface first
  155. if strings.Contains(err.Error(), "does not exist") {
  156. err = nil
  157. break
  158. }
  159. }
  160. time.Sleep(time.Second >> 2)
  161. d, _ = wgclient.Device(deviceiface)
  162. }
  163. ApplyConf(node, ifacename, confPath) // Apply initially
  164. ncutils.PrintLog("waiting for interface...", 1) // ensure interface is created
  165. output, _ := ncutils.RunCmd("wg", false)
  166. starttime := time.Now()
  167. ifaceReady := strings.Contains(output, deviceiface)
  168. for !ifaceReady && !(time.Now().After(starttime.Add(time.Second << 4))) {
  169. if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
  170. deviceiface, err = local.GetMacIface(node.Address)
  171. if err != nil || deviceiface == "" {
  172. deviceiface = ifacename
  173. }
  174. }
  175. output, _ = ncutils.RunCmd("wg", false)
  176. err = ApplyConf(node, node.Interface, confPath)
  177. time.Sleep(time.Second)
  178. ifaceReady = strings.Contains(output, deviceiface)
  179. }
  180. //wgclient does not work well on freebsd
  181. if node.OS == "freebsd" {
  182. if !ifaceReady {
  183. return fmt.Errorf("could not reliably create interface, please check wg installation and retry")
  184. }
  185. } else {
  186. _, devErr := wgclient.Device(deviceiface)
  187. if !ifaceReady || devErr != nil {
  188. return fmt.Errorf("could not reliably create interface, please check wg installation and retry")
  189. }
  190. }
  191. ncutils.PrintLog("interface ready - netclient.. ENGAGE", 1)
  192. if syncconf { // should never be called really.
  193. err = SyncWGQuickConf(ifacename, confPath)
  194. }
  195. if !ncutils.HasWgQuick() && ncutils.IsLinux() {
  196. err = SetPeers(ifacename, node, peers)
  197. if err != nil {
  198. ncutils.PrintLog("error setting peers: "+err.Error(), 1)
  199. }
  200. time.Sleep(time.Second)
  201. }
  202. _, cidr, cidrErr := net.ParseCIDR(modcfg.NetworkSettings.AddressRange)
  203. if cidrErr == nil {
  204. local.SetCIDRRoute(ifacename, node.Address, cidr)
  205. } else {
  206. ncutils.PrintLog("could not set cidr route properly: "+cidrErr.Error(), 1)
  207. }
  208. local.SetCurrentPeerRoutes(ifacename, node.Address, peers)
  209. return err
  210. }
  211. // SetWGConfig - sets the WireGuard Config of a given network and checks if it needs a peer update
  212. func SetWGConfig(network string, peerupdate bool) error {
  213. cfg, err := config.ReadConfig(network)
  214. if err != nil {
  215. return err
  216. }
  217. servercfg := cfg.Server
  218. nodecfg := cfg.Node
  219. peers, hasGateway, gateways, err := server.GetPeers(nodecfg.MacAddress, nodecfg.Network, servercfg.GRPCAddress, nodecfg.IsDualStack == "yes", nodecfg.IsIngressGateway == "yes", nodecfg.IsServer == "yes")
  220. if err != nil {
  221. return err
  222. }
  223. privkey, err := RetrievePrivKey(network)
  224. if err != nil {
  225. return err
  226. }
  227. if peerupdate && !ncutils.IsFreeBSD() && !(ncutils.IsLinux() && !ncutils.IsKernel()) {
  228. var iface string
  229. iface = nodecfg.Interface
  230. if ncutils.IsMac() {
  231. iface, err = local.GetMacIface(nodecfg.Address)
  232. if err != nil {
  233. return err
  234. }
  235. }
  236. err = SetPeers(iface, &nodecfg, peers)
  237. } else if peerupdate {
  238. err = InitWireguard(&nodecfg, privkey, peers, hasGateway, gateways, true)
  239. } else {
  240. err = InitWireguard(&nodecfg, privkey, peers, hasGateway, gateways, false)
  241. }
  242. if nodecfg.DNSOn == "yes" {
  243. _ = local.UpdateDNS(nodecfg.Interface, nodecfg.Network, servercfg.CoreDNSAddr)
  244. }
  245. return err
  246. }
  247. // RemoveConf - removes a configuration for a given WireGuard interface
  248. func RemoveConf(iface string, printlog bool) error {
  249. os := runtime.GOOS
  250. if !ncutils.HasWgQuick() {
  251. os = "nowgquick"
  252. }
  253. var err error
  254. switch os {
  255. case "nowgquick":
  256. err = RemoveWithoutWGQuick(iface)
  257. case "windows":
  258. err = RemoveWindowsConf(iface, printlog)
  259. case "darwin":
  260. err = RemoveConfMac(iface)
  261. default:
  262. confPath := ncutils.GetNetclientPathSpecific() + iface + ".conf"
  263. err = RemoveWGQuickConf(confPath, printlog)
  264. }
  265. return err
  266. }
  267. // ApplyConf - applys a conf on disk to WireGuard interface
  268. func ApplyConf(node *models.Node, ifacename string, confPath string) error {
  269. os := runtime.GOOS
  270. if ncutils.IsLinux() && !ncutils.HasWgQuick() {
  271. os = "nowgquick"
  272. }
  273. var err error
  274. switch os {
  275. case "nowgquick":
  276. ApplyWithoutWGQuick(node, ifacename, confPath)
  277. case "windows":
  278. ApplyWindowsConf(confPath)
  279. case "darwin":
  280. ApplyMacOSConf(node, ifacename, confPath)
  281. default:
  282. ApplyWGQuickConf(confPath, ifacename)
  283. }
  284. var nodeCfg config.ClientConfig
  285. nodeCfg.Network = node.Network
  286. nodeCfg.ReadConfig()
  287. ip, cidr, err := net.ParseCIDR(nodeCfg.NetworkSettings.AddressRange)
  288. if err == nil {
  289. local.SetCIDRRoute(node.Interface, ip.String(), cidr)
  290. }
  291. return err
  292. }
  293. // WriteWgConfig - creates a wireguard config file
  294. //func WriteWgConfig(cfg *config.ClientConfig, privateKey string, peers []wgtypes.PeerConfig) error {
  295. func WriteWgConfig(node *models.Node, privateKey string, peers []wgtypes.PeerConfig) error {
  296. options := ini.LoadOptions{
  297. AllowNonUniqueSections: true,
  298. AllowShadows: true,
  299. }
  300. wireguard := ini.Empty(options)
  301. wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
  302. if node.ListenPort > 0 && node.UDPHolePunch != "yes" {
  303. wireguard.Section(section_interface).Key("ListenPort").SetValue(strconv.Itoa(int(node.ListenPort)))
  304. }
  305. if node.Address != "" {
  306. wireguard.Section(section_interface).Key("Address").SetValue(node.Address)
  307. }
  308. if node.Address6 != "" {
  309. wireguard.Section(section_interface).Key("Address").SetValue(node.Address6)
  310. }
  311. // need to figure out DNS
  312. //if node.DNSOn == "yes" {
  313. // wireguard.Section(section_interface).Key("DNS").SetValue(cfg.Server.CoreDNSAddr)
  314. //}
  315. if node.PostUp != "" {
  316. wireguard.Section(section_interface).Key("PostUp").SetValue(node.PostUp)
  317. }
  318. if node.PostDown != "" {
  319. wireguard.Section(section_interface).Key("PostDown").SetValue(node.PostDown)
  320. }
  321. if node.MTU != 0 {
  322. wireguard.Section(section_interface).Key("MTU").SetValue(strconv.FormatInt(int64(node.MTU), 10))
  323. }
  324. for i, peer := range peers {
  325. wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String())
  326. if peer.PresharedKey != nil {
  327. wireguard.SectionWithIndex(section_peers, i).Key("PreSharedKey").SetValue(peer.PresharedKey.String())
  328. }
  329. if peer.AllowedIPs != nil {
  330. var allowedIPs string
  331. for i, ip := range peer.AllowedIPs {
  332. if i == 0 {
  333. allowedIPs = ip.String()
  334. } else {
  335. allowedIPs = allowedIPs + ", " + ip.String()
  336. }
  337. }
  338. wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs)
  339. }
  340. if peer.Endpoint != nil {
  341. wireguard.SectionWithIndex(section_peers, i).Key("Endpoint").SetValue(peer.Endpoint.String())
  342. }
  343. if peer.PersistentKeepaliveInterval != nil && peer.PersistentKeepaliveInterval.Seconds() > 0 {
  344. wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepalive").SetValue(strconv.FormatInt((int64)(peer.PersistentKeepaliveInterval.Seconds()), 10))
  345. }
  346. }
  347. if err := wireguard.SaveTo(ncutils.GetNetclientPathSpecific() + node.Interface + ".conf"); err != nil {
  348. return err
  349. }
  350. return nil
  351. }
  352. // UpdateWgPeers - updates the peers of a network
  353. func UpdateWgPeers(file string, peers []wgtypes.PeerConfig) error {
  354. options := ini.LoadOptions{
  355. AllowNonUniqueSections: true,
  356. AllowShadows: true,
  357. }
  358. wireguard, err := ini.LoadSources(options, file)
  359. if err != nil {
  360. return err
  361. }
  362. //delete the peers sections as they are going to be replaced
  363. wireguard.DeleteSection(section_peers)
  364. for i, peer := range peers {
  365. wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String())
  366. if peer.PresharedKey != nil {
  367. wireguard.SectionWithIndex(section_peers, i).Key("PreSharedKey").SetValue(peer.PresharedKey.String())
  368. }
  369. if peer.AllowedIPs != nil {
  370. var allowedIPs string
  371. for i, ip := range peer.AllowedIPs {
  372. if i == 0 {
  373. allowedIPs = ip.String()
  374. } else {
  375. allowedIPs = allowedIPs + ", " + ip.String()
  376. }
  377. }
  378. wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs)
  379. }
  380. if peer.Endpoint != nil {
  381. wireguard.SectionWithIndex(section_peers, i).Key("Endpoint").SetValue(peer.Endpoint.String())
  382. }
  383. if peer.PersistentKeepaliveInterval != nil && peer.PersistentKeepaliveInterval.Seconds() > 0 {
  384. wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepalive").SetValue(strconv.FormatInt((int64)(peer.PersistentKeepaliveInterval.Seconds()), 10))
  385. }
  386. }
  387. if err := wireguard.SaveTo(file); err != nil {
  388. return err
  389. }
  390. return nil
  391. }
  392. // UpdateWgInterface - updates the interface section of a wireguard config file
  393. func UpdateWgInterface(file, privateKey, nameserver string, node models.Node) error {
  394. options := ini.LoadOptions{
  395. AllowNonUniqueSections: true,
  396. AllowShadows: true,
  397. }
  398. wireguard, err := ini.LoadSources(options, file)
  399. if err != nil {
  400. return err
  401. }
  402. if node.UDPHolePunch == "yes" {
  403. node.ListenPort = 0
  404. }
  405. wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
  406. wireguard.Section(section_interface).Key("ListenPort").SetValue(strconv.Itoa(int(node.ListenPort)))
  407. if node.Address != "" {
  408. wireguard.Section(section_interface).Key("Address").SetValue(node.Address)
  409. }
  410. if node.Address6 != "" {
  411. wireguard.Section(section_interface).Key("Address").SetValue(node.Address6)
  412. }
  413. //if node.DNSOn == "yes" {
  414. // wireguard.Section(section_interface).Key("DNS").SetValue(nameserver)
  415. //}
  416. if node.PostUp != "" {
  417. wireguard.Section(section_interface).Key("PostUp").SetValue(node.PostUp)
  418. }
  419. if node.PostDown != "" {
  420. wireguard.Section(section_interface).Key("PostDown").SetValue(node.PostDown)
  421. }
  422. if node.MTU != 0 {
  423. wireguard.Section(section_interface).Key("MTU").SetValue(strconv.FormatInt(int64(node.MTU), 10))
  424. }
  425. if err := wireguard.SaveTo(file); err != nil {
  426. return err
  427. }
  428. return nil
  429. }
  430. // UpdatePrivateKey - updates the private key of a wireguard config file
  431. func UpdatePrivateKey(file, privateKey string) error {
  432. options := ini.LoadOptions{
  433. AllowNonUniqueSections: true,
  434. AllowShadows: true,
  435. }
  436. wireguard, err := ini.LoadSources(options, file)
  437. if err != nil {
  438. return err
  439. }
  440. wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
  441. if err := wireguard.SaveTo(file); err != nil {
  442. return err
  443. }
  444. return nil
  445. }