structs.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. // CustomExtClient - struct for CustomExtClient params
  15. type CustomExtClient struct {
  16. ClientID string `json:"clientid"`
  17. PublicKey string `json:"publickey,omitempty"`
  18. }
  19. // AuthParams - struct for auth params
  20. type AuthParams struct {
  21. MacAddress string `json:"macaddress"`
  22. ID string `json:"id"`
  23. Password string `json:"password"`
  24. }
  25. // User struct - struct for Users
  26. type User struct {
  27. UserName string `json:"username" bson:"username" validate:"min=3,max=40,in_charset|email"`
  28. Password string `json:"password" bson:"password" validate:"required,min=5"`
  29. Networks []string `json:"networks" bson:"networks"`
  30. IsAdmin bool `json:"isadmin" bson:"isadmin"`
  31. Groups []string `json:"groups" bson:"groups" yaml:"groups"`
  32. }
  33. // ReturnUser - return user struct
  34. type ReturnUser struct {
  35. UserName string `json:"username" bson:"username"`
  36. Networks []string `json:"networks" bson:"networks"`
  37. IsAdmin bool `json:"isadmin" bson:"isadmin"`
  38. Groups []string `json:"groups" bson:"groups"`
  39. }
  40. // UserAuthParams - user auth params struct
  41. type UserAuthParams struct {
  42. UserName string `json:"username"`
  43. Password string `json:"password"`
  44. }
  45. // UserClaims - user claims struct
  46. type UserClaims struct {
  47. IsAdmin bool
  48. UserName string
  49. Networks []string
  50. Groups []string
  51. jwt.RegisteredClaims
  52. }
  53. // SuccessfulUserLoginResponse - successlogin struct
  54. type SuccessfulUserLoginResponse struct {
  55. UserName string
  56. AuthToken string
  57. }
  58. // Claims is a struct that will be encoded to a JWT.
  59. // jwt.StandardClaims is an embedded type to provide expiry time
  60. type Claims struct {
  61. ID string
  62. MacAddress string
  63. Network string
  64. jwt.RegisteredClaims
  65. }
  66. // SuccessfulLoginResponse is struct to send the request response
  67. type SuccessfulLoginResponse struct {
  68. ID string
  69. AuthToken string
  70. }
  71. // ErrorResponse is struct for error
  72. type ErrorResponse struct {
  73. Code int
  74. Message string
  75. }
  76. // NodeAuth - struct for node auth
  77. type NodeAuth struct {
  78. Network string
  79. Password string
  80. MacAddress string // Depricated
  81. ID string
  82. }
  83. // SuccessResponse is struct for sending error message with code.
  84. type SuccessResponse struct {
  85. Code int
  86. Message string
  87. Response interface{}
  88. }
  89. // AccessKey - access key struct
  90. type AccessKey struct {
  91. Name string `json:"name" bson:"name" validate:"omitempty,max=345"`
  92. Value string `json:"value" bson:"value" validate:"omitempty,alphanum,max=16"`
  93. AccessString string `json:"accessstring" bson:"accessstring"`
  94. Uses int `json:"uses" bson:"uses" validate:"numeric,min=0"`
  95. Expiration *time.Time `json:"expiration" bson:"expiration"`
  96. }
  97. // DisplayKey - what is displayed for key
  98. type DisplayKey struct {
  99. Name string `json:"name" bson:"name"`
  100. Uses int `json:"uses" bson:"uses"`
  101. }
  102. // GlobalConfig - global config
  103. type GlobalConfig struct {
  104. Name string `json:"name" bson:"name"`
  105. }
  106. // CheckInResponse - checkin response
  107. type CheckInResponse struct {
  108. Success bool `json:"success" bson:"success"`
  109. NeedPeerUpdate bool `json:"needpeerupdate" bson:"needpeerupdate"`
  110. NeedConfigUpdate bool `json:"needconfigupdate" bson:"needconfigupdate"`
  111. NeedKeyUpdate bool `json:"needkeyupdate" bson:"needkeyupdate"`
  112. NeedDelete bool `json:"needdelete" bson:"needdelete"`
  113. NodeMessage string `json:"nodemessage" bson:"nodemessage"`
  114. IsPending bool `json:"ispending" bson:"ispending"`
  115. }
  116. // PeersResponse - peers response
  117. type PeersResponse 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. IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway"`
  125. EgressGatewayRanges string `json:"egressgatewayrange" bson:"egressgatewayrange"`
  126. ListenPort int32 `json:"listenport" bson:"listenport"`
  127. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  128. }
  129. // ExtPeersResponse - ext peers response
  130. type ExtPeersResponse struct {
  131. PublicKey string `json:"publickey" bson:"publickey"`
  132. Endpoint string `json:"endpoint" bson:"endpoint"`
  133. Address string `json:"address" bson:"address"`
  134. Address6 string `json:"address6" bson:"address6"`
  135. LocalAddress string `json:"localaddress" bson:"localaddress"`
  136. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  137. ListenPort int32 `json:"listenport" bson:"listenport"`
  138. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  139. }
  140. // EgressGatewayRequest - egress gateway request
  141. type EgressGatewayRequest struct {
  142. NodeID string `json:"nodeid" bson:"nodeid"`
  143. NetID string `json:"netid" bson:"netid"`
  144. NatEnabled string `json:"natenabled" bson:"natenabled"`
  145. Ranges []string `json:"ranges" bson:"ranges"`
  146. }
  147. // RelayRequest - relay request struct
  148. type RelayRequest struct {
  149. NodeID string `json:"nodeid" bson:"nodeid"`
  150. NetID string `json:"netid" bson:"netid"`
  151. RelayAddrs []string `json:"relayaddrs" bson:"relayaddrs"`
  152. }
  153. // HostRelayRequest - struct for host relay creation
  154. type HostRelayRequest struct {
  155. HostID string `json:"host_id"`
  156. RelayedHosts []string `json:"relayed_hosts"`
  157. }
  158. // ServerUpdateData - contains data to configure server
  159. // and if it should set peers
  160. type ServerUpdateData struct {
  161. UpdatePeers bool `json:"updatepeers" bson:"updatepeers"`
  162. Node LegacyNode `json:"servernode" bson:"servernode"`
  163. }
  164. // Telemetry - contains UUID of the server and timestamp of last send to posthog
  165. // also contains assymetrical encryption pub/priv keys for any server traffic
  166. type Telemetry struct {
  167. UUID string `json:"uuid" bson:"uuid"`
  168. LastSend int64 `json:"lastsend" bson:"lastsend"`
  169. TrafficKeyPriv []byte `json:"traffickeypriv" bson:"traffickeypriv"`
  170. TrafficKeyPub []byte `json:"traffickeypub" bson:"traffickeypub"`
  171. }
  172. // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not
  173. type ServerAddr struct {
  174. IsLeader bool `json:"isleader" bson:"isleader" yaml:"isleader"`
  175. Address string `json:"address" bson:"address" yaml:"address"`
  176. }
  177. // TrafficKeys - struct to hold public keys
  178. type TrafficKeys struct {
  179. Mine []byte `json:"mine" bson:"mine" yaml:"mine"`
  180. Server []byte `json:"server" bson:"server" yaml:"server"`
  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. Is_EE bool `yaml:"isee"`
  211. StunPort int `yaml:"stun_port"`
  212. StunList []StunServer `yaml:"stun_list"`
  213. TrafficKey []byte `yaml:"traffickey"`
  214. }
  215. // User.NameInCharset - returns if name is in charset below or not
  216. func (user *User) NameInCharSet() bool {
  217. charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
  218. for _, char := range user.UserName {
  219. if !strings.Contains(charset, strings.ToLower(string(char))) {
  220. return false
  221. }
  222. }
  223. return true
  224. }
  225. // ServerIDs - struct to hold server ids.
  226. type ServerIDs struct {
  227. ServerIDs []string `json:"server_ids"`
  228. }
  229. // JoinData - struct to hold data required for node to join a network on server
  230. type JoinData struct {
  231. Host Host `json:"host" yaml:"host"`
  232. Node Node `json:"node" yaml:"node"`
  233. Key string `json:"key" yaml:"key"`
  234. }
  235. // StunServer - struct to hold data required for using stun server
  236. type StunServer struct {
  237. Domain string `json:"domain" yaml:"domain"`
  238. Port int `json:"port" yaml:"port"`
  239. }