structs.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. }
  143. // RelayRequest - relay request struct
  144. type RelayRequest struct {
  145. NodeID string `json:"nodeid" bson:"nodeid"`
  146. NetID string `json:"netid" bson:"netid"`
  147. RelayAddrs []string `json:"relayaddrs" bson:"relayaddrs"`
  148. }
  149. // HostRelayRequest - struct for host relay creation
  150. type HostRelayRequest struct {
  151. HostID string `json:"host_id"`
  152. RelayedHosts []string `json:"relayed_hosts"`
  153. }
  154. // ServerUpdateData - contains data to configure server
  155. // and if it should set peers
  156. type ServerUpdateData struct {
  157. UpdatePeers bool `json:"updatepeers" bson:"updatepeers"`
  158. Node LegacyNode `json:"servernode" bson:"servernode"`
  159. }
  160. // Telemetry - contains UUID of the server and timestamp of last send to posthog
  161. // also contains assymetrical encryption pub/priv keys for any server traffic
  162. type Telemetry struct {
  163. UUID string `json:"uuid" bson:"uuid"`
  164. LastSend int64 `json:"lastsend" bson:"lastsend"`
  165. TrafficKeyPriv []byte `json:"traffickeypriv" bson:"traffickeypriv"`
  166. TrafficKeyPub []byte `json:"traffickeypub" bson:"traffickeypub"`
  167. }
  168. // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not
  169. type ServerAddr struct {
  170. IsLeader bool `json:"isleader" bson:"isleader" yaml:"isleader"`
  171. Address string `json:"address" bson:"address" yaml:"address"`
  172. }
  173. // TrafficKeys - struct to hold public keys
  174. type TrafficKeys struct {
  175. Mine []byte `json:"mine" bson:"mine" yaml:"mine"`
  176. Server []byte `json:"server" bson:"server" yaml:"server"`
  177. }
  178. // NodeGet - struct for a single node get response
  179. type NodeGet struct {
  180. Node LegacyNode `json:"node" bson:"node" yaml:"node"`
  181. Host Host `json:"host" yaml:"host"`
  182. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  183. HostPeers []wgtypes.PeerConfig `json:"host_peers" bson:"host_peers" yaml:"host_peers"`
  184. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  185. PeerIDs PeerMap `json:"peerids,omitempty" bson:"peerids,omitempty" yaml:"peerids,omitempty"`
  186. }
  187. // NodeJoinResponse data returned to node in response to join
  188. type NodeJoinResponse struct {
  189. Node Node `json:"node" bson:"node" yaml:"node"`
  190. Host Host `json:"host" yaml:"host"`
  191. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  192. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  193. }
  194. // ServerConfig - struct for dealing with the server information for a netclient
  195. type ServerConfig struct {
  196. CoreDNSAddr string `yaml:"corednsaddr"`
  197. API string `yaml:"api"`
  198. APIPort string `yaml:"apiport"`
  199. DNSMode string `yaml:"dnsmode"`
  200. Version string `yaml:"version"`
  201. MQPort string `yaml:"mqport"`
  202. Server string `yaml:"server"`
  203. Broker string `yaml:"broker"`
  204. Is_EE bool `yaml:"isee"`
  205. StunPort int `yaml:"stun_port"`
  206. StunHost string `yaml:"stun_host"`
  207. TrafficKey []byte `yaml:"traffickey"`
  208. }
  209. // User.NameInCharset - returns if name is in charset below or not
  210. func (user *User) NameInCharSet() bool {
  211. charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
  212. for _, char := range user.UserName {
  213. if !strings.Contains(charset, strings.ToLower(string(char))) {
  214. return false
  215. }
  216. }
  217. return true
  218. }
  219. // ServerIDs - struct to hold server ids.
  220. type ServerIDs struct {
  221. ServerIDs []string `json:"server_ids"`
  222. }
  223. // JoinData - struct to hold data required for node to join a network on server
  224. type JoinData struct {
  225. Host Host `json:"host" yaml:"host"`
  226. Node Node `json:"node" yaml:"node"`
  227. Key string `json:"key" yaml:"key"`
  228. }