structs.go 11 KB

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