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.

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