2013-12-02 17:37:36 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2022-05-20 18:18:20 +08:00
|
|
|
"fmt"
|
2013-12-02 17:37:36 +08:00
|
|
|
"net/http"
|
2021-03-30 17:10:50 +08:00
|
|
|
"strconv"
|
2019-03-04 02:17:44 +08:00
|
|
|
"strings"
|
2021-03-30 17:10:50 +08:00
|
|
|
"sync/atomic"
|
2022-05-20 18:18:20 +08:00
|
|
|
"time"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2020-11-01 07:31:39 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
2013-12-02 17:37:36 +08:00
|
|
|
)
|
|
|
|
|
2015-02-26 15:59:07 +08:00
|
|
|
/*
|
2013-12-02 17:37:36 +08:00
|
|
|
|
2015-03-09 16:10:01 +08:00
|
|
|
If volume server is started with a separated public port, the public port will
|
|
|
|
be more "secure".
|
|
|
|
|
|
|
|
Public port currently only supports reads.
|
|
|
|
|
|
|
|
Later writes on public port can have one of the 3
|
2015-02-26 15:59:07 +08:00
|
|
|
security settings:
|
|
|
|
1. not secured
|
|
|
|
2. secured by white list
|
|
|
|
3. secured by JWT(Json Web Token)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
func (vs *VolumeServer) privateStoreHandler(w http.ResponseWriter, r *http.Request) {
|
2024-01-11 02:05:27 +08:00
|
|
|
statusRecorder := stats.NewStatusResponseWriter(w)
|
|
|
|
w = statusRecorder
|
2020-09-21 07:01:56 +08:00
|
|
|
w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
|
2020-11-01 07:31:39 +08:00
|
|
|
if r.Header.Get("Origin") != "" {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
|
|
}
|
2022-08-02 04:18:26 +08:00
|
|
|
start := time.Now()
|
2023-11-07 02:51:47 +08:00
|
|
|
requestMethod := r.Method
|
2024-01-11 02:05:27 +08:00
|
|
|
defer func(start time.Time, method *string, statusRecorder *stats.StatusRecorder) {
|
|
|
|
stats.VolumeServerRequestCounter.WithLabelValues(*method, strconv.Itoa(statusRecorder.Status)).Inc()
|
2023-11-07 02:51:47 +08:00
|
|
|
stats.VolumeServerRequestHistogram.WithLabelValues(*method).Observe(time.Since(start).Seconds())
|
2024-01-11 02:05:27 +08:00
|
|
|
}(start, &requestMethod, statusRecorder)
|
2013-12-02 17:37:36 +08:00
|
|
|
switch r.Method {
|
2023-11-07 02:51:47 +08:00
|
|
|
case http.MethodGet, http.MethodHead:
|
2014-03-26 04:46:59 +08:00
|
|
|
stats.ReadRequest()
|
2021-08-09 14:25:16 +08:00
|
|
|
vs.inFlightDownloadDataLimitCond.L.Lock()
|
2022-08-25 11:03:34 +08:00
|
|
|
inFlightDownloadSize := atomic.LoadInt64(&vs.inFlightDownloadDataSize)
|
|
|
|
for vs.concurrentDownloadLimit != 0 && inFlightDownloadSize > vs.concurrentDownloadLimit {
|
2022-03-15 22:55:22 +08:00
|
|
|
select {
|
|
|
|
case <-r.Context().Done():
|
|
|
|
glog.V(4).Infof("request cancelled from %s: %v", r.RemoteAddr, r.Context().Err())
|
2024-02-15 06:18:56 +08:00
|
|
|
w.WriteHeader(util.HttpStatusCancelled)
|
2022-09-07 14:56:29 +08:00
|
|
|
vs.inFlightDownloadDataLimitCond.L.Unlock()
|
2022-03-15 22:55:22 +08:00
|
|
|
return
|
|
|
|
default:
|
2022-08-25 11:03:34 +08:00
|
|
|
glog.V(4).Infof("wait because inflight download data %d > %d", inFlightDownloadSize, vs.concurrentDownloadLimit)
|
2022-03-15 22:55:22 +08:00
|
|
|
vs.inFlightDownloadDataLimitCond.Wait()
|
|
|
|
}
|
2022-08-25 11:03:34 +08:00
|
|
|
inFlightDownloadSize = atomic.LoadInt64(&vs.inFlightDownloadDataSize)
|
2021-08-09 14:25:16 +08:00
|
|
|
}
|
2021-08-11 05:34:13 +08:00
|
|
|
vs.inFlightDownloadDataLimitCond.L.Unlock()
|
2014-05-27 12:15:05 +08:00
|
|
|
vs.GetOrHeadHandler(w, r)
|
2023-11-07 02:51:47 +08:00
|
|
|
case http.MethodDelete:
|
2014-03-26 04:46:59 +08:00
|
|
|
stats.DeleteRequest()
|
2015-02-26 15:59:07 +08:00
|
|
|
vs.guard.WhiteList(vs.DeleteHandler)(w, r)
|
2023-11-07 02:51:47 +08:00
|
|
|
case http.MethodPut, http.MethodPost:
|
2021-03-30 17:10:50 +08:00
|
|
|
contentLength := getContentLength(r)
|
2022-03-24 16:54:42 +08:00
|
|
|
// exclude the replication from the concurrentUploadLimitMB
|
2022-06-16 16:26:36 +08:00
|
|
|
if r.URL.Query().Get("type") != "replicate" && vs.concurrentUploadLimit != 0 {
|
2022-05-31 09:40:25 +08:00
|
|
|
startTime := time.Now()
|
|
|
|
vs.inFlightUploadDataLimitCond.L.Lock()
|
2022-08-25 11:03:34 +08:00
|
|
|
inFlightUploadDataSize := atomic.LoadInt64(&vs.inFlightUploadDataSize)
|
|
|
|
for inFlightUploadDataSize > vs.concurrentUploadLimit {
|
2022-05-31 09:40:25 +08:00
|
|
|
//wait timeout check
|
2022-05-20 18:18:20 +08:00
|
|
|
if startTime.Add(vs.inflightUploadDataTimeout).Before(time.Now()) {
|
|
|
|
vs.inFlightUploadDataLimitCond.L.Unlock()
|
2022-08-25 11:03:34 +08:00
|
|
|
err := fmt.Errorf("reject because inflight upload data %d > %d, and wait timeout", inFlightUploadDataSize, vs.concurrentUploadLimit)
|
2022-05-20 18:18:20 +08:00
|
|
|
glog.V(1).Infof("too many requests: %v", err)
|
|
|
|
writeJsonError(w, r, http.StatusTooManyRequests, err)
|
|
|
|
return
|
|
|
|
}
|
2022-08-25 11:03:34 +08:00
|
|
|
glog.V(4).Infof("wait because inflight upload data %d > %d", inFlightUploadDataSize, vs.concurrentUploadLimit)
|
2022-05-20 14:33:47 +08:00
|
|
|
vs.inFlightUploadDataLimitCond.Wait()
|
2022-08-25 11:03:34 +08:00
|
|
|
inFlightUploadDataSize = atomic.LoadInt64(&vs.inFlightUploadDataSize)
|
2022-05-20 14:33:47 +08:00
|
|
|
}
|
2022-05-31 09:40:25 +08:00
|
|
|
vs.inFlightUploadDataLimitCond.L.Unlock()
|
2021-03-30 17:10:50 +08:00
|
|
|
}
|
2022-05-31 09:40:25 +08:00
|
|
|
atomic.AddInt64(&vs.inFlightUploadDataSize, contentLength)
|
2021-03-30 17:10:50 +08:00
|
|
|
defer func() {
|
2022-05-31 09:40:25 +08:00
|
|
|
atomic.AddInt64(&vs.inFlightUploadDataSize, -contentLength)
|
2022-06-16 16:26:36 +08:00
|
|
|
if vs.concurrentUploadLimit != 0 {
|
2022-06-16 14:07:11 +08:00
|
|
|
vs.inFlightUploadDataLimitCond.Signal()
|
|
|
|
}
|
2021-03-30 17:10:50 +08:00
|
|
|
}()
|
|
|
|
|
2024-04-14 22:42:40 +08:00
|
|
|
// processes uploads
|
2014-03-26 04:46:59 +08:00
|
|
|
stats.WriteRequest()
|
2015-02-26 15:59:07 +08:00
|
|
|
vs.guard.WhiteList(vs.PostHandler)(w, r)
|
2021-03-30 17:10:50 +08:00
|
|
|
|
2023-11-07 02:51:47 +08:00
|
|
|
case http.MethodOptions:
|
2020-11-01 07:31:39 +08:00
|
|
|
stats.ReadRequest()
|
2020-11-01 07:44:03 +08:00
|
|
|
w.Header().Add("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")
|
|
|
|
w.Header().Add("Access-Control-Allow-Headers", "*")
|
2023-11-07 02:51:47 +08:00
|
|
|
default:
|
|
|
|
requestMethod = "INVALID"
|
|
|
|
writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("unsupported method %s", r.Method))
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
}
|
2014-04-14 15:13:18 +08:00
|
|
|
|
2021-03-30 17:10:50 +08:00
|
|
|
func getContentLength(r *http.Request) int64 {
|
|
|
|
contentLength := r.Header.Get("Content-Length")
|
|
|
|
if contentLength != "" {
|
|
|
|
length, err := strconv.ParseInt(contentLength, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return length
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-03-09 16:10:01 +08:00
|
|
|
func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) {
|
2024-01-11 02:05:27 +08:00
|
|
|
statusRecorder := stats.NewStatusResponseWriter(w)
|
|
|
|
w = statusRecorder
|
2020-09-21 07:01:56 +08:00
|
|
|
w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
|
2020-11-01 07:31:39 +08:00
|
|
|
if r.Header.Get("Origin") != "" {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
|
|
}
|
2024-01-11 02:05:27 +08:00
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
requestMethod := r.Method
|
|
|
|
defer func(start time.Time, method *string, statusRecorder *stats.StatusRecorder) {
|
|
|
|
stats.VolumeServerRequestCounter.WithLabelValues(*method, strconv.Itoa(statusRecorder.Status)).Inc()
|
|
|
|
stats.VolumeServerRequestHistogram.WithLabelValues(*method).Observe(time.Since(start).Seconds())
|
|
|
|
}(start, &requestMethod, statusRecorder)
|
|
|
|
|
2015-02-26 15:59:07 +08:00
|
|
|
switch r.Method {
|
2023-11-07 02:51:47 +08:00
|
|
|
case http.MethodGet, http.MethodHead:
|
2015-02-26 15:59:07 +08:00
|
|
|
stats.ReadRequest()
|
2021-08-11 05:34:13 +08:00
|
|
|
vs.inFlightDownloadDataLimitCond.L.Lock()
|
2022-08-25 11:03:34 +08:00
|
|
|
inFlightDownloadSize := atomic.LoadInt64(&vs.inFlightDownloadDataSize)
|
|
|
|
for vs.concurrentDownloadLimit != 0 && inFlightDownloadSize > vs.concurrentDownloadLimit {
|
|
|
|
glog.V(4).Infof("wait because inflight download data %d > %d", inFlightDownloadSize, vs.concurrentDownloadLimit)
|
2021-08-11 05:34:13 +08:00
|
|
|
vs.inFlightDownloadDataLimitCond.Wait()
|
2022-08-25 11:03:34 +08:00
|
|
|
inFlightDownloadSize = atomic.LoadInt64(&vs.inFlightDownloadDataSize)
|
2021-08-11 05:34:13 +08:00
|
|
|
}
|
|
|
|
vs.inFlightDownloadDataLimitCond.L.Unlock()
|
2015-02-26 15:59:07 +08:00
|
|
|
vs.GetOrHeadHandler(w, r)
|
2023-11-07 02:51:47 +08:00
|
|
|
case http.MethodOptions:
|
2020-11-01 07:31:39 +08:00
|
|
|
stats.ReadRequest()
|
2020-11-01 07:44:03 +08:00
|
|
|
w.Header().Add("Access-Control-Allow-Methods", "GET, OPTIONS")
|
|
|
|
w.Header().Add("Access-Control-Allow-Headers", "*")
|
2014-04-14 16:00:09 +08:00
|
|
|
}
|
|
|
|
}
|
2019-02-14 16:08:20 +08:00
|
|
|
|
2019-06-06 15:29:02 +08:00
|
|
|
func (vs *VolumeServer) maybeCheckJwtAuthorization(r *http.Request, vid, fid string, isWrite bool) bool {
|
2019-02-14 16:08:20 +08:00
|
|
|
|
2019-06-06 15:29:02 +08:00
|
|
|
var signingKey security.SigningKey
|
|
|
|
|
|
|
|
if isWrite {
|
|
|
|
if len(vs.guard.SigningKey) == 0 {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
signingKey = vs.guard.SigningKey
|
|
|
|
}
|
2019-06-11 12:33:32 +08:00
|
|
|
} else {
|
2019-06-06 15:29:02 +08:00
|
|
|
if len(vs.guard.ReadSigningKey) == 0 {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
signingKey = vs.guard.ReadSigningKey
|
|
|
|
}
|
2019-02-14 16:08:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tokenStr := security.GetJwt(r)
|
|
|
|
if tokenStr == "" {
|
|
|
|
glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:40:41 +08:00
|
|
|
token, err := security.DecodeJwt(signingKey, tokenStr, &security.SeaweedFileIdClaims{})
|
2019-02-14 16:08:20 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infof("jwt verification error from %s: %v", r.RemoteAddr, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !token.Valid {
|
|
|
|
glog.V(1).Infof("jwt invalid from %s: %v", r.RemoteAddr, tokenStr)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if sc, ok := token.Claims.(*security.SeaweedFileIdClaims); ok {
|
2019-03-04 02:17:44 +08:00
|
|
|
if sepIndex := strings.LastIndex(fid, "_"); sepIndex > 0 {
|
|
|
|
fid = fid[:sepIndex]
|
|
|
|
}
|
2019-02-14 16:08:20 +08:00
|
|
|
return sc.Fid == vid+","+fid
|
|
|
|
}
|
|
|
|
glog.V(1).Infof("unexpected jwt from %s: %v", r.RemoteAddr, tokenStr)
|
|
|
|
return false
|
|
|
|
}
|