network.go 3.7 KB

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