structs.go 8.1 KB

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