structs.go 13 KB

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