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. MatchDomains []string `json:"match_domains"`
  44. }
  45. // UserRAGs - struct for user access gws
  46. type UserRAGs struct {
  47. GwID string `json:"remote_access_gw_id"`
  48. GWName string `json:"gw_name"`
  49. Network string `json:"network"`
  50. Connected bool `json:"connected"`
  51. IsInternetGateway bool `json:"is_internet_gateway"`
  52. Metadata string `json:"metadata"`
  53. }
  54. // UserRemoteGwsReq - struct to hold user remote acccess gws req
  55. type UserRemoteGwsReq struct {
  56. RemoteAccessClientID string `json:"remote_access_clientid"`
  57. }
  58. // SuccessfulUserLoginResponse - successlogin struct
  59. type SuccessfulUserLoginResponse struct {
  60. UserName string
  61. AuthToken string
  62. }
  63. // PartialUserLoginResponse represents the response returned to the client
  64. // after successful username and password authentication, but before the
  65. // completion of TOTP authentication.
  66. //
  67. // This response includes a temporary token required to complete
  68. // the authentication process.
  69. type PartialUserLoginResponse struct {
  70. UserName string `json:"user_name"`
  71. PreAuthToken string `json:"pre_auth_token"`
  72. }
  73. type TOTPInitiateResponse struct {
  74. OTPAuthURL string `json:"otp_auth_url"`
  75. OTPAuthURLSignature string `json:"otp_auth_url_signature"`
  76. QRCode string `json:"qr_code"`
  77. }
  78. // Claims is a struct that will be encoded to a JWT.
  79. // jwt.StandardClaims is an embedded type to provide expiry time
  80. type Claims struct {
  81. ID string
  82. MacAddress string
  83. Network string
  84. jwt.RegisteredClaims
  85. }
  86. // SuccessfulLoginResponse is struct to send the request response
  87. type SuccessfulLoginResponse struct {
  88. ID string
  89. AuthToken string
  90. }
  91. // ErrorResponse is struct for error
  92. type ErrorResponse struct {
  93. Code int
  94. Message string
  95. }
  96. // NodeAuth - struct for node auth
  97. type NodeAuth struct {
  98. Network string
  99. Password string
  100. MacAddress string // Depricated
  101. ID string
  102. }
  103. // SuccessResponse is struct for sending error message with code.
  104. type SuccessResponse struct {
  105. Code int
  106. Message string
  107. Response interface{}
  108. }
  109. // DisplayKey - what is displayed for key
  110. type DisplayKey struct {
  111. Name string `json:"name" bson:"name"`
  112. Uses int `json:"uses" bson:"uses"`
  113. }
  114. // GlobalConfig - global config
  115. type GlobalConfig struct {
  116. Name string `json:"name" bson:"name"`
  117. }
  118. // CheckInResponse - checkin response
  119. type CheckInResponse struct {
  120. Success bool `json:"success" bson:"success"`
  121. NeedPeerUpdate bool `json:"needpeerupdate" bson:"needpeerupdate"`
  122. NeedConfigUpdate bool `json:"needconfigupdate" bson:"needconfigupdate"`
  123. NeedKeyUpdate bool `json:"needkeyupdate" bson:"needkeyupdate"`
  124. NeedDelete bool `json:"needdelete" bson:"needdelete"`
  125. NodeMessage string `json:"nodemessage" bson:"nodemessage"`
  126. IsPending bool `json:"ispending" bson:"ispending"`
  127. }
  128. // PeersResponse - peers response
  129. type PeersResponse struct {
  130. PublicKey string `json:"publickey" bson:"publickey"`
  131. Endpoint string `json:"endpoint" bson:"endpoint"`
  132. Address string `json:"address" bson:"address"`
  133. Address6 string `json:"address6" bson:"address6"`
  134. LocalAddress string `json:"localaddress" bson:"localaddress"`
  135. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  136. IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway"`
  137. EgressGatewayRanges string `json:"egressgatewayrange" bson:"egressgatewayrange"`
  138. ListenPort int32 `json:"listenport" bson:"listenport"`
  139. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  140. }
  141. // ExtPeersResponse - ext peers response
  142. type ExtPeersResponse struct {
  143. PublicKey string `json:"publickey" bson:"publickey"`
  144. Endpoint string `json:"endpoint" bson:"endpoint"`
  145. Address string `json:"address" bson:"address"`
  146. Address6 string `json:"address6" bson:"address6"`
  147. LocalAddress string `json:"localaddress" bson:"localaddress"`
  148. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  149. ListenPort int32 `json:"listenport" bson:"listenport"`
  150. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  151. }
  152. type EgressRangeMetric struct {
  153. Network string `json:"network"`
  154. RouteMetric uint32 `json:"route_metric"` // preffered range 1-999
  155. Nat bool `json:"nat"`
  156. }
  157. // EgressGatewayRequest - egress gateway request
  158. type EgressGatewayRequest struct {
  159. NodeID string `json:"nodeid" bson:"nodeid"`
  160. NetID string `json:"netid" bson:"netid"`
  161. NatEnabled string `json:"natenabled" bson:"natenabled"`
  162. Ranges []string `json:"ranges" bson:"ranges"`
  163. RangesWithMetric []EgressRangeMetric `json:"ranges_with_metric"`
  164. }
  165. // RelayRequest - relay request struct
  166. type RelayRequest struct {
  167. NodeID string `json:"nodeid"`
  168. NetID string `json:"netid"`
  169. RelayedNodes []string `json:"relayaddrs"`
  170. }
  171. // HostRelayRequest - struct for host relay creation
  172. type HostRelayRequest struct {
  173. HostID string `json:"host_id"`
  174. RelayedHosts []string `json:"relayed_hosts"`
  175. }
  176. // IngressRequest - ingress request struct
  177. type IngressRequest struct {
  178. ExtclientDNS string `json:"extclientdns"`
  179. IsInternetGateway bool `json:"is_internet_gw"`
  180. Metadata string `json:"metadata"`
  181. PersistentKeepalive int32 `json:"persistentkeepalive"`
  182. MTU int32 `json:"mtu"`
  183. }
  184. // InetNodeReq - exit node request struct
  185. type InetNodeReq struct {
  186. InetNodeClientIDs []string `json:"inet_node_client_ids"`
  187. }
  188. // ServerUpdateData - contains data to configure server
  189. // and if it should set peers
  190. type ServerUpdateData struct {
  191. UpdatePeers bool `json:"updatepeers" bson:"updatepeers"`
  192. Node LegacyNode `json:"servernode" bson:"servernode"`
  193. }
  194. // Telemetry - contains UUID of the server and timestamp of last send to posthog
  195. // also contains assymetrical encryption pub/priv keys for any server traffic
  196. type Telemetry struct {
  197. UUID string `json:"uuid" bson:"uuid"`
  198. LastSend int64 `json:"lastsend" bson:"lastsend" swaggertype:"primitive,integer" format:"int64"`
  199. TrafficKeyPriv []byte `json:"traffickeypriv" bson:"traffickeypriv"`
  200. TrafficKeyPub []byte `json:"traffickeypub" bson:"traffickeypub"`
  201. }
  202. // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not
  203. type ServerAddr struct {
  204. IsLeader bool `json:"isleader" bson:"isleader" yaml:"isleader"`
  205. Address string `json:"address" bson:"address" yaml:"address"`
  206. }
  207. // TrafficKeys - struct to hold public keys
  208. type TrafficKeys struct {
  209. Mine []byte `json:"mine" bson:"mine" yaml:"mine"`
  210. Server []byte `json:"server" bson:"server" yaml:"server"`
  211. }
  212. // HostPull - response of a host's pull
  213. type HostPull struct {
  214. Host Host `json:"host" yaml:"host"`
  215. Nodes []Node `json:"nodes" yaml:"nodes"`
  216. Peers []wgtypes.PeerConfig `json:"peers" yaml:"peers"`
  217. ServerConfig ServerConfig `json:"server_config" yaml:"server_config"`
  218. PeerIDs PeerMap `json:"peer_ids,omitempty" yaml:"peer_ids,omitempty"`
  219. HostNetworkInfo HostInfoMap `json:"host_network_info,omitempty" yaml:"host_network_info,omitempty"`
  220. EgressRoutes []EgressNetworkRoutes `json:"egress_network_routes"`
  221. FwUpdate FwUpdate `json:"fw_update"`
  222. ChangeDefaultGw bool `json:"change_default_gw"`
  223. DefaultGwIp net.IP `json:"default_gw_ip"`
  224. IsInternetGw bool `json:"is_inet_gw"`
  225. EndpointDetection bool `json:"endpoint_detection"`
  226. DnsNameservers []Nameserver `json:"dns_nameservers"`
  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. }