2
0

structs.go 14 KB

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