network.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package models
  2. import (
  3. "net"
  4. "time"
  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=32,netid_valid"`
  12. NodesLastModified int64 `json:"nodeslastmodified" bson:"nodeslastmodified" swaggertype:"primitive,integer" format:"int64"`
  13. NetworkLastModified int64 `json:"networklastmodified" bson:"networklastmodified" swaggertype:"primitive,integer" format:"int64"`
  14. DefaultInterface string `json:"defaultinterface" bson:"defaultinterface" validate:"min=1,max=35"`
  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. NameServers []string `json:"dns_nameservers"`
  26. AutoJoin string `json:"auto_join"`
  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=32,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() (upsert bool) {
  42. if network.DefaultUDPHolePunch == "" {
  43. network.DefaultUDPHolePunch = "no"
  44. upsert = true
  45. }
  46. if network.DefaultInterface == "" {
  47. if len(network.NetID) < 33 {
  48. network.DefaultInterface = "nm-" + network.NetID
  49. } else {
  50. network.DefaultInterface = network.NetID
  51. }
  52. upsert = true
  53. }
  54. if network.DefaultListenPort == 0 {
  55. network.DefaultListenPort = 51821
  56. upsert = true
  57. }
  58. if network.NodeLimit == 0 {
  59. network.NodeLimit = 999999999
  60. upsert = true
  61. }
  62. if network.DefaultKeepalive == 0 {
  63. network.DefaultKeepalive = 20
  64. upsert = true
  65. }
  66. if network.AllowManualSignUp == "" {
  67. network.AllowManualSignUp = "no"
  68. upsert = true
  69. }
  70. if network.IsIPv4 == "" {
  71. network.IsIPv4 = "yes"
  72. upsert = true
  73. }
  74. if network.IsIPv6 == "" {
  75. network.IsIPv6 = "no"
  76. upsert = true
  77. }
  78. if network.DefaultMTU == 0 {
  79. network.DefaultMTU = 1280
  80. upsert = true
  81. }
  82. if network.DefaultACL == "" {
  83. network.DefaultACL = "yes"
  84. upsert = true
  85. }
  86. return
  87. }
  88. func (network *Network) GetNetworkNetworkCIDR4() *net.IPNet {
  89. if network.AddressRange == "" {
  90. return nil
  91. }
  92. _, netCidr, _ := net.ParseCIDR(network.AddressRange)
  93. return netCidr
  94. }
  95. func (network *Network) GetNetworkNetworkCIDR6() *net.IPNet {
  96. if network.AddressRange6 == "" {
  97. return nil
  98. }
  99. _, netCidr, _ := net.ParseCIDR(network.AddressRange6)
  100. return netCidr
  101. }
  102. type NetworkStatResp struct {
  103. Network
  104. Hosts int `json:"hosts"`
  105. }