You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.4 KiB

4 years ago
2 years ago
4 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "sync/atomic"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/security"
  12. "github.com/seaweedfs/seaweedfs/weed/stats"
  13. )
  14. /*
  15. If volume server is started with a separated public port, the public port will
  16. be more "secure".
  17. Public port currently only supports reads.
  18. Later writes on public port can have one of the 3
  19. security settings:
  20. 1. not secured
  21. 2. secured by white list
  22. 3. secured by JWT(Json Web Token)
  23. */
  24. func (vs *VolumeServer) privateStoreHandler(w http.ResponseWriter, r *http.Request) {
  25. w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
  26. if r.Header.Get("Origin") != "" {
  27. w.Header().Set("Access-Control-Allow-Origin", "*")
  28. w.Header().Set("Access-Control-Allow-Credentials", "true")
  29. }
  30. stats.VolumeServerRequestCounter.WithLabelValues(r.Method).Inc()
  31. start := time.Now()
  32. defer func() {
  33. stats.VolumeServerRequestHistogram.WithLabelValues(r.Method).Observe(time.Since(start).Seconds())
  34. }()
  35. switch r.Method {
  36. case "GET", "HEAD":
  37. stats.ReadRequest()
  38. vs.inFlightDownloadDataLimitCond.L.Lock()
  39. for vs.concurrentDownloadLimit != 0 && atomic.LoadInt64(&vs.inFlightDownloadDataSize) > vs.concurrentDownloadLimit {
  40. select {
  41. case <-r.Context().Done():
  42. glog.V(4).Infof("request cancelled from %s: %v", r.RemoteAddr, r.Context().Err())
  43. return
  44. default:
  45. glog.V(4).Infof("wait because inflight download data %d > %d", vs.inFlightDownloadDataSize, vs.concurrentDownloadLimit)
  46. vs.inFlightDownloadDataLimitCond.Wait()
  47. }
  48. }
  49. vs.inFlightDownloadDataLimitCond.L.Unlock()
  50. vs.GetOrHeadHandler(w, r)
  51. case "DELETE":
  52. stats.DeleteRequest()
  53. vs.guard.WhiteList(vs.DeleteHandler)(w, r)
  54. case "PUT", "POST":
  55. contentLength := getContentLength(r)
  56. // exclude the replication from the concurrentUploadLimitMB
  57. if r.URL.Query().Get("type") != "replicate" && vs.concurrentUploadLimit != 0 {
  58. startTime := time.Now()
  59. vs.inFlightUploadDataLimitCond.L.Lock()
  60. for vs.inFlightUploadDataSize > vs.concurrentUploadLimit {
  61. //wait timeout check
  62. if startTime.Add(vs.inflightUploadDataTimeout).Before(time.Now()) {
  63. vs.inFlightUploadDataLimitCond.L.Unlock()
  64. err := fmt.Errorf("reject because inflight upload data %d > %d, and wait timeout", vs.inFlightUploadDataSize, vs.concurrentUploadLimit)
  65. glog.V(1).Infof("too many requests: %v", err)
  66. writeJsonError(w, r, http.StatusTooManyRequests, err)
  67. return
  68. }
  69. glog.V(4).Infof("wait because inflight upload data %d > %d", vs.inFlightUploadDataSize, vs.concurrentUploadLimit)
  70. vs.inFlightUploadDataLimitCond.Wait()
  71. }
  72. vs.inFlightUploadDataLimitCond.L.Unlock()
  73. }
  74. atomic.AddInt64(&vs.inFlightUploadDataSize, contentLength)
  75. defer func() {
  76. atomic.AddInt64(&vs.inFlightUploadDataSize, -contentLength)
  77. if vs.concurrentUploadLimit != 0 {
  78. vs.inFlightUploadDataLimitCond.Signal()
  79. }
  80. }()
  81. // processs uploads
  82. stats.WriteRequest()
  83. vs.guard.WhiteList(vs.PostHandler)(w, r)
  84. case "OPTIONS":
  85. stats.ReadRequest()
  86. w.Header().Add("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")
  87. w.Header().Add("Access-Control-Allow-Headers", "*")
  88. }
  89. }
  90. func getContentLength(r *http.Request) int64 {
  91. contentLength := r.Header.Get("Content-Length")
  92. if contentLength != "" {
  93. length, err := strconv.ParseInt(contentLength, 10, 64)
  94. if err != nil {
  95. return 0
  96. }
  97. return length
  98. }
  99. return 0
  100. }
  101. func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) {
  102. w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
  103. if r.Header.Get("Origin") != "" {
  104. w.Header().Set("Access-Control-Allow-Origin", "*")
  105. w.Header().Set("Access-Control-Allow-Credentials", "true")
  106. }
  107. switch r.Method {
  108. case "GET", "HEAD":
  109. stats.ReadRequest()
  110. vs.inFlightDownloadDataLimitCond.L.Lock()
  111. for vs.concurrentDownloadLimit != 0 && atomic.LoadInt64(&vs.inFlightDownloadDataSize) > vs.concurrentDownloadLimit {
  112. glog.V(4).Infof("wait because inflight download data %d > %d", vs.inFlightDownloadDataSize, vs.concurrentDownloadLimit)
  113. vs.inFlightDownloadDataLimitCond.Wait()
  114. }
  115. vs.inFlightDownloadDataLimitCond.L.Unlock()
  116. vs.GetOrHeadHandler(w, r)
  117. case "OPTIONS":
  118. stats.ReadRequest()
  119. w.Header().Add("Access-Control-Allow-Methods", "GET, OPTIONS")
  120. w.Header().Add("Access-Control-Allow-Headers", "*")
  121. }
  122. }
  123. func (vs *VolumeServer) maybeCheckJwtAuthorization(r *http.Request, vid, fid string, isWrite bool) bool {
  124. var signingKey security.SigningKey
  125. if isWrite {
  126. if len(vs.guard.SigningKey) == 0 {
  127. return true
  128. } else {
  129. signingKey = vs.guard.SigningKey
  130. }
  131. } else {
  132. if len(vs.guard.ReadSigningKey) == 0 {
  133. return true
  134. } else {
  135. signingKey = vs.guard.ReadSigningKey
  136. }
  137. }
  138. tokenStr := security.GetJwt(r)
  139. if tokenStr == "" {
  140. glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
  141. return false
  142. }
  143. token, err := security.DecodeJwt(signingKey, tokenStr, &security.SeaweedFileIdClaims{})
  144. if err != nil {
  145. glog.V(1).Infof("jwt verification error from %s: %v", r.RemoteAddr, err)
  146. return false
  147. }
  148. if !token.Valid {
  149. glog.V(1).Infof("jwt invalid from %s: %v", r.RemoteAddr, tokenStr)
  150. return false
  151. }
  152. if sc, ok := token.Claims.(*security.SeaweedFileIdClaims); ok {
  153. if sepIndex := strings.LastIndex(fid, "_"); sepIndex > 0 {
  154. fid = fid[:sepIndex]
  155. }
  156. return sc.Fid == vid+","+fid
  157. }
  158. glog.V(1).Infof("unexpected jwt from %s: %v", r.RemoteAddr, tokenStr)
  159. return false
  160. }