structs.go 13 KB

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