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