structs.go 14 KB

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