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.

185 lines
5.8 KiB

4 years ago
2 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(start time.Time) {
  33. stats.VolumeServerRequestHistogram.WithLabelValues(r.Method).Observe(time.Since(start).Seconds())
  34. }(start)
  35. switch r.Method {
  36. case "GET", "HEAD":
  37. stats.ReadRequest()
  38. vs.inFlightDownloadDataLimitCond.L.Lock()
  39. inFlightDownloadSize := atomic.LoadInt64(&vs.inFlightDownloadDataSize)
  40. for vs.concurrentDownloadLimit != 0 && inFlightDownloadSize > vs.concurrentDownloadLimit {
  41. select {
  42. case <-r.Context().Done():
  43. glog.V(4).Infof("request cancelled from %s: %v", r.RemoteAddr, r.Context().Err())
  44. return
  45. default:
  46. glog.V(4).Infof("wait because inflight download data %d > %d", inFlightDownloadSize, vs.concurrentDownloadLimit)
  47. vs.inFlightDownloadDataLimitCond.Wait()
  48. }
  49. inFlightDownloadSize = atomic.LoadInt64(&vs.inFlightDownloadDataSize)
  50. }
  51. vs.inFlightDownloadDataLimitCond.L.Unlock()
  52. vs.GetOrHeadHandler(w, r)
  53. case "DELETE":
  54. stats.DeleteRequest()
  55. vs.guard.WhiteList(vs.DeleteHandler)(w, r)
  56. case "PUT", "POST":
  57. contentLength := getContentLength(r)
  58. // exclude the replication from the concurrentUploadLimitMB
  59. if r.URL.Query().Get("type") != "replicate" && vs.concurrentUploadLimit != 0 {
  60. startTime := time.Now()
  61. vs.inFlightUploadDataLimitCond.L.Lock()
  62. inFlightUploadDataSize := atomic.LoadInt64(&vs.inFlightUploadDataSize)
  63. for inFlightUploadDataSize > vs.concurrentUploadLimit {
  64. //wait timeout check
  65. if startTime.Add(vs.inflightUploadDataTimeout).Before(time.Now()) {
  66. vs.inFlightUploadDataLimitCond.L.Unlock()
  67. err := fmt.Errorf("reject because inflight upload data %d > %d, and wait timeout", inFlightUploadDataSize, vs.concurrentUploadLimit)
  68. glog.V(1).Infof("too many requests: %v", err)
  69. writeJsonError(w, r, http.StatusTooManyRequests, err)
  70. return
  71. }
  72. glog.V(4).Infof("wait because inflight upload data %d > %d", inFlightUploadDataSize, vs.concurrentUploadLimit)
  73. vs.inFlightUploadDataLimitCond.Wait()
  74. inFlightUploadDataSize = atomic.LoadInt64(&vs.inFlightUploadDataSize)
  75. }
  76. vs.inFlightUploadDataLimitCond.L.Unlock()
  77. }
  78. atomic.AddInt64(&vs.inFlightUploadDataSize, contentLength)
  79. defer func() {
  80. atomic.AddInt64(&vs.inFlightUploadDataSize, -contentLength)
  81. if vs.concurrentUploadLimit != 0 {
  82. vs.inFlightUploadDataLimitCond.Signal()
  83. }
  84. }()
  85. // processs uploads
  86. stats.WriteRequest()
  87. vs.guard.WhiteList(vs.PostHandler)(w, r)
  88. case "OPTIONS":
  89. stats.ReadRequest()
  90. w.Header().Add("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")
  91. w.Header().Add("Access-Control-Allow-Headers", "*")
  92. }
  93. }
  94. func getContentLength(r *http.Request) int64 {
  95. contentLength := r.Header.Get("Content-Length")
  96. if contentLength != "" {
  97. length, err := strconv.ParseInt(contentLength, 10, 64)
  98. if err != nil {
  99. return 0
  100. }
  101. return length
  102. }
  103. return 0
  104. }
  105. func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) {
  106. w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
  107. if r.Header.Get("Origin") != "" {
  108. w.Header().Set("Access-Control-Allow-Origin", "*")
  109. w.Header().Set("Access-Control-Allow-Credentials", "true")
  110. }
  111. switch r.Method {
  112. case "GET", "HEAD":
  113. stats.ReadRequest()
  114. vs.inFlightDownloadDataLimitCond.L.Lock()
  115. inFlightDownloadSize := atomic.LoadInt64(&vs.inFlightDownloadDataSize)
  116. for vs.concurrentDownloadLimit != 0 && inFlightDownloadSize > vs.concurrentDownloadLimit {
  117. glog.V(4).Infof("wait because inflight download data %d > %d", inFlightDownloadSize, vs.concurrentDownloadLimit)
  118. vs.inFlightDownloadDataLimitCond.Wait()
  119. inFlightDownloadSize = atomic.LoadInt64(&vs.inFlightDownloadDataSize)
  120. }
  121. vs.inFlightDownloadDataLimitCond.L.Unlock()
  122. vs.GetOrHeadHandler(w, r)
  123. case "OPTIONS":
  124. stats.ReadRequest()
  125. w.Header().Add("Access-Control-Allow-Methods", "GET, OPTIONS")
  126. w.Header().Add("Access-Control-Allow-Headers", "*")
  127. }
  128. }
  129. func (vs *VolumeServer) maybeCheckJwtAuthorization(r *http.Request, vid, fid string, isWrite bool) bool {
  130. var signingKey security.SigningKey
  131. if isWrite {
  132. if len(vs.guard.SigningKey) == 0 {
  133. return true
  134. } else {
  135. signingKey = vs.guard.SigningKey
  136. }
  137. } else {
  138. if len(vs.guard.ReadSigningKey) == 0 {
  139. return true
  140. } else {
  141. signingKey = vs.guard.ReadSigningKey
  142. }
  143. }
  144. tokenStr := security.GetJwt(r)
  145. if tokenStr == "" {
  146. glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
  147. return false
  148. }
  149. token, err := security.DecodeJwt(signingKey, tokenStr, &security.SeaweedFileIdClaims{})
  150. if err != nil {
  151. glog.V(1).Infof("jwt verification error from %s: %v", r.RemoteAddr, err)
  152. return false
  153. }
  154. if !token.Valid {
  155. glog.V(1).Infof("jwt invalid from %s: %v", r.RemoteAddr, tokenStr)
  156. return false
  157. }
  158. if sc, ok := token.Claims.(*security.SeaweedFileIdClaims); ok {
  159. if sepIndex := strings.LastIndex(fid, "_"); sepIndex > 0 {
  160. fid = fid[:sepIndex]
  161. }
  162. return sc.Fid == vid+","+fid
  163. }
  164. glog.V(1).Infof("unexpected jwt from %s: %v", r.RemoteAddr, tokenStr)
  165. return false
  166. }