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.

173 lines
5.2 KiB

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