structs.go 10 KB

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