structs.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. // 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. type EgressRangeMetric struct {
  137. Network string `json:"network"`
  138. RouteMetric uint32 `json:"route_metric"` // preffered range 1-999
  139. }
  140. // EgressGatewayRequest - egress gateway request
  141. type EgressGatewayRequest struct {
  142. NodeID string `json:"nodeid" bson:"nodeid"`
  143. NetID string `json:"netid" bson:"netid"`
  144. NatEnabled string `json:"natenabled" bson:"natenabled"`
  145. Ranges []string `json:"ranges" bson:"ranges"`
  146. RangesWithMetric []EgressRangeMetric `json:"ranges_with_metric"`
  147. }
  148. // RelayRequest - relay request struct
  149. type RelayRequest struct {
  150. NodeID string `json:"nodeid"`
  151. NetID string `json:"netid"`
  152. RelayedNodes []string `json:"relayaddrs"`
  153. }
  154. // HostRelayRequest - struct for host relay creation
  155. type HostRelayRequest struct {
  156. HostID string `json:"host_id"`
  157. RelayedHosts []string `json:"relayed_hosts"`
  158. }
  159. // IngressRequest - ingress request struct
  160. type IngressRequest struct {
  161. ExtclientDNS string `json:"extclientdns"`
  162. IsInternetGateway bool `json:"is_internet_gw"`
  163. Metadata string `json:"metadata"`
  164. PersistentKeepalive int32 `json:"persistentkeepalive"`
  165. MTU int32 `json:"mtu"`
  166. }
  167. // InetNodeReq - exit node request struct
  168. type InetNodeReq struct {
  169. InetNodeClientIDs []string `json:"inet_node_client_ids"`
  170. }
  171. // ServerUpdateData - contains data to configure server
  172. // and if it should set peers
  173. type ServerUpdateData struct {
  174. UpdatePeers bool `json:"updatepeers" bson:"updatepeers"`
  175. Node LegacyNode `json:"servernode" bson:"servernode"`
  176. }
  177. // Telemetry - contains UUID of the server and timestamp of last send to posthog
  178. // also contains assymetrical encryption pub/priv keys for any server traffic
  179. type Telemetry struct {
  180. UUID string `json:"uuid" bson:"uuid"`
  181. LastSend int64 `json:"lastsend" bson:"lastsend" swaggertype:"primitive,integer" format:"int64"`
  182. TrafficKeyPriv []byte `json:"traffickeypriv" bson:"traffickeypriv"`
  183. TrafficKeyPub []byte `json:"traffickeypub" bson:"traffickeypub"`
  184. }
  185. // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not
  186. type ServerAddr struct {
  187. IsLeader bool `json:"isleader" bson:"isleader" yaml:"isleader"`
  188. Address string `json:"address" bson:"address" yaml:"address"`
  189. }
  190. // TrafficKeys - struct to hold public keys
  191. type TrafficKeys struct {
  192. Mine []byte `json:"mine" bson:"mine" yaml:"mine"`
  193. Server []byte `json:"server" bson:"server" yaml:"server"`
  194. }
  195. // HostPull - response of a host's pull
  196. type HostPull struct {
  197. Host Host `json:"host" yaml:"host"`
  198. Nodes []Node `json:"nodes" yaml:"nodes"`
  199. Peers []wgtypes.PeerConfig `json:"peers" yaml:"peers"`
  200. ServerConfig ServerConfig `json:"server_config" yaml:"server_config"`
  201. PeerIDs PeerMap `json:"peer_ids,omitempty" yaml:"peer_ids,omitempty"`
  202. HostNetworkInfo HostInfoMap `json:"host_network_info,omitempty" yaml:"host_network_info,omitempty"`
  203. EgressRoutes []EgressNetworkRoutes `json:"egress_network_routes"`
  204. FwUpdate FwUpdate `json:"fw_update"`
  205. ChangeDefaultGw bool `json:"change_default_gw"`
  206. DefaultGwIp net.IP `json:"default_gw_ip"`
  207. IsInternetGw bool `json:"is_inet_gw"`
  208. EndpointDetection bool `json:"endpoint_detection"`
  209. }
  210. type DefaultGwInfo struct {
  211. }
  212. // NodeGet - struct for a single node get response
  213. type NodeGet struct {
  214. Node Node `json:"node" bson:"node" yaml:"node"`
  215. Host Host `json:"host" yaml:"host"`
  216. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  217. HostPeers []wgtypes.PeerConfig `json:"host_peers" bson:"host_peers" yaml:"host_peers"`
  218. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  219. PeerIDs PeerMap `json:"peerids,omitempty" bson:"peerids,omitempty" yaml:"peerids,omitempty"`
  220. }
  221. // NodeJoinResponse data returned to node in response to join
  222. type NodeJoinResponse struct {
  223. Node Node `json:"node" bson:"node" yaml:"node"`
  224. Host Host `json:"host" yaml:"host"`
  225. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  226. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  227. }
  228. // ServerConfig - struct for dealing with the server information for a netclient
  229. type ServerConfig struct {
  230. CoreDNSAddr string `yaml:"corednsaddr"`
  231. API string `yaml:"api"`
  232. APIPort string `yaml:"apiport"`
  233. DNSMode string `yaml:"dnsmode"`
  234. Version string `yaml:"version"`
  235. MQPort string `yaml:"mqport"`
  236. MQUserName string `yaml:"mq_username"`
  237. MQPassword string `yaml:"mq_password"`
  238. BrokerType string `yaml:"broker_type"`
  239. Server string `yaml:"server"`
  240. Broker string `yaml:"broker"`
  241. IsPro bool `yaml:"isee" json:"Is_EE"`
  242. TrafficKey []byte `yaml:"traffickey"`
  243. MetricInterval string `yaml:"metric_interval"`
  244. MetricsPort int `yaml:"metrics_port"`
  245. ManageDNS bool `yaml:"manage_dns"`
  246. Stun bool `yaml:"stun"`
  247. StunServers string `yaml:"stun_servers"`
  248. EndpointDetection bool `yaml:"endpoint_detection"`
  249. DefaultDomain string `yaml:"default_domain"`
  250. }
  251. // User.NameInCharset - returns if name is in charset below or not
  252. func (user *User) NameInCharSet() bool {
  253. charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
  254. for _, char := range user.UserName {
  255. if !strings.Contains(charset, strings.ToLower(string(char))) {
  256. return false
  257. }
  258. }
  259. return true
  260. }
  261. // ServerIDs - struct to hold server ids.
  262. type ServerIDs struct {
  263. ServerIDs []string `json:"server_ids"`
  264. }
  265. // JoinData - struct to hold data required for node to join a network on server
  266. type JoinData struct {
  267. Host Host `json:"host" yaml:"host"`
  268. Node Node `json:"node" yaml:"node"`
  269. Key string `json:"key" yaml:"key"`
  270. }
  271. // HookDetails - struct to hold hook info
  272. type HookDetails struct {
  273. Hook func() error
  274. Interval time.Duration
  275. }
  276. // LicenseLimits - struct license limits
  277. type LicenseLimits struct {
  278. Servers int `json:"servers"`
  279. Users int `json:"users"`
  280. Hosts int `json:"hosts"`
  281. Clients int `json:"clients"`
  282. Networks int `json:"networks"`
  283. }
  284. type SignInReqDto struct {
  285. FormFields FormFields `json:"formFields"`
  286. }
  287. type FormField struct {
  288. Id string `json:"id"`
  289. Value any `json:"value"`
  290. }
  291. type FormFields []FormField
  292. type SignInResDto struct {
  293. Status string `json:"status"`
  294. User User `json:"user"`
  295. }
  296. type TenantLoginResDto struct {
  297. Code int `json:"code"`
  298. Message string `json:"message"`
  299. Response struct {
  300. UserName string `json:"UserName"`
  301. AuthToken string `json:"AuthToken"`
  302. } `json:"response"`
  303. }
  304. type SsoLoginReqDto struct {
  305. OauthProvider string `json:"oauthprovider"`
  306. }
  307. type SsoLoginResDto struct {
  308. User string `json:"UserName"`
  309. AuthToken string `json:"AuthToken"`
  310. }
  311. type SsoLoginData struct {
  312. Expiration time.Time `json:"expiration"`
  313. OauthProvider string `json:"oauthprovider,omitempty"`
  314. OauthCode string `json:"oauthcode,omitempty"`
  315. Username string `json:"username,omitempty"`
  316. AmbAccessToken string `json:"ambaccesstoken,omitempty"`
  317. }
  318. type LoginReqDto struct {
  319. Email string `json:"email"`
  320. TenantID string `json:"tenant_id"`
  321. }
  322. const (
  323. ResHeaderKeyStAccessToken = "St-Access-Token"
  324. )
  325. type GetClientConfReqDto struct {
  326. PreferredIp string `json:"preferred_ip"`
  327. }
  328. type RsrcURLInfo struct {
  329. Method string
  330. Path string
  331. }