network.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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:"omitempty,cidrv4"`
  9. AddressRange6 string `json:"addressrange6" bson:"addressrange6" validate:"omitempty,cidrv6"`
  10. NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=32,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=35"`
  14. DefaultListenPort int32 `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,min=1024,max=65535"`
  15. NodeLimit int32 `json:"nodelimit" bson:"nodelimit"`
  16. DefaultPostDown string `json:"defaultpostdown" bson:"defaultpostdown"`
  17. DefaultKeepalive int32 `json:"defaultkeepalive" bson:"defaultkeepalive" validate:"omitempty,max=1000"`
  18. AllowManualSignUp string `json:"allowmanualsignup" bson:"allowmanualsignup" validate:"checkyesorno"`
  19. IsIPv4 string `json:"isipv4" bson:"isipv4" validate:"checkyesorno"`
  20. IsIPv6 string `json:"isipv6" bson:"isipv6" validate:"checkyesorno"`
  21. DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
  22. DefaultMTU int32 `json:"defaultmtu" bson:"defaultmtu"`
  23. DefaultACL string `json:"defaultacl" bson:"defaultacl" yaml:"defaultacl" validate:"checkyesorno"`
  24. }
  25. // SaveData - sensitive fields of a network that should be kept the same
  26. type SaveData struct { // put sensitive fields here
  27. NetID string `json:"netid" bson:"netid" validate:"required,min=1,max=32,netid_valid"`
  28. }
  29. // Network.SetNodesLastModified - sets nodes last modified on network, depricated
  30. func (network *Network) SetNodesLastModified() {
  31. network.NodesLastModified = time.Now().Unix()
  32. }
  33. // Network.SetNetworkLastModified - sets network last modified time
  34. func (network *Network) SetNetworkLastModified() {
  35. network.NetworkLastModified = time.Now().Unix()
  36. }
  37. // Network.SetDefaults - sets default values for a network struct
  38. func (network *Network) SetDefaults() {
  39. if network.DefaultUDPHolePunch == "" {
  40. network.DefaultUDPHolePunch = "no"
  41. }
  42. if network.DefaultInterface == "" {
  43. if len(network.NetID) < 33 {
  44. network.DefaultInterface = "nm-" + network.NetID
  45. } else {
  46. network.DefaultInterface = network.NetID
  47. }
  48. }
  49. if network.DefaultListenPort == 0 {
  50. network.DefaultListenPort = 51821
  51. }
  52. if network.NodeLimit == 0 {
  53. network.NodeLimit = 999999999
  54. }
  55. if network.DefaultKeepalive == 0 {
  56. network.DefaultKeepalive = 20
  57. }
  58. if network.AllowManualSignUp == "" {
  59. network.AllowManualSignUp = "no"
  60. }
  61. if network.IsIPv4 == "" {
  62. network.IsIPv4 = "yes"
  63. }
  64. if network.IsIPv6 == "" {
  65. network.IsIPv6 = "no"
  66. }
  67. if network.DefaultMTU == 0 {
  68. network.DefaultMTU = 1280
  69. }
  70. if network.DefaultACL == "" {
  71. network.DefaultACL = "yes"
  72. }
  73. }