iface.go 2.5 KB

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