iface.go 2.5 KB

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