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.

147 lines
3.8 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. "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/security"
  11. "github.com/chrislusf/seaweedfs/weed/stats"
  12. )
  13. /*
  14. If volume server is started with a separated public port, the public port will
  15. be more "secure".
  16. Public port currently only supports reads.
  17. Later writes on public port can have one of the 3
  18. security settings:
  19. 1. not secured
  20. 2. secured by white list
  21. 3. secured by JWT(Json Web Token)
  22. */
  23. func (vs *VolumeServer) privateStoreHandler(w http.ResponseWriter, r *http.Request) {
  24. w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
  25. if r.Header.Get("Origin") != "" {
  26. w.Header().Set("Access-Control-Allow-Origin", "*")
  27. w.Header().Set("Access-Control-Allow-Credentials", "true")
  28. }
  29. switch r.Method {
  30. case "GET", "HEAD":
  31. stats.ReadRequest()
  32. vs.GetOrHeadHandler(w, r)
  33. case "DELETE":
  34. stats.DeleteRequest()
  35. vs.guard.WhiteList(vs.DeleteHandler)(w, r)
  36. case "PUT", "POST":
  37. // wait until in flight data is less than the limit
  38. contentLength := getContentLength(r)
  39. vs.inFlightDataLimitCond.L.Lock()
  40. for vs.concurrentUploadLimit != 0 && atomic.LoadInt64(&vs.inFlightDataSize) > vs.concurrentUploadLimit {
  41. glog.V(4).Infof("wait because inflight data %d > %d", vs.inFlightDataSize, vs.concurrentUploadLimit)
  42. vs.inFlightDataLimitCond.Wait()
  43. }
  44. atomic.AddInt64(&vs.inFlightDataSize, contentLength)
  45. vs.inFlightDataLimitCond.L.Unlock()
  46. defer func() {
  47. atomic.AddInt64(&vs.inFlightDataSize, -contentLength)
  48. vs.inFlightDataLimitCond.Signal()
  49. }()
  50. // processs uploads
  51. stats.WriteRequest()
  52. vs.guard.WhiteList(vs.PostHandler)(w, r)
  53. case "OPTIONS":
  54. stats.ReadRequest()
  55. w.Header().Add("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")
  56. w.Header().Add("Access-Control-Allow-Headers", "*")
  57. }
  58. }
  59. func getContentLength(r *http.Request) int64 {
  60. contentLength := r.Header.Get("Content-Length")
  61. if contentLength != "" {
  62. length, err := strconv.ParseInt(contentLength, 10, 64)
  63. if err != nil {
  64. return 0
  65. }
  66. return length
  67. }
  68. return 0
  69. }
  70. func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) {
  71. fmt.Printf("publicReadOnlyHandler in.")
  72. w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
  73. if r.Header.Get("Origin") != "" {
  74. w.Header().Set("Access-Control-Allow-Origin", "*")
  75. w.Header().Set("Access-Control-Allow-Credentials", "true")
  76. }
  77. switch r.Method {
  78. case "GET":
  79. stats.ReadRequest()
  80. vs.GetOrHeadHandler(w, r)
  81. case "HEAD":
  82. stats.ReadRequest()
  83. vs.GetOrHeadHandler(w, r)
  84. case "OPTIONS":
  85. stats.ReadRequest()
  86. w.Header().Add("Access-Control-Allow-Methods", "GET, OPTIONS")
  87. w.Header().Add("Access-Control-Allow-Headers", "*")
  88. }
  89. }
  90. func (vs *VolumeServer) maybeCheckJwtAuthorization(r *http.Request, vid, fid string, isWrite bool) bool {
  91. var signingKey security.SigningKey
  92. if isWrite {
  93. if len(vs.guard.SigningKey) == 0 {
  94. return true
  95. } else {
  96. signingKey = vs.guard.SigningKey
  97. }
  98. } else {
  99. if len(vs.guard.ReadSigningKey) == 0 {
  100. return true
  101. } else {
  102. signingKey = vs.guard.ReadSigningKey
  103. }
  104. }
  105. tokenStr := security.GetJwt(r)
  106. if tokenStr == "" {
  107. glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
  108. return false
  109. }
  110. token, err := security.DecodeJwt(signingKey, tokenStr)
  111. if err != nil {
  112. glog.V(1).Infof("jwt verification error from %s: %v", r.RemoteAddr, err)
  113. return false
  114. }
  115. if !token.Valid {
  116. glog.V(1).Infof("jwt invalid from %s: %v", r.RemoteAddr, tokenStr)
  117. return false
  118. }
  119. if sc, ok := token.Claims.(*security.SeaweedFileIdClaims); ok {
  120. if sepIndex := strings.LastIndex(fid, "_"); sepIndex > 0 {
  121. fid = fid[:sepIndex]
  122. }
  123. return sc.Fid == vid+","+fid
  124. }
  125. glog.V(1).Infof("unexpected jwt from %s: %v", r.RemoteAddr, tokenStr)
  126. return false
  127. }