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.

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