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.

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