network.go 3.8 KB

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