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.

160 lines
4.7 KiB

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