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.

163 lines
4.7 KiB

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