network.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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"`
  13. NetworkLastModified int64 `json:"networklastmodified" bson:"networklastmodified"`
  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. }
  26. // SaveData - sensitive fields of a network that should be kept the same
  27. type SaveData struct { // put sensitive fields here
  28. NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=32,netid_valid"`
  29. }
  30. // Network.SetNodesLastModified - sets nodes last modified on network, depricated
  31. func (network *Network) SetNodesLastModified() {
  32. network.NodesLastModified = time.Now().Unix()
  33. }
  34. // Network.SetNetworkLastModified - sets network last modified time
  35. func (network *Network) SetNetworkLastModified() {
  36. network.NetworkLastModified = time.Now().Unix()
  37. }
  38. // Network.SetDefaults - sets default values for a network struct
  39. func (network *Network) SetDefaults() {
  40. if network.DefaultUDPHolePunch == "" {
  41. network.DefaultUDPHolePunch = "no"
  42. }
  43. if network.DefaultInterface == "" {
  44. if len(network.NetID) < 33 {
  45. network.DefaultInterface = "nm-" + network.NetID
  46. } else {
  47. network.DefaultInterface = network.NetID
  48. }
  49. }
  50. if network.DefaultListenPort == 0 {
  51. network.DefaultListenPort = 51821
  52. }
  53. if network.NodeLimit == 0 {
  54. network.NodeLimit = 999999999
  55. }
  56. if network.DefaultKeepalive == 0 {
  57. network.DefaultKeepalive = 20
  58. }
  59. if network.AllowManualSignUp == "" {
  60. network.AllowManualSignUp = "no"
  61. }
  62. if network.IsIPv4 == "" {
  63. network.IsIPv4 = "yes"
  64. }
  65. if network.IsIPv6 == "" {
  66. network.IsIPv6 = "no"
  67. }
  68. if network.DefaultMTU == 0 {
  69. network.DefaultMTU = 1280
  70. }
  71. if network.DefaultACL == "" {
  72. network.DefaultACL = "yes"
  73. }
  74. }
  75. func (network *Network) GetNetworkNetworkCIDR4() *net.IPNet {
  76. if network.AddressRange == "" {
  77. return nil
  78. }
  79. _, netCidr, _ := net.ParseCIDR(network.AddressRange)
  80. return netCidr
  81. }
  82. func (network *Network) GetNetworkNetworkCIDR6() *net.IPNet {
  83. if network.AddressRange6 == "" {
  84. return nil
  85. }
  86. _, netCidr, _ := net.ParseCIDR(network.AddressRange6)
  87. return netCidr
  88. }