network.go 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package models
  2. import (
  3. "time"
  4. )
  5. // Network Struct - contains info for a given unique network
  6. //At some point, need to replace all instances of Name with something else like Identifier
  7. type Network struct {
  8. AddressRange string `json:"addressrange" bson:"addressrange" validate:"omitempty,cidrv4"`
  9. AddressRange6 string `json:"addressrange6" bson:"addressrange6" validate:"omitempty,cidrv6"`
  10. NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=12,netid_valid"`
  11. NodesLastModified int64 `json:"nodeslastmodified" bson:"nodeslastmodified"`
  12. NetworkLastModified int64 `json:"networklastmodified" bson:"networklastmodified"`
  13. DefaultInterface string `json:"defaultinterface" bson:"defaultinterface" validate:"min=1,max=15"`
  14. DefaultListenPort int32 `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,min=1024,max=65535"`
  15. NodeLimit int32 `json:"nodelimit" bson:"nodelimit"`
  16. DefaultPostUp string `json:"defaultpostup" bson:"defaultpostup"`
  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. IsPointToSite string `json:"ispointtosite" bson:"ispointtosite" validate:"checkyesorno"`
  25. LocalRange string `json:"localrange" bson:"localrange" validate:"omitempty,cidr"`
  26. DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
  27. DefaultExtClientDNS string `json:"defaultextclientdns" bson:"defaultextclientdns"`
  28. DefaultMTU int32 `json:"defaultmtu" bson:"defaultmtu"`
  29. DefaultACL string `json:"defaultacl" bson:"defaultacl" yaml:"defaultacl" validate:"checkyesorno"`
  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.IsPointToSite == "" {
  52. network.IsPointToSite = "no"
  53. }
  54. if network.DefaultInterface == "" {
  55. if len(network.NetID) < 13 {
  56. network.DefaultInterface = "nm-" + network.NetID
  57. } else {
  58. network.DefaultInterface = network.NetID
  59. }
  60. }
  61. if network.DefaultListenPort == 0 {
  62. network.DefaultListenPort = 51821
  63. }
  64. if network.NodeLimit == 0 {
  65. network.NodeLimit = 999999999
  66. }
  67. if network.DefaultKeepalive == 0 {
  68. network.DefaultKeepalive = 20
  69. }
  70. if network.AllowManualSignUp == "" {
  71. network.AllowManualSignUp = "no"
  72. }
  73. if network.IsIPv4 == "" {
  74. network.IsIPv4 = "yes"
  75. }
  76. if network.IsIPv6 == "" {
  77. network.IsIPv6 = "no"
  78. }
  79. if network.DefaultMTU == 0 {
  80. network.DefaultMTU = 1280
  81. }
  82. if network.DefaultACL == "" {
  83. network.DefaultACL = "yes"
  84. }
  85. }