iface.go 2.5 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.Node, newNode *models.Node) bool {
  8. // single comparison statements
  9. if newNode.Endpoint != currentNode.Endpoint ||
  10. newNode.LocalAddress != currentNode.LocalAddress ||
  11. newNode.PublicKey != currentNode.PublicKey ||
  12. newNode.Address != currentNode.Address ||
  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. len(newNode.ExcludedAddrs) != len(currentNode.ExcludedAddrs) ||
  23. len(newNode.AllowedIPs) != len(currentNode.AllowedIPs) {
  24. return true
  25. }
  26. // multi-comparison statements
  27. if newNode.IsDualStack == "yes" {
  28. if newNode.Address6 != currentNode.Address6 {
  29. return true
  30. }
  31. }
  32. if newNode.IsEgressGateway == "yes" {
  33. if len(currentNode.EgressGatewayRanges) != len(newNode.EgressGatewayRanges) {
  34. return true
  35. }
  36. for _, address := range newNode.EgressGatewayRanges {
  37. if !StringSliceContains(currentNode.EgressGatewayRanges, address) {
  38. return true
  39. }
  40. }
  41. }
  42. if newNode.IsRelay == "yes" {
  43. if len(currentNode.RelayAddrs) != len(newNode.RelayAddrs) {
  44. return true
  45. }
  46. for _, address := range newNode.RelayAddrs {
  47. if !StringSliceContains(currentNode.RelayAddrs, address) {
  48. return true
  49. }
  50. }
  51. }
  52. for _, address := range newNode.AllowedIPs {
  53. if !StringSliceContains(currentNode.AllowedIPs, address) {
  54. return true
  55. }
  56. }
  57. return false
  58. }
  59. // StringSliceContains - sees if a string slice contains a string element
  60. func StringSliceContains(slice []string, item string) bool {
  61. for _, s := range slice {
  62. if s == item {
  63. return true
  64. }
  65. }
  66. return false
  67. }
  68. // IPNetSliceContains - sees if a string slice contains a string element
  69. func IPNetSliceContains(slice []net.IPNet, item net.IPNet) bool {
  70. for _, s := range slice {
  71. if s.String() == item.String() {
  72. return true
  73. }
  74. }
  75. return false
  76. }
  77. // IfaceExists - return true if you can find the iface
  78. func IfaceExists(ifacename string) bool {
  79. localnets, err := net.Interfaces()
  80. if err != nil {
  81. return false
  82. }
  83. for _, localnet := range localnets {
  84. if ifacename == localnet.Name {
  85. return true
  86. }
  87. }
  88. return false
  89. }