routes_linux.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //go:build linux
  2. // +build linux
  3. package local
  4. import (
  5. //"github.com/davecgh/go-spew/spew"
  6. "net"
  7. "github.com/gravitl/netmaker/netclient/ncutils"
  8. "golang.zx2c4.com/wireguard/wgctrl"
  9. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  10. )
  11. func routeExists(iface, address, mask string) bool {
  12. return false
  13. }
  14. func setRoute(iface, address, mask string) error {
  15. _, err := ncutils.RunCmd("ip route add", true)
  16. return err
  17. }
  18. func DeleteRoute(iface, address string) error {
  19. var err error
  20. return err
  21. }
  22. func ApplyRoutesFromConf(confPath string) error {
  23. var err error
  24. return err
  25. }
  26. //func SetLinuxPeerRoutes(currentPeers []wgtypes.PeerConfig, newPeers []wgtypes.PeerConfig) {
  27. func SetLinuxPeerRoutes(iface string, oldPeers map[string][]net.IP, newPeers []wgtypes.PeerConfig) {
  28. // traverse through all recieved peers
  29. for _, peer := range newPeers {
  30. // if pubkey found in existing peers, check against existing peer
  31. currPeerAllowedIPs := oldPeers[peer.PublicKey.String()]
  32. if currPeerAllowedIPs != nil {
  33. // traverse IPs, check to see if old peer contains each IP
  34. for _, allowedIP := range peer.AllowedIPs {
  35. if !ncutils.StringSliceContains(currPeerAllowedIPs, allowedIP.IP.String()) {
  36. if err := setRoute(iface, allowedIP.IP.String(), allowedIP.Mask.String()); err != nil {
  37. ncutils.PrintLog(err.Error(), 1)
  38. }
  39. }
  40. }
  41. for _, allowedIP := range currPeerAllowedIPs {
  42. if !ncutils.StringSliceContains(currPeerAllowedIPs, allowedIP) {
  43. if err := setRoute(iface, allowedIP.IP.String(), allowedIP.Mask.String()); err != nil {
  44. ncutils.PrintLog(err.Error(), 1)
  45. }
  46. }
  47. }
  48. delete(oldPeers, peer.PublicKey.String())
  49. } else {
  50. for _, allowedIP := range peer.AllowedIPs {
  51. if err := setRoute(iface, allowedIP.IP.String(), allowedIP.Mask.String()); err != nil {
  52. ncutils.PrintLog(err.Error(), 1)
  53. }
  54. }
  55. }
  56. }
  57. // traverse through all existing peers
  58. for _, peer := range oldPeers {
  59. // if pubkey found in existing peers, check against existing peer
  60. currPeerAllowedIPs := oldPeers[peer.PublicKey.String()]
  61. if currPeerAllowedIPs != nil {
  62. // traverse IPs, check to see if old peer contains each IP
  63. for _, allowedIP := range peer.AllowedIPs {
  64. if !ncutils.StringSliceContains(currPeerAllowedIPs, allowedIP.IP.String()) {
  65. if err := setRoute(iface, allowedIP.IP.String(), allowedIP.Mask.String()); err != nil {
  66. ncutils.PrintLog(err.Error(), 1)
  67. }
  68. }
  69. }
  70. } else {
  71. for _, allowedIP := range peer.AllowedIPs {
  72. if err := setRoute(iface, allowedIP.IP.String(), allowedIP.Mask.String()); err != nil {
  73. ncutils.PrintLog(err.Error(), 1)
  74. }
  75. }
  76. }
  77. }
  78. // delete removed AllowedIPs
  79. /*
  80. for _, currentPeer := range currentPeers {
  81. for _, oldIP := range currentPeer.AllowedIPs {
  82. found := true
  83. for _, newPeer := range newPeers {
  84. for _, newIP := range newPeer.AllowedIPs {
  85. if
  86. }
  87. }
  88. }
  89. }
  90. */
  91. }
  92. func GetCurrentIPs() []string {
  93. client, err := wgctrl.New()
  94. if err != nil {
  95. ncutils.PrintLog("failed to start wgctrl", 0)
  96. return err
  97. }
  98. defer client.Close()
  99. device, err := client.Device(iface)
  100. if err != nil {
  101. ncutils.PrintLog("failed to parse interface", 0)
  102. return err
  103. }
  104. devicePeers = device.Peers
  105. }