group.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package models
  2. import (
  3. // "../mongoconn"
  4. "go.mongodb.org/mongo-driver/bson/primitive"
  5. "time"
  6. )
  7. //Group Struct
  8. //At some point, need to replace all instances of Name with something else like Identifier
  9. type Group struct {
  10. ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
  11. AddressRange string `json:"addressrange" bson:"addressrange" validate:"required,addressrange_valid"`
  12. DisplayName string `json:"displayname,omitempty" bson:"displayname,omitempty" validate:"omitempty,displayname_unique,min=1,max=100"`
  13. NameID string `json:"nameid" bson:"nameid" validate:"required,nameid_valid,min=1,max=12"`
  14. NodesLastModified int64 `json:"nodeslastmodified" bson:"nodeslastmodified"`
  15. GroupLastModified int64 `json:"grouplastmodified" bson:"grouplastmodified"`
  16. DefaultInterface string `json:"defaulinterface" bson:"defaultinterface"`
  17. DefaultListenPort int32 `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,numeric,min=1024,max=65535"`
  18. DefaultPostUp string `json:"defaultpostup" bson:"defaultpostup"`
  19. DefaultPreUp string `json:"defaultpreup" bson:"defaultpreup"`
  20. DefaultKeepalive int32 `json:"defaultkeepalive" bson:"defaultkeepalive" validate: "omitempty,numeric,max=1000"`
  21. DefaultSaveConfig *bool `json:"defaultsaveconfig" bson:"defaultsaveconfig"`
  22. AccessKeys []AccessKey `json:"accesskeys" bson:"accesskeys"`
  23. AllowManualSignUp *bool `json:"allowmanualsignup" bson:"allowmanualsignup"`
  24. DefaultCheckInInterval int32 `json:"checkininterval,omitempty" bson:"checkininterval,omitempty" validate:"omitempty,numeric,min=1,max=100000"`
  25. }
  26. //TODO:
  27. //Not sure if we need the below two functions. Got rid of one of the calls. May want to revisit
  28. func(group *Group) SetNodesLastModified(){
  29. group.NodesLastModified = time.Now().Unix()
  30. }
  31. func(group *Group) SetGroupLastModified(){
  32. group.GroupLastModified = time.Now().Unix()
  33. }
  34. func(group *Group) SetDefaults(){
  35. if group.DisplayName == "" {
  36. group.DisplayName = group.NameID
  37. }
  38. if group.DefaultInterface == "" {
  39. group.DefaultInterface = "wc-" + group.NameID
  40. }
  41. if group.DefaultListenPort == 0 {
  42. group.DefaultListenPort = 5555
  43. }
  44. if group.DefaultPreUp == "" {
  45. }
  46. if group.DefaultSaveConfig == nil {
  47. defaultsave := true
  48. group.DefaultSaveConfig = &defaultsave
  49. }
  50. if group.DefaultKeepalive == 0 {
  51. group.DefaultKeepalive = 20
  52. }
  53. if group.DefaultPostUp == "" {
  54. postup := "sudo wg addconf " + group.DefaultInterface + " /etc/wireguard/peers.conf"
  55. group.DefaultPostUp = postup
  56. }
  57. //Check-In Interval for Nodes, In Seconds
  58. if group.DefaultCheckInInterval == 0 {
  59. group.DefaultCheckInInterval = 120
  60. }
  61. if group.AllowManualSignUp == nil {
  62. signup := false
  63. group.AllowManualSignUp = &signup
  64. }
  65. }