network.go 3.5 KB

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