structs.go 12 KB

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