2
0

structs.go 12 KB

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