common.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. package wireguard
  2. import (
  3. "fmt"
  4. "log"
  5. "net"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/gravitl/netmaker/logger"
  11. "github.com/gravitl/netmaker/models"
  12. "github.com/gravitl/netmaker/netclient/config"
  13. "github.com/gravitl/netmaker/netclient/local"
  14. "github.com/gravitl/netmaker/netclient/ncutils"
  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 keepalive = node.PersistentKeepalive
  27. var oldPeerAllowedIps = make(map[string]bool, len(peers))
  28. internetV4Gateway := false
  29. internetV6Gateway := false
  30. var err error
  31. devicePeers, err = GetDevicePeers(iface)
  32. if err != nil {
  33. return err
  34. }
  35. if len(devicePeers) > 1 && len(peers) == 0 {
  36. logger.Log(1, "no peers pulled")
  37. return err
  38. }
  39. for _, peer := range peers {
  40. // make sure peer has AllowedIP's before comparison
  41. hasPeerIP := len(peer.AllowedIPs) > 0
  42. for _, currentPeer := range devicePeers {
  43. // make sure currenPeer has AllowedIP's before comparison
  44. hascurrentPeerIP := len(currentPeer.AllowedIPs) > 0
  45. if hasPeerIP && hascurrentPeerIP &&
  46. currentPeer.AllowedIPs[0].String() == peer.AllowedIPs[0].String() &&
  47. currentPeer.PublicKey.String() != peer.PublicKey.String() {
  48. _, err := ncutils.RunCmd("wg set "+iface+" peer "+currentPeer.PublicKey.String()+" remove", true)
  49. if err != nil {
  50. log.Println("error removing peer", peer.Endpoint.String())
  51. }
  52. }
  53. }
  54. udpendpoint := peer.Endpoint.String()
  55. var allowedips string
  56. var iparr []string
  57. for _, ipaddr := range peer.AllowedIPs {
  58. if hasPeerIP {
  59. if ipaddr.String() == "0.0.0.0/0" {
  60. if node.IsServer == "yes" {
  61. //skip server
  62. logger.Log(2, "skipping internet gateway for server")
  63. continue
  64. }
  65. internetV4Gateway = true
  66. }
  67. if ipaddr.String() == "::/0" {
  68. if node.IsServer == "yes" {
  69. //skip server
  70. logger.Log(2, "skipping internet gateway for server")
  71. continue
  72. }
  73. internetV6Gateway = true
  74. }
  75. iparr = append(iparr, ipaddr.String())
  76. }
  77. }
  78. if len(iparr) > 0 {
  79. allowedips = strings.Join(iparr, ",")
  80. }
  81. keepAliveString := strconv.Itoa(int(keepalive))
  82. if keepAliveString == "0" {
  83. keepAliveString = "15"
  84. }
  85. if node.IsServer == "yes" || peer.Endpoint == nil {
  86. _, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
  87. " persistent-keepalive "+keepAliveString+
  88. " allowed-ips "+allowedips, true)
  89. } else {
  90. _, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
  91. " endpoint "+udpendpoint+
  92. " persistent-keepalive "+keepAliveString+
  93. " allowed-ips "+allowedips, true)
  94. }
  95. if err != nil {
  96. log.Println("error setting peer", peer.PublicKey.String())
  97. }
  98. }
  99. if len(devicePeers) > 0 {
  100. for _, currentPeer := range devicePeers {
  101. shouldDelete := true
  102. if len(peers) > 0 {
  103. for _, peer := range peers {
  104. if len(peer.AllowedIPs) > 0 && len(currentPeer.AllowedIPs) > 0 &&
  105. peer.AllowedIPs[0].String() == currentPeer.AllowedIPs[0].String() {
  106. shouldDelete = false
  107. }
  108. // re-check this if logic is not working, added in case of allowedips not working
  109. if peer.PublicKey.String() == currentPeer.PublicKey.String() {
  110. shouldDelete = false
  111. }
  112. v4, v6 := shouldDeleteInternetGateway(peer.AllowedIPs, currentPeer.AllowedIPs)
  113. if v4 || v6 {
  114. if local.RemoveInternetGatewayRoute(node.Interface, strconv.Itoa(int(node.ListenPort)), v4, v6); err != nil {
  115. logger.Log(0, "failed to remove internet gateways routes", err.Error())
  116. }
  117. }
  118. }
  119. if shouldDelete {
  120. output, err := ncutils.RunCmd("wg set "+iface+" peer "+currentPeer.PublicKey.String()+" remove", true)
  121. if err != nil {
  122. log.Println(output, "error removing peer", currentPeer.PublicKey.String())
  123. }
  124. }
  125. for _, ip := range currentPeer.AllowedIPs {
  126. oldPeerAllowedIps[ip.String()] = true
  127. }
  128. }
  129. }
  130. }
  131. //TODO === why only Mac/Linux????
  132. if ncutils.IsMac() {
  133. err = SetMacPeerRoutes(iface)
  134. return err
  135. } else if ncutils.IsLinux() {
  136. if len(peers) > 0 {
  137. local.SetPeerRoutes(iface, oldPeerAllowedIps, peers)
  138. }
  139. }
  140. //check if internet gateway
  141. if internetV4Gateway || internetV6Gateway {
  142. if err := local.SetInternetGatewayRoute(node.Interface, strconv.Itoa(int(node.ListenPort)), internetV4Gateway, internetV6Gateway); err != nil {
  143. return err
  144. }
  145. }
  146. return nil
  147. }
  148. // Initializes a WireGuard interface
  149. func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig, syncconf bool) error {
  150. key, err := wgtypes.ParseKey(privkey)
  151. if err != nil {
  152. return err
  153. }
  154. wgclient, err := wgctrl.New()
  155. if err != nil {
  156. return err
  157. }
  158. defer wgclient.Close()
  159. //nodecfg := modcfg.Node
  160. var ifacename string
  161. if node.Interface != "" {
  162. ifacename = node.Interface
  163. } else {
  164. return fmt.Errorf("no interface to configure")
  165. }
  166. if node.PrimaryAddress() == "" {
  167. return fmt.Errorf("no address to configure")
  168. }
  169. if err := WriteWgConfig(node, key.String(), peers); err != nil {
  170. logger.Log(1, "error writing wg conf file: ", err.Error())
  171. return err
  172. }
  173. // spin up userspace / windows interface + apply the conf file
  174. confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
  175. var deviceiface = ifacename
  176. var mErr error
  177. if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
  178. deviceiface, mErr = local.GetMacIface(node.PrimaryAddress())
  179. if mErr != nil || deviceiface == "" {
  180. deviceiface = ifacename
  181. }
  182. }
  183. // ensure you clear any existing interface first
  184. RemoveConfGraceful(deviceiface)
  185. ApplyConf(node, ifacename, confPath) // Apply initially
  186. logger.Log(1, "waiting for interface...") // ensure interface is created
  187. output, _ := ncutils.RunCmd("wg", false)
  188. starttime := time.Now()
  189. ifaceReady := strings.Contains(output, deviceiface)
  190. for !ifaceReady && !(time.Now().After(starttime.Add(time.Second << 4))) {
  191. if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
  192. deviceiface, mErr = local.GetMacIface(node.PrimaryAddress())
  193. if mErr != nil || deviceiface == "" {
  194. deviceiface = ifacename
  195. }
  196. }
  197. output, _ = ncutils.RunCmd("wg", false)
  198. err = ApplyConf(node, node.Interface, confPath)
  199. time.Sleep(time.Second)
  200. ifaceReady = strings.Contains(output, deviceiface)
  201. }
  202. //wgclient does not work well on freebsd
  203. if node.OS == "freebsd" {
  204. if !ifaceReady {
  205. return fmt.Errorf("could not reliably create interface, please check wg installation and retry")
  206. }
  207. } else {
  208. _, devErr := wgclient.Device(deviceiface)
  209. if !ifaceReady || devErr != nil {
  210. fmt.Printf("%v\n", devErr)
  211. return fmt.Errorf("could not reliably create interface, please check wg installation and retry")
  212. }
  213. }
  214. logger.Log(1, "interface ready - netclient.. ENGAGE")
  215. if syncconf { // should never be called really.
  216. fmt.Println("why here")
  217. err = SyncWGQuickConf(ifacename, confPath)
  218. }
  219. if !ncutils.HasWgQuick() && ncutils.IsLinux() {
  220. err = SetPeers(ifacename, node, peers)
  221. if err != nil {
  222. logger.Log(1, "error setting peers: ", err.Error())
  223. }
  224. time.Sleep(time.Second)
  225. }
  226. //ipv4
  227. if node.Address != "" {
  228. _, cidr, cidrErr := net.ParseCIDR(node.NetworkSettings.AddressRange)
  229. if cidrErr == nil {
  230. local.SetCIDRRoute(ifacename, node.Address, cidr)
  231. } else {
  232. logger.Log(1, "could not set cidr route properly: ", cidrErr.Error())
  233. }
  234. local.SetCurrentPeerRoutes(ifacename, node.Address, peers)
  235. }
  236. if node.Address6 != "" {
  237. //ipv6
  238. _, cidr, cidrErr := net.ParseCIDR(node.NetworkSettings.AddressRange6)
  239. if cidrErr == nil {
  240. local.SetCIDRRoute(ifacename, node.Address6, cidr)
  241. } else {
  242. logger.Log(1, "could not set cidr route properly: ", cidrErr.Error())
  243. }
  244. local.SetCurrentPeerRoutes(ifacename, node.Address6, peers)
  245. }
  246. return err
  247. }
  248. // SetWGConfig - sets the WireGuard Config of a given network and checks if it needs a peer update
  249. func SetWGConfig(network string, peerupdate bool, peers []wgtypes.PeerConfig) error {
  250. cfg, err := config.ReadConfig(network)
  251. if err != nil {
  252. return err
  253. }
  254. privkey, err := RetrievePrivKey(network)
  255. if err != nil {
  256. return err
  257. }
  258. if peerupdate && !ncutils.IsFreeBSD() && !(ncutils.IsLinux() && !ncutils.IsKernel()) {
  259. var iface string
  260. iface = cfg.Node.Interface
  261. if ncutils.IsMac() {
  262. iface, err = local.GetMacIface(cfg.Node.PrimaryAddress())
  263. if err != nil {
  264. return err
  265. }
  266. }
  267. err = SetPeers(iface, &cfg.Node, peers)
  268. } else if peerupdate {
  269. err = InitWireguard(&cfg.Node, privkey, peers, true)
  270. } else {
  271. err = InitWireguard(&cfg.Node, privkey, peers, false)
  272. }
  273. return err
  274. }
  275. // RemoveConf - removes a configuration for a given WireGuard interface
  276. func RemoveConf(iface string, printlog bool) error {
  277. os := runtime.GOOS
  278. if ncutils.IsLinux() && !ncutils.HasWgQuick() {
  279. os = "nowgquick"
  280. }
  281. var err error
  282. switch os {
  283. case "nowgquick":
  284. err = RemoveWithoutWGQuick(iface)
  285. case "windows":
  286. err = RemoveWindowsConf(iface, printlog)
  287. case "darwin":
  288. err = RemoveConfMac(iface)
  289. default:
  290. confPath := ncutils.GetNetclientPathSpecific() + iface + ".conf"
  291. err = RemoveWGQuickConf(confPath, printlog)
  292. }
  293. return err
  294. }
  295. // ApplyConf - applys a conf on disk to WireGuard interface
  296. func ApplyConf(node *models.Node, ifacename string, confPath string) error {
  297. os := runtime.GOOS
  298. if ncutils.IsLinux() && !ncutils.HasWgQuick() {
  299. os = "nowgquick"
  300. }
  301. var err error
  302. switch os {
  303. case "windows":
  304. ApplyWindowsConf(confPath)
  305. case "darwin":
  306. ApplyMacOSConf(node, ifacename, confPath)
  307. case "nowgquick":
  308. ApplyWithoutWGQuick(node, ifacename, confPath)
  309. default:
  310. ApplyWGQuickConf(confPath, ifacename)
  311. }
  312. var nodeCfg config.ClientConfig
  313. nodeCfg.Network = node.Network
  314. nodeCfg.ReadConfig()
  315. if nodeCfg.NetworkSettings.AddressRange != "" {
  316. ip, cidr, err := net.ParseCIDR(nodeCfg.NetworkSettings.AddressRange)
  317. if err == nil {
  318. local.SetCIDRRoute(node.Interface, ip.String(), cidr)
  319. }
  320. }
  321. if nodeCfg.NetworkSettings.AddressRange6 != "" {
  322. ip, cidr, err := net.ParseCIDR(nodeCfg.NetworkSettings.AddressRange6)
  323. if err == nil {
  324. local.SetCIDRRoute(node.Interface, ip.String(), cidr)
  325. }
  326. }
  327. return err
  328. }
  329. // WriteWgConfig - creates a wireguard config file
  330. // func WriteWgConfig(cfg *config.ClientConfig, privateKey string, peers []wgtypes.PeerConfig) error {
  331. func WriteWgConfig(node *models.Node, privateKey string, peers []wgtypes.PeerConfig) error {
  332. options := ini.LoadOptions{
  333. AllowNonUniqueSections: true,
  334. AllowShadows: true,
  335. }
  336. wireguard := ini.Empty(options)
  337. wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
  338. if node.ListenPort > 0 && node.UDPHolePunch != "yes" {
  339. wireguard.Section(section_interface).Key("ListenPort").SetValue(strconv.Itoa(int(node.ListenPort)))
  340. }
  341. addrString := node.Address
  342. if node.Address6 != "" {
  343. if addrString != "" {
  344. addrString += ","
  345. }
  346. addrString += node.Address6
  347. }
  348. wireguard.Section(section_interface).Key("Address").SetValue(addrString)
  349. // need to figure out DNS
  350. //if node.DNSOn == "yes" {
  351. // wireguard.Section(section_interface).Key("DNS").SetValue(cfg.Server.CoreDNSAddr)
  352. //}
  353. if node.PostUp != "" {
  354. wireguard.Section(section_interface).Key("PostUp").SetValue(node.PostUp)
  355. }
  356. if node.PostDown != "" {
  357. wireguard.Section(section_interface).Key("PostDown").SetValue(node.PostDown)
  358. }
  359. if node.MTU != 0 {
  360. wireguard.Section(section_interface).Key("MTU").SetValue(strconv.FormatInt(int64(node.MTU), 10))
  361. }
  362. for i, peer := range peers {
  363. wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String())
  364. if peer.PresharedKey != nil {
  365. wireguard.SectionWithIndex(section_peers, i).Key("PreSharedKey").SetValue(peer.PresharedKey.String())
  366. }
  367. if peer.AllowedIPs != nil {
  368. var allowedIPs string
  369. for i, ip := range peer.AllowedIPs {
  370. if i == 0 {
  371. allowedIPs = ip.String()
  372. } else {
  373. allowedIPs = allowedIPs + ", " + ip.String()
  374. }
  375. }
  376. wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs)
  377. }
  378. if peer.Endpoint != nil {
  379. wireguard.SectionWithIndex(section_peers, i).Key("Endpoint").SetValue(peer.Endpoint.String())
  380. }
  381. if peer.PersistentKeepaliveInterval != nil && peer.PersistentKeepaliveInterval.Seconds() > 0 {
  382. wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepalive").SetValue(strconv.FormatInt((int64)(peer.PersistentKeepaliveInterval.Seconds()), 10))
  383. }
  384. }
  385. if err := wireguard.SaveTo(ncutils.GetNetclientPathSpecific() + node.Interface + ".conf"); err != nil {
  386. return err
  387. }
  388. return nil
  389. }
  390. // UpdateWgPeers - updates the peers of a network
  391. func UpdateWgPeers(file string, peers []wgtypes.PeerConfig) error {
  392. options := ini.LoadOptions{
  393. AllowNonUniqueSections: true,
  394. AllowShadows: true,
  395. }
  396. wireguard, err := ini.LoadSources(options, file)
  397. if err != nil {
  398. return err
  399. }
  400. //delete the peers sections as they are going to be replaced
  401. wireguard.DeleteSection(section_peers)
  402. for i, peer := range peers {
  403. wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String())
  404. if peer.PresharedKey != nil {
  405. wireguard.SectionWithIndex(section_peers, i).Key("PreSharedKey").SetValue(peer.PresharedKey.String())
  406. }
  407. if peer.AllowedIPs != nil {
  408. var allowedIPs string
  409. for i, ip := range peer.AllowedIPs {
  410. if i == 0 {
  411. allowedIPs = ip.String()
  412. } else {
  413. allowedIPs = allowedIPs + ", " + ip.String()
  414. }
  415. }
  416. wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs)
  417. }
  418. if peer.Endpoint != nil {
  419. wireguard.SectionWithIndex(section_peers, i).Key("Endpoint").SetValue(peer.Endpoint.String())
  420. }
  421. if peer.PersistentKeepaliveInterval != nil && peer.PersistentKeepaliveInterval.Seconds() > 0 {
  422. wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepalive").SetValue(strconv.FormatInt((int64)(peer.PersistentKeepaliveInterval.Seconds()), 10))
  423. }
  424. }
  425. if err := wireguard.SaveTo(file); err != nil {
  426. return err
  427. }
  428. return nil
  429. }
  430. // UpdateWgInterface - updates the interface section of a wireguard config file
  431. func UpdateWgInterface(file, privateKey, nameserver string, node models.Node) 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. if node.UDPHolePunch == "yes" {
  441. node.ListenPort = 0
  442. }
  443. wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
  444. wireguard.Section(section_interface).Key("ListenPort").SetValue(strconv.Itoa(int(node.ListenPort)))
  445. addrString := node.Address
  446. if node.Address6 != "" {
  447. if addrString != "" {
  448. addrString += ","
  449. }
  450. addrString += node.Address6
  451. }
  452. wireguard.Section(section_interface).Key("Address").SetValue(addrString)
  453. //if node.DNSOn == "yes" {
  454. // wireguard.Section(section_interface).Key("DNS").SetValue(nameserver)
  455. //}
  456. if node.PostUp != "" {
  457. wireguard.Section(section_interface).Key("PostUp").SetValue(node.PostUp)
  458. }
  459. if node.PostDown != "" {
  460. wireguard.Section(section_interface).Key("PostDown").SetValue(node.PostDown)
  461. }
  462. if node.MTU != 0 {
  463. wireguard.Section(section_interface).Key("MTU").SetValue(strconv.FormatInt(int64(node.MTU), 10))
  464. }
  465. if err := wireguard.SaveTo(file); err != nil {
  466. return err
  467. }
  468. return nil
  469. }
  470. // UpdatePrivateKey - updates the private key of a wireguard config file
  471. func UpdatePrivateKey(file, privateKey string) error {
  472. options := ini.LoadOptions{
  473. AllowNonUniqueSections: true,
  474. AllowShadows: true,
  475. }
  476. wireguard, err := ini.LoadSources(options, file)
  477. if err != nil {
  478. return err
  479. }
  480. wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
  481. if err := wireguard.SaveTo(file); err != nil {
  482. return err
  483. }
  484. return nil
  485. }
  486. // UpdateKeepAlive - updates the persistentkeepalive of all peers
  487. func UpdateKeepAlive(file string, keepalive int32) error {
  488. options := ini.LoadOptions{
  489. AllowNonUniqueSections: true,
  490. AllowShadows: true,
  491. }
  492. wireguard, err := ini.LoadSources(options, file)
  493. if err != nil {
  494. return err
  495. }
  496. peers, err := wireguard.SectionsByName(section_peers)
  497. if err != nil {
  498. return err
  499. }
  500. newvalue := strconv.Itoa(int(keepalive))
  501. for i := range peers {
  502. wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepALive").SetValue(newvalue)
  503. }
  504. if err := wireguard.SaveTo(file); err != nil {
  505. return err
  506. }
  507. return nil
  508. }
  509. // RemoveConfGraceful - Run remove conf and wait for it to actually be gone before proceeding
  510. func RemoveConfGraceful(ifacename string) {
  511. // ensure you clear any existing interface first
  512. wgclient, err := wgctrl.New()
  513. if err != nil {
  514. logger.Log(0, "could not create wgclient")
  515. return
  516. }
  517. defer wgclient.Close()
  518. d, _ := wgclient.Device(ifacename)
  519. startTime := time.Now()
  520. for d != nil && d.Name == ifacename {
  521. if err = RemoveConf(ifacename, false); err != nil { // remove interface first
  522. if strings.Contains(err.Error(), "does not exist") {
  523. err = nil
  524. break
  525. }
  526. }
  527. time.Sleep(time.Second >> 2)
  528. d, _ = wgclient.Device(ifacename)
  529. if time.Now().After(startTime.Add(time.Second << 4)) {
  530. break
  531. }
  532. }
  533. time.Sleep(time.Second << 1)
  534. }
  535. // GetDevicePeers - gets the current device's peers
  536. func GetDevicePeers(iface string) ([]wgtypes.Peer, error) {
  537. if ncutils.IsFreeBSD() {
  538. if devicePeers, err := ncutils.GetPeers(iface); err != nil {
  539. return nil, err
  540. } else {
  541. return devicePeers, nil
  542. }
  543. } else {
  544. client, err := wgctrl.New()
  545. if err != nil {
  546. logger.Log(0, "failed to start wgctrl")
  547. return nil, err
  548. }
  549. defer client.Close()
  550. device, err := client.Device(iface)
  551. if err != nil {
  552. logger.Log(0, "failed to parse interface", iface)
  553. return nil, err
  554. }
  555. return device.Peers, nil
  556. }
  557. }
  558. func shouldDeleteInternetGateway(new, current []net.IPNet) (bool, bool) {
  559. oldv4gatewayExists := false
  560. newv4gatewayExists := false
  561. oldv6gatewayExists := false
  562. newv6gatewayExists := false
  563. v4 := false
  564. v6 := false
  565. for _, ip := range current {
  566. if ip.String() == "0.0.0.0/0" {
  567. oldv4gatewayExists = true
  568. }
  569. if ip.String() == "::/0" {
  570. oldv6gatewayExists = true
  571. }
  572. }
  573. for _, ip := range new {
  574. if ip.String() == "0.0.0.0/0" {
  575. newv4gatewayExists = true
  576. }
  577. if ip.String() == "::/0" {
  578. newv6gatewayExists = true
  579. }
  580. }
  581. if oldv4gatewayExists && !newv4gatewayExists {
  582. v4 = true
  583. }
  584. if oldv6gatewayExists && !newv6gatewayExists {
  585. v6 = true
  586. }
  587. return v4, v6
  588. }