network.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. Name string `json:"name"`
  10. AddressRange string `json:"addressrange" bson:"addressrange" validate:"omitempty,cidrv4"`
  11. AddressRange6 string `json:"addressrange6" bson:"addressrange6" validate:"omitempty,cidrv6"`
  12. NetID string `json:"netid"`
  13. NodesLastModified int64 `json:"nodeslastmodified" bson:"nodeslastmodified" swaggertype:"primitive,integer" format:"int64"`
  14. NetworkLastModified int64 `json:"networklastmodified" bson:"networklastmodified" swaggertype:"primitive,integer" format:"int64"`
  15. DefaultInterface string `json:"defaultinterface" bson:"defaultinterface" validate:"min=1,max=35"`
  16. DefaultListenPort int32 `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,min=1024,max=65535"`
  17. NodeLimit int32 `json:"nodelimit" bson:"nodelimit"`
  18. DefaultPostDown string `json:"defaultpostdown" bson:"defaultpostdown"`
  19. DefaultKeepalive int32 `json:"defaultkeepalive" bson:"defaultkeepalive" validate:"omitempty,max=1000"`
  20. AllowManualSignUp string `json:"allowmanualsignup" bson:"allowmanualsignup" validate:"checkyesorno"`
  21. IsIPv4 string `json:"isipv4" bson:"isipv4" validate:"checkyesorno"`
  22. IsIPv6 string `json:"isipv6" bson:"isipv6" validate:"checkyesorno"`
  23. DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
  24. DefaultMTU int32 `json:"defaultmtu" bson:"defaultmtu"`
  25. DefaultACL string `json:"defaultacl" bson:"defaultacl" yaml:"defaultacl" validate:"checkyesorno"`
  26. CreatedBy string `json:"created_by"`
  27. CreatedAt string `json:"created_at"`
  28. }
  29. // SaveData - sensitive fields of a network that should be kept the same
  30. type SaveData struct { // put sensitive fields here
  31. NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=32,netid_valid"`
  32. }
  33. // Network.SetNodesLastModified - sets nodes last modified on network, depricated
  34. func (network *Network) SetNodesLastModified() {
  35. network.NodesLastModified = time.Now().Unix()
  36. }
  37. // Network.SetNetworkLastModified - sets network last modified time
  38. func (network *Network) SetNetworkLastModified() {
  39. network.NetworkLastModified = time.Now().Unix()
  40. }
  41. // Network.SetDefaults - sets default values for a network struct
  42. func (network *Network) SetDefaults() {
  43. if network.DefaultUDPHolePunch == "" {
  44. network.DefaultUDPHolePunch = "no"
  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. }
  53. if network.DefaultListenPort == 0 {
  54. network.DefaultListenPort = 51821
  55. }
  56. if network.NodeLimit == 0 {
  57. network.NodeLimit = 999999999
  58. }
  59. if network.DefaultKeepalive == 0 {
  60. network.DefaultKeepalive = 20
  61. }
  62. if network.AllowManualSignUp == "" {
  63. network.AllowManualSignUp = "no"
  64. }
  65. if network.IsIPv4 == "" {
  66. network.IsIPv4 = "yes"
  67. }
  68. if network.IsIPv6 == "" {
  69. network.IsIPv6 = "no"
  70. }
  71. if network.DefaultMTU == 0 {
  72. network.DefaultMTU = 1280
  73. }
  74. if network.DefaultACL == "" {
  75. network.DefaultACL = "yes"
  76. }
  77. network.CreatedAt = time.Now().UTC().String()
  78. }
  79. func (network *Network) GetNetworkNetworkCIDR4() *net.IPNet {
  80. if network.AddressRange == "" {
  81. return nil
  82. }
  83. _, netCidr, _ := net.ParseCIDR(network.AddressRange)
  84. return netCidr
  85. }
  86. func (network *Network) GetNetworkNetworkCIDR6() *net.IPNet {
  87. if network.AddressRange6 == "" {
  88. return nil
  89. }
  90. _, netCidr, _ := net.ParseCIDR(network.AddressRange6)
  91. return netCidr
  92. }
  93. type NetworkStatResp struct {
  94. Network
  95. Hosts int `json:"hosts"`
  96. }