structs.go 15 KB

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