network.go 3.6 KB

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