structs.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. GwClient ExtClient `json:"gw_client"`
  62. }
  63. // UserRemoteGwsReq - struct to hold user remote acccess gws req
  64. type UserRemoteGwsReq struct {
  65. RemoteAccessClientID string `json:"remote_access_clientid"`
  66. }
  67. // SuccessfulUserLoginResponse - successlogin struct
  68. type SuccessfulUserLoginResponse struct {
  69. UserName string
  70. AuthToken string
  71. }
  72. // Claims is a struct that will be encoded to a JWT.
  73. // jwt.StandardClaims is an embedded type to provide expiry time
  74. type Claims struct {
  75. ID string
  76. MacAddress string
  77. Network string
  78. jwt.RegisteredClaims
  79. }
  80. // SuccessfulLoginResponse is struct to send the request response
  81. type SuccessfulLoginResponse struct {
  82. ID string
  83. AuthToken string
  84. }
  85. // ErrorResponse is struct for error
  86. type ErrorResponse struct {
  87. Code int
  88. Message string
  89. }
  90. // NodeAuth - struct for node auth
  91. type NodeAuth struct {
  92. Network string
  93. Password string
  94. MacAddress string // Depricated
  95. ID string
  96. }
  97. // SuccessResponse is struct for sending error message with code.
  98. type SuccessResponse struct {
  99. Code int
  100. Message string
  101. Response interface{}
  102. }
  103. // DisplayKey - what is displayed for key
  104. type DisplayKey struct {
  105. Name string `json:"name" bson:"name"`
  106. Uses int `json:"uses" bson:"uses"`
  107. }
  108. // GlobalConfig - global config
  109. type GlobalConfig struct {
  110. Name string `json:"name" bson:"name"`
  111. }
  112. // CheckInResponse - checkin response
  113. type CheckInResponse struct {
  114. Success bool `json:"success" bson:"success"`
  115. NeedPeerUpdate bool `json:"needpeerupdate" bson:"needpeerupdate"`
  116. NeedConfigUpdate bool `json:"needconfigupdate" bson:"needconfigupdate"`
  117. NeedKeyUpdate bool `json:"needkeyupdate" bson:"needkeyupdate"`
  118. NeedDelete bool `json:"needdelete" bson:"needdelete"`
  119. NodeMessage string `json:"nodemessage" bson:"nodemessage"`
  120. IsPending bool `json:"ispending" bson:"ispending"`
  121. }
  122. // PeersResponse - peers response
  123. type PeersResponse struct {
  124. PublicKey string `json:"publickey" bson:"publickey"`
  125. Endpoint string `json:"endpoint" bson:"endpoint"`
  126. Address string `json:"address" bson:"address"`
  127. Address6 string `json:"address6" bson:"address6"`
  128. LocalAddress string `json:"localaddress" bson:"localaddress"`
  129. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  130. IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway"`
  131. EgressGatewayRanges string `json:"egressgatewayrange" bson:"egressgatewayrange"`
  132. ListenPort int32 `json:"listenport" bson:"listenport"`
  133. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  134. }
  135. // ExtPeersResponse - ext peers response
  136. type ExtPeersResponse struct {
  137. PublicKey string `json:"publickey" bson:"publickey"`
  138. Endpoint string `json:"endpoint" bson:"endpoint"`
  139. Address string `json:"address" bson:"address"`
  140. Address6 string `json:"address6" bson:"address6"`
  141. LocalAddress string `json:"localaddress" bson:"localaddress"`
  142. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  143. ListenPort int32 `json:"listenport" bson:"listenport"`
  144. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  145. }
  146. // EgressGatewayRequest - egress gateway request
  147. type EgressGatewayRequest struct {
  148. NodeID string `json:"nodeid" bson:"nodeid"`
  149. NetID string `json:"netid" bson:"netid"`
  150. NatEnabled string `json:"natenabled" bson:"natenabled"`
  151. Ranges []string `json:"ranges" bson:"ranges"`
  152. }
  153. // RelayRequest - relay request struct
  154. type RelayRequest struct {
  155. NodeID string `json:"nodeid"`
  156. NetID string `json:"netid"`
  157. RelayedNodes []string `json:"relayaddrs"`
  158. }
  159. // HostRelayRequest - struct for host relay creation
  160. type HostRelayRequest struct {
  161. HostID string `json:"host_id"`
  162. RelayedHosts []string `json:"relayed_hosts"`
  163. }
  164. // IngressRequest - ingress request struct
  165. type IngressRequest struct {
  166. ExtclientDNS string `json:"extclientdns"`
  167. Failover bool `json:"failover"`
  168. }
  169. // ServerUpdateData - contains data to configure server
  170. // and if it should set peers
  171. type ServerUpdateData struct {
  172. UpdatePeers bool `json:"updatepeers" bson:"updatepeers"`
  173. Node LegacyNode `json:"servernode" bson:"servernode"`
  174. }
  175. // Telemetry - contains UUID of the server and timestamp of last send to posthog
  176. // also contains assymetrical encryption pub/priv keys for any server traffic
  177. type Telemetry struct {
  178. UUID string `json:"uuid" bson:"uuid"`
  179. LastSend int64 `json:"lastsend" bson:"lastsend"`
  180. TrafficKeyPriv []byte `json:"traffickeypriv" bson:"traffickeypriv"`
  181. TrafficKeyPub []byte `json:"traffickeypub" bson:"traffickeypub"`
  182. }
  183. // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not
  184. type ServerAddr struct {
  185. IsLeader bool `json:"isleader" bson:"isleader" yaml:"isleader"`
  186. Address string `json:"address" bson:"address" yaml:"address"`
  187. }
  188. // TrafficKeys - struct to hold public keys
  189. type TrafficKeys struct {
  190. Mine []byte `json:"mine" bson:"mine" yaml:"mine"`
  191. Server []byte `json:"server" bson:"server" yaml:"server"`
  192. }
  193. // HostPull - response of a host's pull
  194. type HostPull struct {
  195. Host Host `json:"host" yaml:"host"`
  196. Nodes []Node `json:"nodes" yaml:"nodes"`
  197. Peers []wgtypes.PeerConfig `json:"peers" yaml:"peers"`
  198. ServerConfig ServerConfig `json:"server_config" yaml:"server_config"`
  199. PeerIDs PeerMap `json:"peer_ids,omitempty" yaml:"peer_ids,omitempty"`
  200. }
  201. // NodeGet - struct for a single node get response
  202. type NodeGet struct {
  203. Node Node `json:"node" bson:"node" yaml:"node"`
  204. Host Host `json:"host" yaml:"host"`
  205. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  206. HostPeers []wgtypes.PeerConfig `json:"host_peers" bson:"host_peers" yaml:"host_peers"`
  207. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  208. PeerIDs PeerMap `json:"peerids,omitempty" bson:"peerids,omitempty" yaml:"peerids,omitempty"`
  209. }
  210. // NodeJoinResponse data returned to node in response to join
  211. type NodeJoinResponse struct {
  212. Node Node `json:"node" bson:"node" yaml:"node"`
  213. Host Host `json:"host" yaml:"host"`
  214. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  215. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  216. }
  217. // ServerConfig - struct for dealing with the server information for a netclient
  218. type ServerConfig struct {
  219. CoreDNSAddr string `yaml:"corednsaddr"`
  220. API string `yaml:"api"`
  221. APIPort string `yaml:"apiport"`
  222. DNSMode string `yaml:"dnsmode"`
  223. Version string `yaml:"version"`
  224. MQPort string `yaml:"mqport"`
  225. MQUserName string `yaml:"mq_username"`
  226. MQPassword string `yaml:"mq_password"`
  227. Server string `yaml:"server"`
  228. Broker string `yaml:"broker"`
  229. IsPro bool `yaml:"isee" json:"Is_EE"`
  230. StunPort int `yaml:"stun_port"`
  231. TrafficKey []byte `yaml:"traffickey"`
  232. TurnDomain string `yaml:"turn_domain"`
  233. TurnPort int `yaml:"turn_port"`
  234. UseTurn bool `yaml:"use_turn"`
  235. }
  236. // User.NameInCharset - returns if name is in charset below or not
  237. func (user *User) NameInCharSet() bool {
  238. charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
  239. for _, char := range user.UserName {
  240. if !strings.Contains(charset, strings.ToLower(string(char))) {
  241. return false
  242. }
  243. }
  244. return true
  245. }
  246. // ServerIDs - struct to hold server ids.
  247. type ServerIDs struct {
  248. ServerIDs []string `json:"server_ids"`
  249. }
  250. // JoinData - struct to hold data required for node to join a network on server
  251. type JoinData struct {
  252. Host Host `json:"host" yaml:"host"`
  253. Node Node `json:"node" yaml:"node"`
  254. Key string `json:"key" yaml:"key"`
  255. }
  256. // HookDetails - struct to hold hook info
  257. type HookDetails struct {
  258. Hook func() error
  259. Interval time.Duration
  260. }
  261. // LicenseLimits - struct license limits
  262. type LicenseLimits struct {
  263. Servers int `json:"servers"`
  264. Users int `json:"users"`
  265. Hosts int `json:"hosts"`
  266. Clients int `json:"clients"`
  267. Networks int `json:"networks"`
  268. }