structs.go 13 KB

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