structs.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package models
  2. import (
  3. "strings"
  4. "time"
  5. jwt "github.com/golang-jwt/jwt/v4"
  6. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  7. )
  8. const (
  9. // PLACEHOLDER_KEY_TEXT - access key placeholder text if option turned off
  10. PLACEHOLDER_KEY_TEXT = "ACCESS_KEY"
  11. // PLACEHOLDER_TOKEN_TEXT - access key token placeholder text if option turned off
  12. PLACEHOLDER_TOKEN_TEXT = "ACCESS_TOKEN"
  13. )
  14. // AuthParams - struct for auth params
  15. type AuthParams struct {
  16. MacAddress string `json:"macaddress"`
  17. ID string `json:"id"`
  18. Password string `json:"password"`
  19. }
  20. // User struct - struct for Users
  21. type User struct {
  22. UserName string `json:"username" bson:"username" validate:"min=3,max=40,in_charset|email"`
  23. Password string `json:"password" bson:"password" validate:"required,min=5"`
  24. IsAdmin bool `json:"isadmin" bson:"isadmin"`
  25. IsSuperAdmin bool `json:"issuperadmin"`
  26. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"`
  27. LastLoginTime time.Time `json:"last_login_time"`
  28. }
  29. // ReturnUser - return user struct
  30. type ReturnUser struct {
  31. UserName string `json:"username"`
  32. IsAdmin bool `json:"isadmin"`
  33. IsSuperAdmin bool `json:"issuperadmin"`
  34. RemoteGwIDs map[string]struct{} `json:"remote_gw_ids"`
  35. LastLoginTime time.Time `json:"last_login_time"`
  36. }
  37. // UserAuthParams - user auth params struct
  38. type UserAuthParams struct {
  39. UserName string `json:"username"`
  40. Password string `json:"password"`
  41. }
  42. // UserClaims - user claims struct
  43. type UserClaims struct {
  44. IsAdmin bool
  45. IsSuperAdmin bool
  46. UserName string
  47. jwt.RegisteredClaims
  48. }
  49. // IngressGwUsers - struct to hold users on a ingress gw
  50. type IngressGwUsers struct {
  51. NodeID string `json:"node_id"`
  52. Network string `json:"network"`
  53. Users []ReturnUser `json:"users"`
  54. }
  55. // UserRemoteGws - struct to hold user's remote gws
  56. type UserRemoteGws struct {
  57. GwID string `json:"remote_access_gw_id"`
  58. GWName string `json:"gw_name"`
  59. Network string `json:"network"`
  60. Connected bool `json:"connected"`
  61. IsInternetGateway bool `json:"is_internet_gateway"`
  62. GwClient ExtClient `json:"gw_client"`
  63. }
  64. // UserRemoteGwsReq - struct to hold user remote acccess gws req
  65. type UserRemoteGwsReq struct {
  66. RemoteAccessClientID string `json:"remote_access_clientid"`
  67. }
  68. // SuccessfulUserLoginResponse - successlogin struct
  69. type SuccessfulUserLoginResponse struct {
  70. UserName string
  71. AuthToken string
  72. }
  73. // Claims is a struct that will be encoded to a JWT.
  74. // jwt.StandardClaims is an embedded type to provide expiry time
  75. type Claims struct {
  76. ID string
  77. MacAddress string
  78. Network string
  79. jwt.RegisteredClaims
  80. }
  81. // SuccessfulLoginResponse is struct to send the request response
  82. type SuccessfulLoginResponse struct {
  83. ID string
  84. AuthToken string
  85. }
  86. // ErrorResponse is struct for error
  87. type ErrorResponse struct {
  88. Code int
  89. Message string
  90. }
  91. // NodeAuth - struct for node auth
  92. type NodeAuth struct {
  93. Network string
  94. Password string
  95. MacAddress string // Depricated
  96. ID string
  97. }
  98. // SuccessResponse is struct for sending error message with code.
  99. type SuccessResponse struct {
  100. Code int
  101. Message string
  102. Response interface{}
  103. }
  104. // DisplayKey - what is displayed for key
  105. type DisplayKey struct {
  106. Name string `json:"name" bson:"name"`
  107. Uses int `json:"uses" bson:"uses"`
  108. }
  109. // GlobalConfig - global config
  110. type GlobalConfig struct {
  111. Name string `json:"name" bson:"name"`
  112. }
  113. // CheckInResponse - checkin response
  114. type CheckInResponse struct {
  115. Success bool `json:"success" bson:"success"`
  116. NeedPeerUpdate bool `json:"needpeerupdate" bson:"needpeerupdate"`
  117. NeedConfigUpdate bool `json:"needconfigupdate" bson:"needconfigupdate"`
  118. NeedKeyUpdate bool `json:"needkeyupdate" bson:"needkeyupdate"`
  119. NeedDelete bool `json:"needdelete" bson:"needdelete"`
  120. NodeMessage string `json:"nodemessage" bson:"nodemessage"`
  121. IsPending bool `json:"ispending" bson:"ispending"`
  122. }
  123. // PeersResponse - peers response
  124. type PeersResponse struct {
  125. PublicKey string `json:"publickey" bson:"publickey"`
  126. Endpoint string `json:"endpoint" bson:"endpoint"`
  127. Address string `json:"address" bson:"address"`
  128. Address6 string `json:"address6" bson:"address6"`
  129. LocalAddress string `json:"localaddress" bson:"localaddress"`
  130. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  131. IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway"`
  132. EgressGatewayRanges string `json:"egressgatewayrange" bson:"egressgatewayrange"`
  133. ListenPort int32 `json:"listenport" bson:"listenport"`
  134. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  135. }
  136. // ExtPeersResponse - ext peers response
  137. type ExtPeersResponse struct {
  138. PublicKey string `json:"publickey" bson:"publickey"`
  139. Endpoint string `json:"endpoint" bson:"endpoint"`
  140. Address string `json:"address" bson:"address"`
  141. Address6 string `json:"address6" bson:"address6"`
  142. LocalAddress string `json:"localaddress" bson:"localaddress"`
  143. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  144. ListenPort int32 `json:"listenport" bson:"listenport"`
  145. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  146. }
  147. // EgressGatewayRequest - egress gateway request
  148. type EgressGatewayRequest struct {
  149. NodeID string `json:"nodeid" bson:"nodeid"`
  150. NetID string `json:"netid" bson:"netid"`
  151. NatEnabled string `json:"natenabled" bson:"natenabled"`
  152. Ranges []string `json:"ranges" bson:"ranges"`
  153. }
  154. // RelayRequest - relay request struct
  155. type RelayRequest struct {
  156. NodeID string `json:"nodeid"`
  157. NetID string `json:"netid"`
  158. RelayedNodes []string `json:"relayaddrs"`
  159. }
  160. // HostRelayRequest - struct for host relay creation
  161. type HostRelayRequest struct {
  162. HostID string `json:"host_id"`
  163. RelayedHosts []string `json:"relayed_hosts"`
  164. }
  165. // IngressRequest - ingress request struct
  166. type IngressRequest struct {
  167. ExtclientDNS string `json:"extclientdns"`
  168. IsInternetGateway bool `json:"is_internet_gw"`
  169. }
  170. // ServerUpdateData - contains data to configure server
  171. // and if it should set peers
  172. type ServerUpdateData struct {
  173. UpdatePeers bool `json:"updatepeers" bson:"updatepeers"`
  174. Node LegacyNode `json:"servernode" bson:"servernode"`
  175. }
  176. // Telemetry - contains UUID of the server and timestamp of last send to posthog
  177. // also contains assymetrical encryption pub/priv keys for any server traffic
  178. type Telemetry struct {
  179. UUID string `json:"uuid" bson:"uuid"`
  180. LastSend int64 `json:"lastsend" bson:"lastsend"`
  181. TrafficKeyPriv []byte `json:"traffickeypriv" bson:"traffickeypriv"`
  182. TrafficKeyPub []byte `json:"traffickeypub" bson:"traffickeypub"`
  183. }
  184. // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not
  185. type ServerAddr struct {
  186. IsLeader bool `json:"isleader" bson:"isleader" yaml:"isleader"`
  187. Address string `json:"address" bson:"address" yaml:"address"`
  188. }
  189. // TrafficKeys - struct to hold public keys
  190. type TrafficKeys struct {
  191. Mine []byte `json:"mine" bson:"mine" yaml:"mine"`
  192. Server []byte `json:"server" bson:"server" yaml:"server"`
  193. }
  194. // HostPull - response of a host's pull
  195. type HostPull struct {
  196. Host Host `json:"host" yaml:"host"`
  197. Nodes []Node `json:"nodes" yaml:"nodes"`
  198. Peers []wgtypes.PeerConfig `json:"peers" yaml:"peers"`
  199. ServerConfig ServerConfig `json:"server_config" yaml:"server_config"`
  200. PeerIDs PeerMap `json:"peer_ids,omitempty" yaml:"peer_ids,omitempty"`
  201. HostNetworkInfo HostInfoMap `json:"host_network_info,omitempty" yaml:"host_network_info,omitempty"`
  202. }
  203. // NodeGet - struct for a single node get response
  204. type NodeGet struct {
  205. Node Node `json:"node" bson:"node" yaml:"node"`
  206. Host Host `json:"host" yaml:"host"`
  207. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  208. HostPeers []wgtypes.PeerConfig `json:"host_peers" bson:"host_peers" yaml:"host_peers"`
  209. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  210. PeerIDs PeerMap `json:"peerids,omitempty" bson:"peerids,omitempty" yaml:"peerids,omitempty"`
  211. }
  212. // NodeJoinResponse data returned to node in response to join
  213. type NodeJoinResponse struct {
  214. Node Node `json:"node" bson:"node" yaml:"node"`
  215. Host Host `json:"host" yaml:"host"`
  216. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  217. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  218. }
  219. // ServerConfig - struct for dealing with the server information for a netclient
  220. type ServerConfig struct {
  221. CoreDNSAddr string `yaml:"corednsaddr"`
  222. API string `yaml:"api"`
  223. APIPort string `yaml:"apiport"`
  224. DNSMode string `yaml:"dnsmode"`
  225. Version string `yaml:"version"`
  226. MQPort string `yaml:"mqport"`
  227. MQUserName string `yaml:"mq_username"`
  228. MQPassword string `yaml:"mq_password"`
  229. Server string `yaml:"server"`
  230. Broker string `yaml:"broker"`
  231. IsPro bool `yaml:"isee" json:"Is_EE"`
  232. StunPort int `yaml:"stun_port"`
  233. TrafficKey []byte `yaml:"traffickey"`
  234. TurnDomain string `yaml:"turn_domain"`
  235. TurnPort int `yaml:"turn_port"`
  236. UseTurn bool `yaml:"use_turn"`
  237. }
  238. // User.NameInCharset - returns if name is in charset below or not
  239. func (user *User) NameInCharSet() bool {
  240. charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
  241. for _, char := range user.UserName {
  242. if !strings.Contains(charset, strings.ToLower(string(char))) {
  243. return false
  244. }
  245. }
  246. return true
  247. }
  248. // ServerIDs - struct to hold server ids.
  249. type ServerIDs struct {
  250. ServerIDs []string `json:"server_ids"`
  251. }
  252. // JoinData - struct to hold data required for node to join a network on server
  253. type JoinData struct {
  254. Host Host `json:"host" yaml:"host"`
  255. Node Node `json:"node" yaml:"node"`
  256. Key string `json:"key" yaml:"key"`
  257. }
  258. // HookDetails - struct to hold hook info
  259. type HookDetails struct {
  260. Hook func() error
  261. Interval time.Duration
  262. }
  263. // LicenseLimits - struct license limits
  264. type LicenseLimits struct {
  265. Servers int `json:"servers"`
  266. Users int `json:"users"`
  267. Hosts int `json:"hosts"`
  268. Clients int `json:"clients"`
  269. Networks int `json:"networks"`
  270. }