structs.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package models
  2. import (
  3. "net"
  4. "strings"
  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. // DisplayKey - what is displayed for key
  90. type DisplayKey struct {
  91. Name string `json:"name" bson:"name"`
  92. Uses int `json:"uses" bson:"uses"`
  93. }
  94. // GlobalConfig - global config
  95. type GlobalConfig struct {
  96. Name string `json:"name" bson:"name"`
  97. }
  98. // CheckInResponse - checkin response
  99. type CheckInResponse struct {
  100. Success bool `json:"success" bson:"success"`
  101. NeedPeerUpdate bool `json:"needpeerupdate" bson:"needpeerupdate"`
  102. NeedConfigUpdate bool `json:"needconfigupdate" bson:"needconfigupdate"`
  103. NeedKeyUpdate bool `json:"needkeyupdate" bson:"needkeyupdate"`
  104. NeedDelete bool `json:"needdelete" bson:"needdelete"`
  105. NodeMessage string `json:"nodemessage" bson:"nodemessage"`
  106. IsPending bool `json:"ispending" bson:"ispending"`
  107. }
  108. // PeersResponse - peers response
  109. type PeersResponse struct {
  110. PublicKey string `json:"publickey" bson:"publickey"`
  111. Endpoint string `json:"endpoint" bson:"endpoint"`
  112. Address string `json:"address" bson:"address"`
  113. Address6 string `json:"address6" bson:"address6"`
  114. LocalAddress string `json:"localaddress" bson:"localaddress"`
  115. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  116. IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway"`
  117. EgressGatewayRanges string `json:"egressgatewayrange" bson:"egressgatewayrange"`
  118. ListenPort int32 `json:"listenport" bson:"listenport"`
  119. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  120. }
  121. // ExtPeersResponse - ext peers response
  122. type ExtPeersResponse struct {
  123. PublicKey string `json:"publickey" bson:"publickey"`
  124. Endpoint string `json:"endpoint" bson:"endpoint"`
  125. Address string `json:"address" bson:"address"`
  126. Address6 string `json:"address6" bson:"address6"`
  127. LocalAddress string `json:"localaddress" bson:"localaddress"`
  128. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport"`
  129. ListenPort int32 `json:"listenport" bson:"listenport"`
  130. KeepAlive int32 `json:"persistentkeepalive" bson:"persistentkeepalive"`
  131. }
  132. // EgressGatewayRequest - egress gateway request
  133. type EgressGatewayRequest struct {
  134. NodeID string `json:"nodeid" bson:"nodeid"`
  135. NetID string `json:"netid" bson:"netid"`
  136. NatEnabled string `json:"natenabled" bson:"natenabled"`
  137. Ranges []string `json:"ranges" bson:"ranges"`
  138. }
  139. // RelayRequest - relay request struct
  140. type RelayRequest struct {
  141. NodeID string `json:"nodeid" bson:"nodeid"`
  142. NetID string `json:"netid" bson:"netid"`
  143. RelayAddrs []string `json:"relayaddrs" bson:"relayaddrs"`
  144. }
  145. // HostRelayRequest - struct for host relay creation
  146. type HostRelayRequest struct {
  147. HostID string `json:"host_id"`
  148. RelayedHosts []string `json:"relayed_hosts"`
  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. // NodeGet - struct for a single node get response
  175. type NodeGet struct {
  176. Node Node `json:"node" bson:"node" yaml:"node"`
  177. Host Host `json:"host" yaml:"host"`
  178. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  179. HostPeers []wgtypes.PeerConfig `json:"host_peers" bson:"host_peers" yaml:"host_peers"`
  180. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  181. PeerIDs PeerMap `json:"peerids,omitempty" bson:"peerids,omitempty" yaml:"peerids,omitempty"`
  182. }
  183. // NodeJoinResponse data returned to node in response to join
  184. type NodeJoinResponse struct {
  185. Node Node `json:"node" bson:"node" yaml:"node"`
  186. Host Host `json:"host" yaml:"host"`
  187. ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
  188. Peers []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
  189. }
  190. // ServerConfig - struct for dealing with the server information for a netclient
  191. type ServerConfig struct {
  192. CoreDNSAddr string `yaml:"corednsaddr"`
  193. API string `yaml:"api"`
  194. APIPort string `yaml:"apiport"`
  195. DNSMode string `yaml:"dnsmode"`
  196. Version string `yaml:"version"`
  197. MQPort string `yaml:"mqport"`
  198. MQUserName string `yaml:"mq_username"`
  199. MQPassword string `yaml:"mq_password"`
  200. Server string `yaml:"server"`
  201. Broker string `yaml:"broker"`
  202. Is_EE bool `yaml:"isee"`
  203. StunPort int `yaml:"stun_port"`
  204. StunList []StunServer `yaml:"stun_list"`
  205. TrafficKey []byte `yaml:"traffickey"`
  206. TurnServer *net.UDPAddr `yaml:"turn_server"`
  207. }
  208. // User.NameInCharset - returns if name is in charset below or not
  209. func (user *User) NameInCharSet() bool {
  210. charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
  211. for _, char := range user.UserName {
  212. if !strings.Contains(charset, strings.ToLower(string(char))) {
  213. return false
  214. }
  215. }
  216. return true
  217. }
  218. // ServerIDs - struct to hold server ids.
  219. type ServerIDs struct {
  220. ServerIDs []string `json:"server_ids"`
  221. }
  222. // JoinData - struct to hold data required for node to join a network on server
  223. type JoinData struct {
  224. Host Host `json:"host" yaml:"host"`
  225. Node Node `json:"node" yaml:"node"`
  226. Key string `json:"key" yaml:"key"`
  227. }
  228. // StunServer - struct to hold data required for using stun server
  229. type StunServer struct {
  230. Domain string `json:"domain" yaml:"domain"`
  231. Port int `json:"port" yaml:"port"`
  232. }