structs.go 12 KB

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