network.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package models
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/gravitl/netmaker/servercfg"
  6. )
  7. // Network Struct - contains info for a given unique network
  8. //At some point, need to replace all instances of Name with something else like Identifier
  9. type Network struct {
  10. AddressRange string `json:"addressrange" bson:"addressrange" validate:"required,cidr"`
  11. AddressRange6 string `json:"addressrange6" bson:"addressrange6" validate:"regexp=^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$"`
  12. DisplayName string `json:"displayname,omitempty" bson:"displayname,omitempty" validate:"omitempty,min=1,max=20,displayname_valid"`
  13. NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=12,netid_valid"`
  14. NodesLastModified int64 `json:"nodeslastmodified" bson:"nodeslastmodified"`
  15. NetworkLastModified int64 `json:"networklastmodified" bson:"networklastmodified"`
  16. DefaultInterface string `json:"defaultinterface" bson:"defaultinterface" validate:"min=1,max=15"`
  17. DefaultListenPort int32 `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,min=1024,max=65535"`
  18. NodeLimit int32 `json:"nodelimit" bson:"nodelimit"`
  19. DefaultPostUp string `json:"defaultpostup" bson:"defaultpostup"`
  20. DefaultPostDown string `json:"defaultpostdown" bson:"defaultpostdown"`
  21. KeyUpdateTimeStamp int64 `json:"keyupdatetimestamp" bson:"keyupdatetimestamp"`
  22. DefaultKeepalive int32 `json:"defaultkeepalive" bson:"defaultkeepalive" validate:"omitempty,max=1000"`
  23. DefaultSaveConfig string `json:"defaultsaveconfig" bson:"defaultsaveconfig" validate:"checkyesorno"`
  24. AccessKeys []AccessKey `json:"accesskeys" bson:"accesskeys"`
  25. AllowManualSignUp string `json:"allowmanualsignup" bson:"allowmanualsignup" validate:"checkyesorno"`
  26. IsLocal string `json:"islocal" bson:"islocal" validate:"checkyesorno"`
  27. IsDualStack string `json:"isdualstack" bson:"isdualstack" validate:"checkyesorno"`
  28. IsIPv4 string `json:"isipv4" bson:"isipv4" validate:"checkyesorno"`
  29. IsIPv6 string `json:"isipv6" bson:"isipv6" validate:"checkyesorno"`
  30. IsGRPCHub string `json:"isgrpchub" bson:"isgrpchub" validate:"checkyesorno"`
  31. LocalRange string `json:"localrange" bson:"localrange" validate:"omitempty,cidr"`
  32. // checkin interval is depreciated at the network level. Set on server with CHECKIN_INTERVAL
  33. DefaultCheckInInterval int32 `json:"checkininterval,omitempty" bson:"checkininterval,omitempty" validate:"omitempty,numeric,min=2,max=100000"`
  34. DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
  35. DefaultExtClientDNS string `json:"defaultextclientdns" bson:"defaultextclientdns"`
  36. DefaultMTU int32 `json:"defaultmtu" bson:"defaultmtu"`
  37. }
  38. // SaveData - sensitive fields of a network that should be kept the same
  39. type SaveData struct { // put sensitive fields here
  40. NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=12,netid_valid"`
  41. }
  42. // Network.DisplayNameInNetworkCharSet - checks if displayname uses valid characters
  43. func (network *Network) DisplayNameInNetworkCharSet() bool {
  44. charset := "abcdefghijklmnopqrstuvwxyz1234567890-_./;% ^#()!@$*"
  45. for _, char := range network.DisplayName {
  46. if !strings.Contains(charset, strings.ToLower(string(char))) {
  47. return false
  48. }
  49. }
  50. return true
  51. }
  52. // Network.SetNodesLastModified - sets nodes last modified on network, depricated
  53. func (network *Network) SetNodesLastModified() {
  54. network.NodesLastModified = time.Now().Unix()
  55. }
  56. // Network.SetNetworkLastModified - sets network last modified time
  57. func (network *Network) SetNetworkLastModified() {
  58. network.NetworkLastModified = time.Now().Unix()
  59. }
  60. // Network.SetDefaults - sets default values for a network struct
  61. func (network *Network) SetDefaults() {
  62. if network.DefaultUDPHolePunch == "" {
  63. if servercfg.IsClientMode() != "off" {
  64. network.DefaultUDPHolePunch = "yes"
  65. } else {
  66. network.DefaultUDPHolePunch = "no"
  67. }
  68. }
  69. if network.IsLocal == "" {
  70. network.IsLocal = "no"
  71. }
  72. if network.IsGRPCHub == "" {
  73. network.IsGRPCHub = "no"
  74. }
  75. if network.DisplayName == "" {
  76. network.DisplayName = network.NetID
  77. }
  78. if network.DefaultInterface == "" {
  79. if len(network.NetID) < 13 {
  80. network.DefaultInterface = "nm-" + network.NetID
  81. } else {
  82. network.DefaultInterface = network.NetID
  83. }
  84. }
  85. if network.DefaultListenPort == 0 {
  86. network.DefaultListenPort = 51821
  87. }
  88. if network.NodeLimit == 0 {
  89. network.NodeLimit = 999999999
  90. }
  91. if network.DefaultSaveConfig == "" {
  92. network.DefaultSaveConfig = "no"
  93. }
  94. if network.DefaultKeepalive == 0 {
  95. network.DefaultKeepalive = 20
  96. }
  97. //Check-In Interval for Nodes, In Seconds
  98. if network.DefaultCheckInInterval == 0 {
  99. network.DefaultCheckInInterval = 30
  100. }
  101. if network.AllowManualSignUp == "" {
  102. network.AllowManualSignUp = "no"
  103. }
  104. if network.IsDualStack == "" {
  105. network.IsDualStack = "no"
  106. }
  107. if network.IsDualStack == "yes" {
  108. network.IsIPv6 = "yes"
  109. network.IsIPv4 = "yes"
  110. } else {
  111. network.IsIPv6 = "no"
  112. network.IsIPv4 = "yes"
  113. }
  114. if network.DefaultMTU == 0 {
  115. network.DefaultMTU = 1280
  116. }
  117. }