structs.go 9.7 KB

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