structs.go 12 KB

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