network.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
  26. DefaultExtClientDNS string `json:"defaultextclientdns" bson:"defaultextclientdns"`
  27. DefaultMTU int32 `json:"defaultmtu" bson:"defaultmtu"`
  28. DefaultACL string `json:"defaultacl" bson:"defaultacl" yaml:"defaultacl" validate:"checkyesorno"`
  29. ProSettings *promodels.ProNetwork `json:"prosettings,omitempty" bson:"prosettings,omitempty" yaml:"prosettings,omitempty"`
  30. }
  31. // SaveData - sensitive fields of a network that should be kept the same
  32. type SaveData struct { // put sensitive fields here
  33. NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=12,netid_valid"`
  34. }
  35. // Network.SetNodesLastModified - sets nodes last modified on network, depricated
  36. func (network *Network) SetNodesLastModified() {
  37. network.NodesLastModified = time.Now().Unix()
  38. }
  39. // Network.SetNetworkLastModified - sets network last modified time
  40. func (network *Network) SetNetworkLastModified() {
  41. network.NetworkLastModified = time.Now().Unix()
  42. }
  43. // Network.SetDefaults - sets default values for a network struct
  44. func (network *Network) SetDefaults() {
  45. if network.DefaultUDPHolePunch == "" {
  46. network.DefaultUDPHolePunch = "no"
  47. }
  48. if network.IsLocal == "" {
  49. network.IsLocal = "no"
  50. }
  51. if network.DefaultInterface == "" {
  52. if len(network.NetID) < 13 {
  53. network.DefaultInterface = "nm-" + network.NetID
  54. } else {
  55. network.DefaultInterface = network.NetID
  56. }
  57. }
  58. if network.DefaultListenPort == 0 {
  59. network.DefaultListenPort = 51821
  60. }
  61. if network.NodeLimit == 0 {
  62. network.NodeLimit = 999999999
  63. }
  64. if network.DefaultKeepalive == 0 {
  65. network.DefaultKeepalive = 20
  66. }
  67. if network.AllowManualSignUp == "" {
  68. network.AllowManualSignUp = "no"
  69. }
  70. if network.IsIPv4 == "" {
  71. network.IsIPv4 = "yes"
  72. }
  73. if network.IsIPv6 == "" {
  74. network.IsIPv6 = "no"
  75. }
  76. if network.DefaultMTU == 0 {
  77. network.DefaultMTU = 1280
  78. }
  79. if network.DefaultACL == "" {
  80. network.DefaultACL = "yes"
  81. }
  82. }