network.go 5.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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:"required,cidr"`
  9. AddressRange6 string `json:"addressrange6" bson:"addressrange6" validate:"regexp=^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$"`
  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. IsDualStack string `json:"isdualstack" bson:"isdualstack" validate:"checkyesorno"`
  23. IsIPv4 string `json:"isipv4" bson:"isipv4" validate:"checkyesorno"`
  24. IsIPv6 string `json:"isipv6" bson:"isipv6" validate:"checkyesorno"`
  25. IsPointToSite string `json:"ispointtosite" bson:"ispointtosite" validate:"checkyesorno"`
  26. LocalRange string `json:"localrange" bson:"localrange" validate:"omitempty,cidr"`
  27. DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
  28. DefaultExtClientDNS string `json:"defaultextclientdns" bson:"defaultextclientdns"`
  29. DefaultMTU int32 `json:"defaultmtu" bson:"defaultmtu"`
  30. DefaultACL string `json:"defaultacl" bson:"defaultacl" yaml:"defaultacl" validate:"checkyesorno"`
  31. }
  32. // SaveData - sensitive fields of a network that should be kept the same
  33. type SaveData struct { // put sensitive fields here
  34. NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=12,netid_valid"`
  35. }
  36. // Network.SetNodesLastModified - sets nodes last modified on network, depricated
  37. func (network *Network) SetNodesLastModified() {
  38. network.NodesLastModified = time.Now().Unix()
  39. }
  40. // Network.SetNetworkLastModified - sets network last modified time
  41. func (network *Network) SetNetworkLastModified() {
  42. network.NetworkLastModified = time.Now().Unix()
  43. }
  44. // Network.SetDefaults - sets default values for a network struct
  45. func (network *Network) SetDefaults() {
  46. if network.DefaultUDPHolePunch == "" {
  47. network.DefaultUDPHolePunch = "no"
  48. }
  49. if network.IsLocal == "" {
  50. network.IsLocal = "no"
  51. }
  52. if network.IsPointToSite == "" {
  53. network.IsPointToSite = "no"
  54. }
  55. if network.DefaultInterface == "" {
  56. if len(network.NetID) < 13 {
  57. network.DefaultInterface = "nm-" + network.NetID
  58. } else {
  59. network.DefaultInterface = network.NetID
  60. }
  61. }
  62. if network.DefaultListenPort == 0 {
  63. network.DefaultListenPort = 51821
  64. }
  65. if network.NodeLimit == 0 {
  66. network.NodeLimit = 999999999
  67. }
  68. if network.DefaultKeepalive == 0 {
  69. network.DefaultKeepalive = 20
  70. }
  71. if network.AllowManualSignUp == "" {
  72. network.AllowManualSignUp = "no"
  73. }
  74. if network.IsDualStack == "" {
  75. network.IsDualStack = "no"
  76. }
  77. if network.IsDualStack == "yes" {
  78. network.IsIPv6 = "yes"
  79. network.IsIPv4 = "yes"
  80. } else {
  81. network.IsIPv6 = "no"
  82. network.IsIPv4 = "yes"
  83. }
  84. if network.DefaultMTU == 0 {
  85. network.DefaultMTU = 1280
  86. }
  87. if network.DefaultACL == "" {
  88. network.DefaultACL = "yes"
  89. }
  90. }