structs.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package models
  2. import (
  3. "strings"
  4. jwt "github.com/golang-jwt/jwt/v4"
  5. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  6. )
  7. const PLACEHOLDER_KEY_TEXT = "ACCESS_KEY"
  8. const PLACEHOLDER_TOKEN_TEXT = "ACCESS_TOKEN"
  9. // CustomExtClient - struct for CustomExtClient params
  10. type CustomExtClient struct {
  11. ClientID string `json:"clientid"`
  12. }
  13. // AuthParams - struct for auth params
  14. type AuthParams struct {
  15. MacAddress string `json:"macaddress"`
  16. ID string `json:"id"`
  17. Password string `json:"password"`
  18. }
  19. // User struct - struct for Users
  20. type User struct {
  21. UserName string `json:"username" bson:"username" validate:"min=3,max=40,in_charset|email"`
  22. Password string `json:"password" bson:"password" validate:"required,min=5"`
  23. Networks []string `json:"networks" bson:"networks"`
  24. IsAdmin bool `json:"isadmin" bson:"isadmin"`
  25. }
  26. // ReturnUser - return user struct
  27. type ReturnUser struct {
  28. UserName string `json:"username" bson:"username"`
  29. Networks []string `json:"networks" bson:"networks"`
  30. IsAdmin bool `json:"isadmin" bson:"isadmin"`
  31. }
  32. // UserAuthParams - user auth params struct
  33. type UserAuthParams struct {
  34. UserName string `json:"username"`
  35. Password string `json:"password"`
  36. }
  37. // UserClaims - user claims struct
  38. type UserClaims struct {
  39. IsAdmin bool
  40. UserName string
  41. Networks []string
  42. jwt.RegisteredClaims
  43. }
  44. // SuccessfulUserLoginResponse - successlogin struct
  45. type SuccessfulUserLoginResponse struct {
  46. UserName string
  47. AuthToken string
  48. }
  49. // Claims is a struct that will be encoded to a JWT.
  50. // jwt.StandardClaims is an embedded type to provide expiry time
  51. type Claims struct {
  52. ID string
  53. MacAddress string
  54. Network string
  55. jwt.RegisteredClaims
  56. }
  57. // SuccessfulLoginResponse is struct to send the request response
  58. type SuccessfulLoginResponse struct {
  59. ID string
  60. AuthToken string
  61. }
  62. // ErrorResponse is struct for error
  63. type ErrorResponse struct {
  64. Code int
  65. Message string
  66. }
  67. // NodeAuth - struct for node auth
  68. type NodeAuth struct {
  69. Network string
  70. Password string
  71. MacAddress string // Depricated
  72. ID string
  73. }
  74. // SuccessResponse is struct for sending error message with code.
  75. type SuccessResponse struct {
  76. Code int
  77. Message string
  78. Response interface{}
  79. }
  80. // AccessKey - access key struct
  81. type AccessKey struct {
  82. Name string `json:"name" bson:"name" validate:"omitempty,max=20"`
  83. Value string `json:"value" bson:"value" validate:"omitempty,alphanum,max=16"`
  84. AccessString string `json:"accessstring" bson:"accessstring"`
  85. Uses int `json:"uses" bson:"uses" validate:"numeric,min=0"`
  86. }
  87. // DisplayKey - what is displayed for key
  88. type DisplayKey struct {
  89. Name string `json:"name" bson:"name"`
  90. Uses int `json:"uses" bson:"uses"`
  91. }
  92. // GlobalConfig - global config
  93. type GlobalConfig struct {
  94. Name string `json:"name" bson:"name"`
  95. }
  96. // CheckInResponse - checkin response
  97. type CheckInResponse struct {
  98. Success bool `json:"success" bson:"success"`
  99. NeedPeerUpdate bool `json:"needpeerupdate" bson:"needpeerupdate"`
  100. NeedConfigUpdate bool `json:"needconfigupdate" bson:"needconfigupdate"`
  101. NeedKeyUpdate bool `json:"needkeyupdate" bson:"needkeyupdate"`
  102. NeedDelete bool `json:"needdelete" bson:"needdelete"`
  103. NodeMessage string `json:"nodemessage" bson:"nodemessage"`
  104. IsPending bool `json:"ispending" bson:"ispending"`
  105. }
  106. // PeersResponse - peers response
  107. type PeersResponse struct {
  108. PublicKey string `json:"publickey" bson:"publickey"`
  109. Endpoint string `json:"endpoint" bson:"endpoint"`
  110. Address string `json:"address" bson:"address"`
  111. Address6 string `json:"address6" bson:"address6"`
  112. LocalAddress string `json:"localaddress" bson:"localaddress"`
  113. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  114. IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway"`
  115. EgressGatewayRanges string `json:"egressgatewayrange" bson:"egressgatewayrange"`
  116. ListenPort int32 `json:"listenport" bson:"listenport"`
  117. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  118. }
  119. // ExtPeersResponse - ext peers response
  120. type ExtPeersResponse struct {
  121. PublicKey string `json:"publickey" bson:"publickey"`
  122. Endpoint string `json:"endpoint" bson:"endpoint"`
  123. Address string `json:"address" bson:"address"`
  124. Address6 string `json:"address6" bson:"address6"`
  125. LocalAddress string `json:"localaddress" bson:"localaddress"`
  126. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  127. ListenPort int32 `json:"listenport" bson:"listenport"`
  128. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  129. }
  130. // EgressGatewayRequest - egress gateway request
  131. type EgressGatewayRequest struct {
  132. NodeID string `json:"nodeid" bson:"nodeid"`
  133. NetID string `json:"netid" bson:"netid"`
  134. NatEnabled string `json:"natenabled" bson:"natenabled"`
  135. Ranges []string `json:"ranges" bson:"ranges"`
  136. Interface string `json:"interface" bson:"interface"`
  137. PostUp string `json:"postup" bson:"postup"`
  138. PostDown string `json:"postdown" bson:"postdown"`
  139. }
  140. // RelayRequest - relay request struct
  141. type RelayRequest struct {
  142. NodeID string `json:"nodeid" bson:"nodeid"`
  143. NetID string `json:"netid" bson:"netid"`
  144. RelayAddrs []string `json:"relayaddrs" bson:"relayaddrs"`
  145. }
  146. // ServerUpdateData - contains data to configure server
  147. // and if it should set peers
  148. type ServerUpdateData struct {
  149. UpdatePeers bool `json:"updatepeers" bson:"updatepeers"`
  150. Node Node `json:"servernode" bson:"servernode"`
  151. }
  152. // Telemetry - contains UUID of the server and timestamp of last send to posthog
  153. // also contains assymetrical encryption pub/priv keys for any server traffic
  154. type Telemetry struct {
  155. UUID string `json:"uuid" bson:"uuid"`
  156. LastSend int64 `json:"lastsend" bson:"lastsend"`
  157. TrafficKeyPriv []byte `json:"traffickeypriv" bson:"traffickeypriv"`
  158. TrafficKeyPub []byte `json:"traffickeypub" bson:"traffickeypub"`
  159. }
  160. // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not
  161. type ServerAddr struct {
  162. IsLeader bool `json:"isleader" bson:"isleader" yaml:"isleader"`
  163. Address string `json:"address" bson:"address" yaml:"address"`
  164. }
  165. // TrafficKeys - struct to hold public keys
  166. type TrafficKeys struct {
  167. Mine []byte `json:"mine" bson:"mine" yaml:"mine"`
  168. Server []byte `json:"server" bson:"server" yaml:"server"`
  169. }
  170. // NodeGet - struct for a single node get response
  171. type NodeGet struct {
  172. Node Node `json:"node" bson:"node" yaml:"node"`
  173. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  174. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  175. }
  176. // ServerConfig - struct for dealing with the server information for a netclient
  177. type ServerConfig struct {
  178. CoreDNSAddr string `yaml:"corednsaddr"`
  179. API string `yaml:"api"`
  180. APIPort string `yaml:"apiport"`
  181. ClientMode string `yaml:"clientmode"`
  182. DNSMode string `yaml:"dnsmode"`
  183. Version string `yaml:"version"`
  184. MQPort string `yaml:"mqport"`
  185. Server string `yaml:"server"`
  186. }
  187. // User.NameInCharset - returns if name is in charset below or not
  188. func (user *User) NameInCharSet() bool {
  189. charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
  190. for _, char := range user.UserName {
  191. if !strings.Contains(charset, strings.ToLower(string(char))) {
  192. return false
  193. }
  194. }
  195. return true
  196. }
  197. // ServerIDs - struct to hold server ids.
  198. type ServerIDs struct {
  199. ServerIDs []string `json:"server_ids"`
  200. }