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.

150 lines
4.1 KiB

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