structs.go 12 KB

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