structs.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package models
  2. import (
  3. "net"
  4. "strings"
  5. "time"
  6. jwt "github.com/golang-jwt/jwt/v4"
  7. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  8. )
  9. const (
  10. // PLACEHOLDER_KEY_TEXT - access key placeholder text if option turned off
  11. PLACEHOLDER_KEY_TEXT = "ACCESS_KEY"
  12. // PLACEHOLDER_TOKEN_TEXT - access key token placeholder text if option turned off
  13. PLACEHOLDER_TOKEN_TEXT = "ACCESS_TOKEN"
  14. )
  15. type FeatureFlags struct {
  16. EnableEgressHA bool `json:"enable_egress_ha"`
  17. EnableNetworkActivity bool `json:"enable_network_activity"`
  18. EnableOAuth bool `json:"enable_oauth"`
  19. EnableIDPIntegration bool `json:"enable_idp_integration"`
  20. AllowMultiServerLicense bool `json:"allow_multi_server_license"`
  21. EnableGwsHA bool `json:"enable_gws_ha"`
  22. EnableDeviceApproval bool `json:"enable_device_approval"`
  23. }
  24. // AuthParams - struct for auth params
  25. type AuthParams struct {
  26. MacAddress string `json:"macaddress"`
  27. ID string `json:"id"`
  28. Password string `json:"password"`
  29. }
  30. // IngressGwUsers - struct to hold users on a ingress gw
  31. type IngressGwUsers struct {
  32. NodeID string `json:"node_id"`
  33. Network string `json:"network"`
  34. Users []ReturnUser `json:"users"`
  35. }
  36. // UserRemoteGws - struct to hold user's remote gws
  37. type UserRemoteGws struct {
  38. GwID string `json:"remote_access_gw_id"`
  39. GWName string `json:"gw_name"`
  40. Network string `json:"network"`
  41. Connected bool `json:"connected"`
  42. IsInternetGateway bool `json:"is_internet_gateway"`
  43. GwClient ExtClient `json:"gw_client"`
  44. GwPeerPublicKey string `json:"gw_peer_public_key"`
  45. GwListenPort int `json:"gw_listen_port"`
  46. Metadata string `json:"metadata"`
  47. AllowedEndpoints []string `json:"allowed_endpoints"`
  48. NetworkAddresses []string `json:"network_addresses"`
  49. Status NodeStatus `json:"status"`
  50. ManageDNS bool `json:"manage_dns"`
  51. DnsAddress string `json:"dns_address"`
  52. Addresses string `json:"addresses"`
  53. MatchDomains []string `json:"match_domains"`
  54. SearchDomains []string `json:"search_domains"`
  55. }
  56. // UserRAGs - struct for user access gws
  57. type UserRAGs struct {
  58. GwID string `json:"remote_access_gw_id"`
  59. GWName string `json:"gw_name"`
  60. Network string `json:"network"`
  61. Connected bool `json:"connected"`
  62. IsInternetGateway bool `json:"is_internet_gateway"`
  63. Metadata string `json:"metadata"`
  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. // PartialUserLoginResponse represents the response returned to the client
  75. // after successful username and password authentication, but before the
  76. // completion of TOTP authentication.
  77. //
  78. // This response includes a temporary token required to complete
  79. // the authentication process.
  80. type PartialUserLoginResponse struct {
  81. UserName string `json:"user_name"`
  82. PreAuthToken string `json:"pre_auth_token"`
  83. }
  84. type TOTPInitiateResponse struct {
  85. OTPAuthURL string `json:"otp_auth_url"`
  86. OTPAuthURLSignature string `json:"otp_auth_url_signature"`
  87. QRCode string `json:"qr_code"`
  88. }
  89. // Claims is a struct that will be encoded to a JWT.
  90. // jwt.StandardClaims is an embedded type to provide expiry time
  91. type Claims struct {
  92. ID string
  93. MacAddress string
  94. Network string
  95. jwt.RegisteredClaims
  96. }
  97. // SuccessfulLoginResponse is struct to send the request response
  98. type SuccessfulLoginResponse struct {
  99. ID string
  100. AuthToken string
  101. }
  102. // ErrorResponse is struct for error
  103. type ErrorResponse struct {
  104. Code int
  105. Message string
  106. }
  107. // NodeAuth - struct for node auth
  108. type NodeAuth struct {
  109. Network string
  110. Password string
  111. MacAddress string // Depricated
  112. ID string
  113. }
  114. // SuccessResponse is struct for sending error message with code.
  115. type SuccessResponse struct {
  116. Code int
  117. Message string
  118. Response interface{}
  119. }
  120. // DisplayKey - what is displayed for key
  121. type DisplayKey struct {
  122. Name string `json:"name" bson:"name"`
  123. Uses int `json:"uses" bson:"uses"`
  124. }
  125. // GlobalConfig - global config
  126. type GlobalConfig struct {
  127. Name string `json:"name" bson:"name"`
  128. }
  129. // CheckInResponse - checkin response
  130. type CheckInResponse struct {
  131. Success bool `json:"success" bson:"success"`
  132. NeedPeerUpdate bool `json:"needpeerupdate" bson:"needpeerupdate"`
  133. NeedConfigUpdate bool `json:"needconfigupdate" bson:"needconfigupdate"`
  134. NeedKeyUpdate bool `json:"needkeyupdate" bson:"needkeyupdate"`
  135. NeedDelete bool `json:"needdelete" bson:"needdelete"`
  136. NodeMessage string `json:"nodemessage" bson:"nodemessage"`
  137. IsPending bool `json:"ispending" bson:"ispending"`
  138. }
  139. // PeersResponse - peers response
  140. type PeersResponse struct {
  141. PublicKey string `json:"publickey" bson:"publickey"`
  142. Endpoint string `json:"endpoint" bson:"endpoint"`
  143. Address string `json:"address" bson:"address"`
  144. Address6 string `json:"address6" bson:"address6"`
  145. LocalAddress string `json:"localaddress" bson:"localaddress"`
  146. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  147. IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway"`
  148. EgressGatewayRanges string `json:"egressgatewayrange" bson:"egressgatewayrange"`
  149. ListenPort int32 `json:"listenport" bson:"listenport"`
  150. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  151. }
  152. // ExtPeersResponse - ext peers response
  153. type ExtPeersResponse struct {
  154. PublicKey string `json:"publickey" bson:"publickey"`
  155. Endpoint string `json:"endpoint" bson:"endpoint"`
  156. Address string `json:"address" bson:"address"`
  157. Address6 string `json:"address6" bson:"address6"`
  158. LocalAddress string `json:"localaddress" bson:"localaddress"`
  159. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  160. ListenPort int32 `json:"listenport" bson:"listenport"`
  161. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  162. }
  163. type EgressRangeMetric struct {
  164. Network string `json:"network"`
  165. RouteMetric uint32 `json:"route_metric"` // preffered range 1-999
  166. Nat bool `json:"nat"`
  167. }
  168. // EgressGatewayRequest - egress gateway request
  169. type EgressGatewayRequest struct {
  170. NodeID string `json:"nodeid" bson:"nodeid"`
  171. NetID string `json:"netid" bson:"netid"`
  172. NatEnabled string `json:"natenabled" bson:"natenabled"`
  173. Ranges []string `json:"ranges" bson:"ranges"`
  174. RangesWithMetric []EgressRangeMetric `json:"ranges_with_metric"`
  175. }
  176. // RelayRequest - relay request struct
  177. type RelayRequest struct {
  178. NodeID string `json:"nodeid"`
  179. NetID string `json:"netid"`
  180. RelayedNodes []string `json:"relayaddrs"`
  181. }
  182. // HostRelayRequest - struct for host relay creation
  183. type HostRelayRequest struct {
  184. HostID string `json:"host_id"`
  185. RelayedHosts []string `json:"relayed_hosts"`
  186. }
  187. // IngressRequest - ingress request struct
  188. type IngressRequest struct {
  189. ExtclientDNS string `json:"extclientdns"`
  190. IsInternetGateway bool `json:"is_internet_gw"`
  191. Metadata string `json:"metadata"`
  192. PersistentKeepalive int32 `json:"persistentkeepalive"`
  193. MTU int32 `json:"mtu"`
  194. }
  195. // InetNodeReq - exit node request struct
  196. type InetNodeReq struct {
  197. InetNodeClientIDs []string `json:"inet_node_client_ids"`
  198. }
  199. // ServerUpdateData - contains data to configure server
  200. // and if it should set peers
  201. type ServerUpdateData struct {
  202. UpdatePeers bool `json:"updatepeers" bson:"updatepeers"`
  203. Node LegacyNode `json:"servernode" bson:"servernode"`
  204. }
  205. // Telemetry - contains UUID of the server and timestamp of last send to posthog
  206. // also contains assymetrical encryption pub/priv keys for any server traffic
  207. type Telemetry struct {
  208. UUID string `json:"uuid" bson:"uuid"`
  209. LastSend int64 `json:"lastsend" bson:"lastsend" swaggertype:"primitive,integer" format:"int64"`
  210. TrafficKeyPriv []byte `json:"traffickeypriv" bson:"traffickeypriv"`
  211. TrafficKeyPub []byte `json:"traffickeypub" bson:"traffickeypub"`
  212. }
  213. // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not
  214. type ServerAddr struct {
  215. IsLeader bool `json:"isleader" bson:"isleader" yaml:"isleader"`
  216. Address string `json:"address" bson:"address" yaml:"address"`
  217. }
  218. // TrafficKeys - struct to hold public keys
  219. type TrafficKeys struct {
  220. Mine []byte `json:"mine" bson:"mine" yaml:"mine"`
  221. Server []byte `json:"server" bson:"server" yaml:"server"`
  222. }
  223. // HostPull - response of a host's pull
  224. type HostPull struct {
  225. Host Host `json:"host" yaml:"host"`
  226. Nodes []Node `json:"nodes" yaml:"nodes"`
  227. Peers []wgtypes.PeerConfig `json:"peers" yaml:"peers"`
  228. ServerConfig ServerConfig `json:"server_config" yaml:"server_config"`
  229. PeerIDs PeerMap `json:"peer_ids,omitempty" yaml:"peer_ids,omitempty"`
  230. HostNetworkInfo HostInfoMap `json:"host_network_info,omitempty" yaml:"host_network_info,omitempty"`
  231. EgressRoutes []EgressNetworkRoutes `json:"egress_network_routes"`
  232. FwUpdate FwUpdate `json:"fw_update"`
  233. ChangeDefaultGw bool `json:"change_default_gw"`
  234. DefaultGwIp net.IP `json:"default_gw_ip"`
  235. IsInternetGw bool `json:"is_inet_gw"`
  236. EndpointDetection bool `json:"endpoint_detection"`
  237. NameServers []string `json:"name_servers"`
  238. EgressWithDomains []EgressDomain `json:"egress_with_domains"`
  239. DnsNameservers []Nameserver `json:"dns_nameservers"`
  240. AutoRelayNodes map[NetworkID][]Node `json:"auto_relay_nodes"`
  241. GwNodes map[NetworkID][]Node `json:"gw_nodes"`
  242. ReplacePeers bool `json:"replace_peers"`
  243. }
  244. // NodeGet - struct for a single node get response
  245. type NodeGet struct {
  246. Node Node `json:"node" bson:"node" yaml:"node"`
  247. Host Host `json:"host" yaml:"host"`
  248. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  249. HostPeers []wgtypes.PeerConfig `json:"host_peers" bson:"host_peers" yaml:"host_peers"`
  250. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  251. PeerIDs PeerMap `json:"peerids,omitempty" bson:"peerids,omitempty" yaml:"peerids,omitempty"`
  252. }
  253. // NodeJoinResponse data returned to node in response to join
  254. type NodeJoinResponse struct {
  255. Node Node `json:"node" bson:"node" yaml:"node"`
  256. Host Host `json:"host" yaml:"host"`
  257. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  258. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  259. }
  260. // ServerConfig - struct for dealing with the server information for a netclient
  261. type ServerConfig struct {
  262. CoreDNSAddr string `yaml:"corednsaddr"`
  263. API string `yaml:"api"`
  264. APIHost string `yaml:"apihost"`
  265. APIPort string `yaml:"apiport"`
  266. DNSMode string `yaml:"dnsmode"`
  267. Version string `yaml:"version"`
  268. MQPort string `yaml:"mqport"`
  269. MQUserName string `yaml:"mq_username"`
  270. MQPassword string `yaml:"mq_password"`
  271. BrokerType string `yaml:"broker_type"`
  272. Server string `yaml:"server"`
  273. Broker string `yaml:"broker"`
  274. IsPro bool `yaml:"isee" json:"Is_EE"`
  275. TrafficKey []byte `yaml:"traffickey"`
  276. MetricInterval string `yaml:"metric_interval"`
  277. MetricsPort int `yaml:"metrics_port"`
  278. ManageDNS bool `yaml:"manage_dns"`
  279. Stun bool `yaml:"stun"`
  280. StunServers string `yaml:"stun_servers"`
  281. EndpointDetection bool `yaml:"endpoint_detection"`
  282. DefaultDomain string `yaml:"default_domain"`
  283. PeerConnectionCheckInterval string `yaml:"peer_connection_check_interval"`
  284. OldAClsSupport bool `json:"-"`
  285. }
  286. // User.NameInCharset - returns if name is in charset below or not
  287. func (user *User) NameInCharSet() bool {
  288. charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
  289. for _, char := range user.UserName {
  290. if !strings.Contains(charset, strings.ToLower(string(char))) {
  291. return false
  292. }
  293. }
  294. return true
  295. }
  296. // ServerIDs - struct to hold server ids.
  297. type ServerIDs struct {
  298. ServerIDs []string `json:"server_ids"`
  299. }
  300. // JoinData - struct to hold data required for node to join a network on server
  301. type JoinData struct {
  302. Host Host `json:"host" yaml:"host"`
  303. Node Node `json:"node" yaml:"node"`
  304. Key string `json:"key" yaml:"key"`
  305. }
  306. // HookDetails - struct to hold hook info
  307. type HookDetails struct {
  308. Hook func() error
  309. Interval time.Duration
  310. }
  311. // LicenseLimits - struct license limits
  312. type LicenseLimits struct {
  313. Servers int `json:"servers"`
  314. Users int `json:"users"`
  315. Hosts int `json:"hosts"`
  316. Clients int `json:"clients"`
  317. Networks int `json:"networks"`
  318. }
  319. type SignInReqDto struct {
  320. FormFields FormFields `json:"formFields"`
  321. }
  322. type FormField struct {
  323. Id string `json:"id"`
  324. Value any `json:"value"`
  325. }
  326. type FormFields []FormField
  327. type SignInResDto struct {
  328. Status string `json:"status"`
  329. User User `json:"user"`
  330. }
  331. type TenantLoginResDto struct {
  332. Code int `json:"code"`
  333. Message string `json:"message"`
  334. Response struct {
  335. UserName string `json:"UserName"`
  336. AuthToken string `json:"AuthToken"`
  337. } `json:"response"`
  338. }
  339. type SsoLoginReqDto struct {
  340. OauthProvider string `json:"oauthprovider"`
  341. }
  342. type SsoLoginResDto struct {
  343. User string `json:"UserName"`
  344. AuthToken string `json:"AuthToken"`
  345. }
  346. type SsoLoginData struct {
  347. Expiration time.Time `json:"expiration"`
  348. OauthProvider string `json:"oauthprovider,omitempty"`
  349. OauthCode string `json:"oauthcode,omitempty"`
  350. Username string `json:"username,omitempty"`
  351. AmbAccessToken string `json:"ambaccesstoken,omitempty"`
  352. }
  353. type LoginReqDto struct {
  354. Email string `json:"email"`
  355. TenantID string `json:"tenant_id"`
  356. }
  357. const (
  358. ResHeaderKeyStAccessToken = "St-Access-Token"
  359. )
  360. type GetClientConfReqDto struct {
  361. PreferredIp string `json:"preferred_ip"`
  362. }
  363. type RsrcURLInfo struct {
  364. Method string
  365. Path string
  366. }
  367. type IDPSyncStatus struct {
  368. // Status would be one of: in_progress, completed or failed.
  369. Status string `json:"status"`
  370. // Description is empty if the sync is ongoing or completed,
  371. // and describes the error when the sync fails.
  372. Description string `json:"description"`
  373. }
  374. type IDPSyncTestRequest struct {
  375. AuthProvider string `json:"auth_provider"`
  376. ClientID string `json:"client_id"`
  377. ClientSecret string `json:"client_secret"`
  378. AzureTenantID string `json:"azure_tenant_id"`
  379. GoogleAdminEmail string `json:"google_admin_email"`
  380. GoogleSACredsJson string `json:"google_sa_creds_json"`
  381. OktaOrgURL string `json:"okta_org_url"`
  382. OktaAPIToken string `json:"okta_api_token"`
  383. }