structs.go 7.8 KB

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