network.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. }
  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=32,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() (upsert bool) {
  41. if network.DefaultUDPHolePunch == "" {
  42. network.DefaultUDPHolePunch = "no"
  43. upsert = true
  44. }
  45. if network.DefaultInterface == "" {
  46. if len(network.NetID) < 33 {
  47. network.DefaultInterface = "nm-" + network.NetID
  48. } else {
  49. network.DefaultInterface = network.NetID
  50. }
  51. upsert = true
  52. }
  53. if network.DefaultListenPort == 0 {
  54. network.DefaultListenPort = 51821
  55. upsert = true
  56. }
  57. if network.NodeLimit == 0 {
  58. network.NodeLimit = 999999999
  59. upsert = true
  60. }
  61. if network.DefaultKeepalive == 0 {
  62. network.DefaultKeepalive = 20
  63. upsert = true
  64. }
  65. if network.AllowManualSignUp == "" {
  66. network.AllowManualSignUp = "no"
  67. upsert = true
  68. }
  69. if network.IsIPv4 == "" {
  70. network.IsIPv4 = "yes"
  71. upsert = true
  72. }
  73. if network.IsIPv6 == "" {
  74. network.IsIPv6 = "no"
  75. upsert = true
  76. }
  77. if network.DefaultMTU == 0 {
  78. network.DefaultMTU = 1280
  79. upsert = true
  80. }
  81. if network.DefaultACL == "" {
  82. network.DefaultACL = "yes"
  83. upsert = true
  84. }
  85. return
  86. }
  87. func (network *Network) GetNetworkNetworkCIDR4() *net.IPNet {
  88. if network.AddressRange == "" {
  89. return nil
  90. }
  91. _, netCidr, _ := net.ParseCIDR(network.AddressRange)
  92. return netCidr
  93. }
  94. func (network *Network) GetNetworkNetworkCIDR6() *net.IPNet {
  95. if network.AddressRange6 == "" {
  96. return nil
  97. }
  98. _, netCidr, _ := net.ParseCIDR(network.AddressRange6)
  99. return netCidr
  100. }
  101. type NetworkStatResp struct {
  102. Network
  103. Hosts int `json:"hosts"`
  104. }