structs.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // CustomExtClient - struct for CustomExtClient params
  14. type CustomExtClient struct {
  15. ClientID string `json:"clientid"`
  16. PublicKey string `json:"publickey,omitempty"`
  17. }
  18. // AuthParams - struct for auth params
  19. type AuthParams struct {
  20. MacAddress string `json:"macaddress"`
  21. ID string `json:"id"`
  22. Password string `json:"password"`
  23. }
  24. // User struct - struct for Users
  25. type User struct {
  26. UserName string `json:"username" bson:"username" validate:"min=3,max=40,in_charset|email"`
  27. Password string `json:"password" bson:"password" validate:"required,min=5"`
  28. Networks []string `json:"networks" bson:"networks"`
  29. IsAdmin bool `json:"isadmin" bson:"isadmin"`
  30. Groups []string `json:"groups" bson:"groups" yaml:"groups"`
  31. }
  32. // ReturnUser - return user struct
  33. type ReturnUser struct {
  34. UserName string `json:"username" bson:"username"`
  35. Networks []string `json:"networks" bson:"networks"`
  36. IsAdmin bool `json:"isadmin" bson:"isadmin"`
  37. Groups []string `json:"groups" bson:"groups"`
  38. }
  39. // UserAuthParams - user auth params struct
  40. type UserAuthParams struct {
  41. UserName string `json:"username"`
  42. Password string `json:"password"`
  43. }
  44. // UserClaims - user claims struct
  45. type UserClaims struct {
  46. IsAdmin bool
  47. UserName string
  48. Networks []string
  49. Groups []string
  50. jwt.RegisteredClaims
  51. }
  52. // SuccessfulUserLoginResponse - successlogin struct
  53. type SuccessfulUserLoginResponse struct {
  54. UserName string
  55. AuthToken string
  56. }
  57. // Claims is a struct that will be encoded to a JWT.
  58. // jwt.StandardClaims is an embedded type to provide expiry time
  59. type Claims struct {
  60. ID string
  61. MacAddress string
  62. Network string
  63. jwt.RegisteredClaims
  64. }
  65. // SuccessfulLoginResponse is struct to send the request response
  66. type SuccessfulLoginResponse struct {
  67. ID string
  68. AuthToken string
  69. }
  70. // ErrorResponse is struct for error
  71. type ErrorResponse struct {
  72. Code int
  73. Message string
  74. }
  75. // NodeAuth - struct for node auth
  76. type NodeAuth struct {
  77. Network string
  78. Password string
  79. MacAddress string // Depricated
  80. ID string
  81. }
  82. // SuccessResponse is struct for sending error message with code.
  83. type SuccessResponse struct {
  84. Code int
  85. Message string
  86. Response interface{}
  87. }
  88. // DisplayKey - what is displayed for key
  89. type DisplayKey struct {
  90. Name string `json:"name" bson:"name"`
  91. Uses int `json:"uses" bson:"uses"`
  92. }
  93. // GlobalConfig - global config
  94. type GlobalConfig struct {
  95. Name string `json:"name" bson:"name"`
  96. }
  97. // CheckInResponse - checkin response
  98. type CheckInResponse struct {
  99. Success bool `json:"success" bson:"success"`
  100. NeedPeerUpdate bool `json:"needpeerupdate" bson:"needpeerupdate"`
  101. NeedConfigUpdate bool `json:"needconfigupdate" bson:"needconfigupdate"`
  102. NeedKeyUpdate bool `json:"needkeyupdate" bson:"needkeyupdate"`
  103. NeedDelete bool `json:"needdelete" bson:"needdelete"`
  104. NodeMessage string `json:"nodemessage" bson:"nodemessage"`
  105. IsPending bool `json:"ispending" bson:"ispending"`
  106. }
  107. // PeersResponse - peers response
  108. type PeersResponse struct {
  109. PublicKey string `json:"publickey" bson:"publickey"`
  110. Endpoint string `json:"endpoint" bson:"endpoint"`
  111. Address string `json:"address" bson:"address"`
  112. Address6 string `json:"address6" bson:"address6"`
  113. LocalAddress string `json:"localaddress" bson:"localaddress"`
  114. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  115. IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway"`
  116. EgressGatewayRanges string `json:"egressgatewayrange" bson:"egressgatewayrange"`
  117. ListenPort int32 `json:"listenport" bson:"listenport"`
  118. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  119. }
  120. // ExtPeersResponse - ext peers response
  121. type ExtPeersResponse 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. ListenPort int32 `json:"listenport" bson:"listenport"`
  129. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  130. }
  131. // EgressGatewayRequest - egress gateway request
  132. type EgressGatewayRequest struct {
  133. NodeID string `json:"nodeid" bson:"nodeid"`
  134. NetID string `json:"netid" bson:"netid"`
  135. NatEnabled string `json:"natenabled" bson:"natenabled"`
  136. Ranges []string `json:"ranges" bson:"ranges"`
  137. }
  138. // RelayRequest - relay request struct
  139. type RelayRequest struct {
  140. NodeID string `json:"nodeid" bson:"nodeid"`
  141. NetID string `json:"netid" bson:"netid"`
  142. RelayAddrs []string `json:"relayaddrs" bson:"relayaddrs"`
  143. }
  144. // HostRelayRequest - struct for host relay creation
  145. type HostRelayRequest struct {
  146. HostID string `json:"host_id"`
  147. RelayedHosts []string `json:"relayed_hosts"`
  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. // NodeGet - struct for a single node get response
  174. type NodeGet struct {
  175. Node Node `json:"node" bson:"node" yaml:"node"`
  176. Host Host `json:"host" yaml:"host"`
  177. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  178. HostPeers []wgtypes.PeerConfig `json:"host_peers" bson:"host_peers" yaml:"host_peers"`
  179. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  180. PeerIDs PeerMap `json:"peerids,omitempty" bson:"peerids,omitempty" yaml:"peerids,omitempty"`
  181. }
  182. // NodeJoinResponse data returned to node in response to join
  183. type NodeJoinResponse struct {
  184. Node Node `json:"node" bson:"node" yaml:"node"`
  185. Host Host `json:"host" yaml:"host"`
  186. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  187. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  188. }
  189. // ServerConfig - struct for dealing with the server information for a netclient
  190. type ServerConfig struct {
  191. CoreDNSAddr string `yaml:"corednsaddr"`
  192. API string `yaml:"api"`
  193. APIPort string `yaml:"apiport"`
  194. DNSMode string `yaml:"dnsmode"`
  195. Version string `yaml:"version"`
  196. MQPort string `yaml:"mqport"`
  197. MQUserName string `yaml:"mq_username"`
  198. MQPassword string `yaml:"mq_password"`
  199. Server string `yaml:"server"`
  200. Broker string `yaml:"broker"`
  201. Is_EE bool `yaml:"isee"`
  202. StunPort int `yaml:"stun_port"`
  203. StunList []StunServer `yaml:"stun_list"`
  204. TrafficKey []byte `yaml:"traffickey"`
  205. }
  206. // User.NameInCharset - returns if name is in charset below or not
  207. func (user *User) NameInCharSet() bool {
  208. charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
  209. for _, char := range user.UserName {
  210. if !strings.Contains(charset, strings.ToLower(string(char))) {
  211. return false
  212. }
  213. }
  214. return true
  215. }
  216. // ServerIDs - struct to hold server ids.
  217. type ServerIDs struct {
  218. ServerIDs []string `json:"server_ids"`
  219. }
  220. // JoinData - struct to hold data required for node to join a network on server
  221. type JoinData struct {
  222. Host Host `json:"host" yaml:"host"`
  223. Node Node `json:"node" yaml:"node"`
  224. Key string `json:"key" yaml:"key"`
  225. }
  226. // StunServer - struct to hold data required for using stun server
  227. type StunServer struct {
  228. Domain string `json:"domain" yaml:"domain"`
  229. Port int `json:"port" yaml:"port"`
  230. }