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.

145 lines
3.7 KiB

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