structs.go 9.3 KB

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