iface.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package ncutils
  2. import (
  3. "net"
  4. "github.com/gravitl/netmaker/models"
  5. )
  6. // IfaceDelta - checks if the new node causes an interface change
  7. func IfaceDelta(currentNode *models.LegacyNode, newNode *models.LegacyNode) bool {
  8. // single comparison statements
  9. if newNode.Endpoint != currentNode.Endpoint ||
  10. newNode.PublicKey != currentNode.PublicKey ||
  11. newNode.Address != currentNode.Address ||
  12. newNode.Address6 != currentNode.Address6 ||
  13. newNode.IsEgressGateway != currentNode.IsEgressGateway ||
  14. newNode.IsIngressGateway != currentNode.IsIngressGateway ||
  15. newNode.IsRelay != currentNode.IsRelay ||
  16. newNode.ListenPort != currentNode.ListenPort ||
  17. newNode.UDPHolePunch != currentNode.UDPHolePunch ||
  18. newNode.MTU != currentNode.MTU ||
  19. newNode.IsPending != currentNode.IsPending ||
  20. newNode.PersistentKeepalive != currentNode.PersistentKeepalive ||
  21. newNode.DNSOn != currentNode.DNSOn ||
  22. newNode.Connected != currentNode.Connected ||
  23. newNode.PostUp != currentNode.PostUp ||
  24. newNode.PostDown != currentNode.PostDown ||
  25. len(newNode.AllowedIPs) != len(currentNode.AllowedIPs) {
  26. return true
  27. }
  28. // multi-comparison statements
  29. if newNode.IsEgressGateway == "yes" {
  30. if len(currentNode.EgressGatewayRanges) != len(newNode.EgressGatewayRanges) {
  31. return true
  32. }
  33. for _, address := range newNode.EgressGatewayRanges {
  34. if !StringSliceContains(currentNode.EgressGatewayRanges, address) {
  35. return true
  36. }
  37. }
  38. }
  39. if newNode.IsRelay == "yes" {
  40. if len(currentNode.RelayAddrs) != len(newNode.RelayAddrs) {
  41. return true
  42. }
  43. for _, address := range newNode.RelayAddrs {
  44. if !StringSliceContains(currentNode.RelayAddrs, address) {
  45. return true
  46. }
  47. }
  48. }
  49. for _, address := range newNode.AllowedIPs {
  50. if !StringSliceContains(currentNode.AllowedIPs, address) {
  51. return true
  52. }
  53. }
  54. return false
  55. }
  56. // StringSliceContains - sees if a string slice contains a string element
  57. func StringSliceContains(slice []string, item string) bool {
  58. for _, s := range slice {
  59. if s == item {
  60. return true
  61. }
  62. }
  63. return false
  64. }
  65. // IPNetSliceContains - sees if a string slice contains a string element
  66. func IPNetSliceContains(slice []net.IPNet, item net.IPNet) bool {
  67. for _, s := range slice {
  68. if s.String() == item.String() {
  69. return true
  70. }
  71. }
  72. return false
  73. }
  74. // IfaceExists - return true if you can find the iface
  75. func IfaceExists(ifacename string) bool {
  76. localnets, err := net.Interfaces()
  77. if err != nil {
  78. return false
  79. }
  80. for _, localnet := range localnets {
  81. if ifacename == localnet.Name {
  82. return true
  83. }
  84. }
  85. return false
  86. }
  87. func IpIsPrivate(ipnet net.IP) bool {
  88. return ipnet.IsPrivate() || ipnet.IsLoopback()
  89. }