iface.go 2.4 KB

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