structs.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 (
  9. // PLACEHOLDER_KEY_TEXT - access key placeholder text if option turned off
  10. PLACEHOLDER_KEY_TEXT = "ACCESS_KEY"
  11. // PLACEHOLDER_TOKEN_TEXT - access key token placeholder text if option turned off
  12. PLACEHOLDER_TOKEN_TEXT = "ACCESS_TOKEN"
  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. IsAdmin bool `json:"isadmin" bson:"isadmin"`
  25. IsSuperAdmin bool `json:"issuperadmin"`
  26. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"`
  27. LastLoginTime time.Time `json:"last_login_time"`
  28. }
  29. // ReturnUser - return user struct
  30. type ReturnUser struct {
  31. UserName string `json:"username"`
  32. IsAdmin bool `json:"isadmin"`
  33. IsSuperAdmin bool `json:"issuperadmin"`
  34. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"`
  35. LastLoginTime time.Time `json:"last_login_time"`
  36. }
  37. // UserAuthParams - user auth params struct
  38. type UserAuthParams struct {
  39. UserName string `json:"username"`
  40. Password string `json:"password"`
  41. }
  42. // UserClaims - user claims struct
  43. type UserClaims struct {
  44. IsAdmin bool
  45. IsSuperAdmin bool
  46. UserName string
  47. jwt.RegisteredClaims
  48. }
  49. // IngressGwUsers - struct to hold users on a ingress gw
  50. type IngressGwUsers struct {
  51. NodeID string `json:"node_id"`
  52. Network string `json:"network"`
  53. Users []ReturnUser `json:"users"`
  54. }
  55. // UserRemoteGws - struct to hold user's remote gws
  56. type UserRemoteGws struct {
  57. GwID string `json:"remote_access_gw_id"`
  58. GWName string `json:"gw_name"`
  59. Network string `json:"network"`
  60. Connected bool `json:"connected"`
  61. IsInternetGateway bool `json:"is_internet_gateway"`
  62. GwClient ExtClient `json:"gw_client"`
  63. GwPeerPublicKey string `json:"gw_peer_public_key"`
  64. }
  65. // UserRemoteGwsReq - struct to hold user remote acccess gws req
  66. type UserRemoteGwsReq struct {
  67. RemoteAccessClientID string `json:"remote_access_clientid"`
  68. }
  69. // SuccessfulUserLoginResponse - successlogin struct
  70. type SuccessfulUserLoginResponse struct {
  71. UserName string
  72. AuthToken string
  73. }
  74. // Claims is a struct that will be encoded to a JWT.
  75. // jwt.StandardClaims is an embedded type to provide expiry time
  76. type Claims struct {
  77. ID string
  78. MacAddress string
  79. Network string
  80. jwt.RegisteredClaims
  81. }
  82. // SuccessfulLoginResponse is struct to send the request response
  83. type SuccessfulLoginResponse struct {
  84. ID string
  85. AuthToken string
  86. }
  87. // ErrorResponse is struct for error
  88. type ErrorResponse struct {
  89. Code int
  90. Message string
  91. }
  92. // NodeAuth - struct for node auth
  93. type NodeAuth struct {
  94. Network string
  95. Password string
  96. MacAddress string // Depricated
  97. ID string
  98. }
  99. // SuccessResponse is struct for sending error message with code.
  100. type SuccessResponse struct {
  101. Code int
  102. Message string
  103. Response interface{}
  104. }
  105. // DisplayKey - what is displayed for key
  106. type DisplayKey struct {
  107. Name string `json:"name" bson:"name"`
  108. Uses int `json:"uses" bson:"uses"`
  109. }
  110. // GlobalConfig - global config
  111. type GlobalConfig struct {
  112. Name string `json:"name" bson:"name"`
  113. }
  114. // CheckInResponse - checkin response
  115. type CheckInResponse struct {
  116. Success bool `json:"success" bson:"success"`
  117. NeedPeerUpdate bool `json:"needpeerupdate" bson:"needpeerupdate"`
  118. NeedConfigUpdate bool `json:"needconfigupdate" bson:"needconfigupdate"`
  119. NeedKeyUpdate bool `json:"needkeyupdate" bson:"needkeyupdate"`
  120. NeedDelete bool `json:"needdelete" bson:"needdelete"`
  121. NodeMessage string `json:"nodemessage" bson:"nodemessage"`
  122. IsPending bool `json:"ispending" bson:"ispending"`
  123. }
  124. // PeersResponse - peers response
  125. type PeersResponse 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. IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway"`
  133. EgressGatewayRanges string `json:"egressgatewayrange" bson:"egressgatewayrange"`
  134. ListenPort int32 `json:"listenport" bson:"listenport"`
  135. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  136. }
  137. // ExtPeersResponse - ext peers response
  138. type ExtPeersResponse struct {
  139. PublicKey string `json:"publickey" bson:"publickey"`
  140. Endpoint string `json:"endpoint" bson:"endpoint"`
  141. Address string `json:"address" bson:"address"`
  142. Address6 string `json:"address6" bson:"address6"`
  143. LocalAddress string `json:"localaddress" bson:"localaddress"`
  144. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  145. ListenPort int32 `json:"listenport" bson:"listenport"`
  146. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  147. }
  148. // EgressGatewayRequest - egress gateway request
  149. type EgressGatewayRequest struct {
  150. NodeID string `json:"nodeid" bson:"nodeid"`
  151. NetID string `json:"netid" bson:"netid"`
  152. NatEnabled string `json:"natenabled" bson:"natenabled"`
  153. Ranges []string `json:"ranges" bson:"ranges"`
  154. }
  155. // RelayRequest - relay request struct
  156. type RelayRequest struct {
  157. NodeID string `json:"nodeid"`
  158. NetID string `json:"netid"`
  159. RelayedNodes []string `json:"relayaddrs"`
  160. }
  161. // HostRelayRequest - struct for host relay creation
  162. type HostRelayRequest struct {
  163. HostID string `json:"host_id"`
  164. RelayedHosts []string `json:"relayed_hosts"`
  165. }
  166. // IngressRequest - ingress request struct
  167. type IngressRequest struct {
  168. ExtclientDNS string `json:"extclientdns"`
  169. IsInternetGateway bool `json:"is_internet_gw"`
  170. }
  171. // ServerUpdateData - contains data to configure server
  172. // and if it should set peers
  173. type ServerUpdateData struct {
  174. UpdatePeers bool `json:"updatepeers" bson:"updatepeers"`
  175. Node LegacyNode `json:"servernode" bson:"servernode"`
  176. }
  177. // Telemetry - contains UUID of the server and timestamp of last send to posthog
  178. // also contains assymetrical encryption pub/priv keys for any server traffic
  179. type Telemetry struct {
  180. UUID string `json:"uuid" bson:"uuid"`
  181. LastSend int64 `json:"lastsend" bson:"lastsend"`
  182. TrafficKeyPriv []byte `json:"traffickeypriv" bson:"traffickeypriv"`
  183. TrafficKeyPub []byte `json:"traffickeypub" bson:"traffickeypub"`
  184. }
  185. // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not
  186. type ServerAddr struct {
  187. IsLeader bool `json:"isleader" bson:"isleader" yaml:"isleader"`
  188. Address string `json:"address" bson:"address" yaml:"address"`
  189. }
  190. // TrafficKeys - struct to hold public keys
  191. type TrafficKeys struct {
  192. Mine []byte `json:"mine" bson:"mine" yaml:"mine"`
  193. Server []byte `json:"server" bson:"server" yaml:"server"`
  194. }
  195. // HostPull - response of a host's pull
  196. type HostPull struct {
  197. Host Host `json:"host" yaml:"host"`
  198. Nodes []Node `json:"nodes" yaml:"nodes"`
  199. Peers []wgtypes.PeerConfig `json:"peers" yaml:"peers"`
  200. ServerConfig ServerConfig `json:"server_config" yaml:"server_config"`
  201. PeerIDs PeerMap `json:"peer_ids,omitempty" yaml:"peer_ids,omitempty"`
  202. HostNetworkInfo HostInfoMap `json:"host_network_info,omitempty" yaml:"host_network_info,omitempty"`
  203. }
  204. // NodeGet - struct for a single node get response
  205. type NodeGet struct {
  206. Node Node `json:"node" bson:"node" yaml:"node"`
  207. Host Host `json:"host" yaml:"host"`
  208. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  209. HostPeers []wgtypes.PeerConfig `json:"host_peers" bson:"host_peers" yaml:"host_peers"`
  210. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  211. PeerIDs PeerMap `json:"peerids,omitempty" bson:"peerids,omitempty" yaml:"peerids,omitempty"`
  212. }
  213. // NodeJoinResponse data returned to node in response to join
  214. type NodeJoinResponse struct {
  215. Node Node `json:"node" bson:"node" yaml:"node"`
  216. Host Host `json:"host" yaml:"host"`
  217. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  218. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  219. }
  220. // ServerConfig - struct for dealing with the server information for a netclient
  221. type ServerConfig struct {
  222. CoreDNSAddr string `yaml:"corednsaddr"`
  223. API string `yaml:"api"`
  224. APIPort string `yaml:"apiport"`
  225. DNSMode string `yaml:"dnsmode"`
  226. Version string `yaml:"version"`
  227. MQPort string `yaml:"mqport"`
  228. MQUserName string `yaml:"mq_username"`
  229. MQPassword string `yaml:"mq_password"`
  230. Server string `yaml:"server"`
  231. Broker string `yaml:"broker"`
  232. IsPro bool `yaml:"isee" json:"Is_EE"`
  233. TrafficKey []byte `yaml:"traffickey"`
  234. }
  235. // User.NameInCharset - returns if name is in charset below or not
  236. func (user *User) NameInCharSet() bool {
  237. charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
  238. for _, char := range user.UserName {
  239. if !strings.Contains(charset, strings.ToLower(string(char))) {
  240. return false
  241. }
  242. }
  243. return true
  244. }
  245. // ServerIDs - struct to hold server ids.
  246. type ServerIDs struct {
  247. ServerIDs []string `json:"server_ids"`
  248. }
  249. // JoinData - struct to hold data required for node to join a network on server
  250. type JoinData struct {
  251. Host Host `json:"host" yaml:"host"`
  252. Node Node `json:"node" yaml:"node"`
  253. Key string `json:"key" yaml:"key"`
  254. }
  255. // HookDetails - struct to hold hook info
  256. type HookDetails struct {
  257. Hook func() error
  258. Interval time.Duration
  259. }
  260. // LicenseLimits - struct license limits
  261. type LicenseLimits struct {
  262. Servers int `json:"servers"`
  263. Users int `json:"users"`
  264. Hosts int `json:"hosts"`
  265. Clients int `json:"clients"`
  266. Networks int `json:"networks"`
  267. }
  268. type SignInReqDto struct {
  269. FormFields FormFields `json:"formFields"`
  270. }
  271. type FormField struct {
  272. Id string `json:"id"`
  273. Value any `json:"value"`
  274. }
  275. type FormFields []FormField
  276. type SignInResDto struct {
  277. Status string `json:"status"`
  278. User User `json:"user"`
  279. }
  280. type TenantLoginResDto struct {
  281. Code int `json:"code"`
  282. Message string `json:"message"`
  283. Response struct {
  284. UserName string `json:"UserName"`
  285. AuthToken string `json:"AuthToken"`
  286. } `json:"response"`
  287. }
  288. type SsoLoginReqDto struct {
  289. OauthProvider string `json:"oauthprovider"`
  290. }
  291. type SsoLoginResDto struct {
  292. User string `json:"UserName"`
  293. AuthToken string `json:"AuthToken"`
  294. }
  295. type SsoLoginData struct {
  296. Expiration time.Time `json:"expiration"`
  297. OauthProvider string `json:"oauthprovider,omitempty"`
  298. OauthCode string `json:"oauthcode,omitempty"`
  299. Username string `json:"username,omitempty"`
  300. AmbAccessToken string `json:"ambaccesstoken,omitempty"`
  301. }
  302. type LoginReqDto struct {
  303. Email string `json:"email"`
  304. TenantID string `json:"tenant_id"`
  305. }
  306. const (
  307. ResHeaderKeyStAccessToken = "St-Access-Token"
  308. )