network.go 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package models
  2. import (
  3. "time"
  4. "github.com/gravitl/netmaker/models/promodels"
  5. )
  6. // Network Struct - contains info for a given unique network
  7. // At some point, need to replace all instances of Name with something else like Identifier
  8. type Network struct {
  9. AddressRange string `json:"addressrange" bson:"addressrange" validate:"omitempty,cidrv4"`
  10. AddressRange6 string `json:"addressrange6" bson:"addressrange6" validate:"omitempty,cidrv6"`
  11. NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=12,netid_valid"`
  12. NodesLastModified int64 `json:"nodeslastmodified" bson:"nodeslastmodified"`
  13. NetworkLastModified int64 `json:"networklastmodified" bson:"networklastmodified"`
  14. DefaultInterface string `json:"defaultinterface" bson:"defaultinterface" validate:"min=1,max=15"`
  15. DefaultListenPort int32 `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,min=1024,max=65535"`
  16. NodeLimit int32 `json:"nodelimit" bson:"nodelimit"`
  17. DefaultPostUp string `json:"defaultpostup" bson:"defaultpostup"`
  18. DefaultPostDown string `json:"defaultpostdown" bson:"defaultpostdown"`
  19. DefaultKeepalive int32 `json:"defaultkeepalive" bson:"defaultkeepalive" validate:"omitempty,max=1000"`
  20. AccessKeys []AccessKey `json:"accesskeys" bson:"accesskeys"`
  21. AllowManualSignUp string `json:"allowmanualsignup" bson:"allowmanualsignup" validate:"checkyesorno"`
  22. IsLocal string `json:"islocal" bson:"islocal" validate:"checkyesorno"`
  23. IsIPv4 string `json:"isipv4" bson:"isipv4" validate:"checkyesorno"`
  24. IsIPv6 string `json:"isipv6" bson:"isipv6" validate:"checkyesorno"`
  25. IsPointToSite string `json:"ispointtosite" bson:"ispointtosite" validate:"checkyesorno"`
  26. DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
  27. DefaultExtClientDNS string `json:"defaultextclientdns" bson:"defaultextclientdns"`
  28. DefaultMTU int32 `json:"defaultmtu" bson:"defaultmtu"`
  29. DefaultACL string `json:"defaultacl" bson:"defaultacl" yaml:"defaultacl" validate:"checkyesorno"`
  30. ProSettings *promodels.ProNetwork `json:"prosettings,omitempty" bson:"prosettings,omitempty" yaml:"prosettings,omitempty"`
  31. }
  32. // SaveData - sensitive fields of a network that should be kept the same
  33. type SaveData struct { // put sensitive fields here
  34. NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=12,netid_valid"`
  35. }
  36. // Network.SetNodesLastModified - sets nodes last modified on network, depricated
  37. func (network *Network) SetNodesLastModified() {
  38. network.NodesLastModified = time.Now().Unix()
  39. }
  40. // Network.SetNetworkLastModified - sets network last modified time
  41. func (network *Network) SetNetworkLastModified() {
  42. network.NetworkLastModified = time.Now().Unix()
  43. }
  44. // Network.SetDefaults - sets default values for a network struct
  45. func (network *Network) SetDefaults() {
  46. if network.DefaultUDPHolePunch == "" {
  47. network.DefaultUDPHolePunch = "no"
  48. }
  49. if network.IsLocal == "" {
  50. network.IsLocal = "no"
  51. }
  52. if network.IsPointToSite == "" {
  53. network.IsPointToSite = "no"
  54. }
  55. if network.DefaultInterface == "" {
  56. if len(network.NetID) < 13 {
  57. network.DefaultInterface = "nm-" + network.NetID
  58. } else {
  59. network.DefaultInterface = network.NetID
  60. }
  61. }
  62. if network.DefaultListenPort == 0 {
  63. network.DefaultListenPort = 51821
  64. }
  65. if network.NodeLimit == 0 {
  66. network.NodeLimit = 999999999
  67. }
  68. if network.DefaultKeepalive == 0 {
  69. network.DefaultKeepalive = 20
  70. }
  71. if network.AllowManualSignUp == "" {
  72. network.AllowManualSignUp = "no"
  73. }
  74. if network.IsIPv4 == "" {
  75. network.IsIPv4 = "yes"
  76. }
  77. if network.IsIPv6 == "" {
  78. network.IsIPv6 = "no"
  79. }
  80. if network.DefaultMTU == 0 {
  81. network.DefaultMTU = 1280
  82. }
  83. if network.DefaultACL == "" {
  84. network.DefaultACL = "yes"
  85. }
  86. }