structs.go 12 KB

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