volume_server_handlers.go 4.5 KB

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