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.

175 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" && vs.concurrentUploadLimit != 0 {
  53. startTime := time.Now()
  54. vs.inFlightUploadDataLimitCond.L.Lock()
  55. for 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. if vs.concurrentUploadLimit != 0 {
  73. vs.inFlightUploadDataLimitCond.Signal()
  74. }
  75. }()
  76. // processs uploads
  77. stats.WriteRequest()
  78. vs.guard.WhiteList(vs.PostHandler)(w, r)
  79. case "OPTIONS":
  80. stats.ReadRequest()
  81. w.Header().Add("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")
  82. w.Header().Add("Access-Control-Allow-Headers", "*")
  83. }
  84. }
  85. func getContentLength(r *http.Request) int64 {
  86. contentLength := r.Header.Get("Content-Length")
  87. if contentLength != "" {
  88. length, err := strconv.ParseInt(contentLength, 10, 64)
  89. if err != nil {
  90. return 0
  91. }
  92. return length
  93. }
  94. return 0
  95. }
  96. func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) {
  97. w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
  98. if r.Header.Get("Origin") != "" {
  99. w.Header().Set("Access-Control-Allow-Origin", "*")
  100. w.Header().Set("Access-Control-Allow-Credentials", "true")
  101. }
  102. switch r.Method {
  103. case "GET", "HEAD":
  104. stats.ReadRequest()
  105. vs.inFlightDownloadDataLimitCond.L.Lock()
  106. for vs.concurrentDownloadLimit != 0 && atomic.LoadInt64(&vs.inFlightDownloadDataSize) > vs.concurrentDownloadLimit {
  107. glog.V(4).Infof("wait because inflight download data %d > %d", vs.inFlightDownloadDataSize, vs.concurrentDownloadLimit)
  108. vs.inFlightDownloadDataLimitCond.Wait()
  109. }
  110. vs.inFlightDownloadDataLimitCond.L.Unlock()
  111. vs.GetOrHeadHandler(w, r)
  112. case "OPTIONS":
  113. stats.ReadRequest()
  114. w.Header().Add("Access-Control-Allow-Methods", "GET, OPTIONS")
  115. w.Header().Add("Access-Control-Allow-Headers", "*")
  116. }
  117. }
  118. func (vs *VolumeServer) maybeCheckJwtAuthorization(r *http.Request, vid, fid string, isWrite bool) bool {
  119. var signingKey security.SigningKey
  120. if isWrite {
  121. if len(vs.guard.SigningKey) == 0 {
  122. return true
  123. } else {
  124. signingKey = vs.guard.SigningKey
  125. }
  126. } else {
  127. if len(vs.guard.ReadSigningKey) == 0 {
  128. return true
  129. } else {
  130. signingKey = vs.guard.ReadSigningKey
  131. }
  132. }
  133. tokenStr := security.GetJwt(r)
  134. if tokenStr == "" {
  135. glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
  136. return false
  137. }
  138. token, err := security.DecodeJwt(signingKey, tokenStr, &security.SeaweedFileIdClaims{})
  139. if err != nil {
  140. glog.V(1).Infof("jwt verification error from %s: %v", r.RemoteAddr, err)
  141. return false
  142. }
  143. if !token.Valid {
  144. glog.V(1).Infof("jwt invalid from %s: %v", r.RemoteAddr, tokenStr)
  145. return false
  146. }
  147. if sc, ok := token.Claims.(*security.SeaweedFileIdClaims); ok {
  148. if sepIndex := strings.LastIndex(fid, "_"); sepIndex > 0 {
  149. fid = fid[:sepIndex]
  150. }
  151. return sc.Fid == vid+","+fid
  152. }
  153. glog.V(1).Infof("unexpected jwt from %s: %v", r.RemoteAddr, tokenStr)
  154. return false
  155. }