structs.go 11 KB

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