2013-12-02 09:37:36 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2022-05-20 10:18:20 +00:00
|
|
|
"fmt"
|
2013-12-02 09:37:36 +00:00
|
|
|
"net/http"
|
2021-03-30 09:10:50 +00:00
|
|
|
"strconv"
|
2019-03-03 18:17:44 +00:00
|
|
|
"strings"
|
2021-03-30 09:10:50 +00:00
|
|
|
"sync/atomic"
|
2022-05-20 10:18:20 +00:00
|
|
|
"time"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2020-10-31 23:31:39 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
2013-12-02 09:37:36 +00:00
|
|
|
)
|
|
|
|
|
2015-02-26 07:59:07 +00:00
|
|
|
/*
|
2013-12-02 09:37:36 +00:00
|
|
|
|
2015-03-09 08:10:01 +00: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 07:59:07 +00: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) {
|
2020-09-20 23:01:56 +00:00
|
|
|
w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
|
2020-10-31 23:31:39 +00:00
|
|
|
if r.Header.Get("Origin") != "" {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
|
|
}
|
2022-08-01 20:18:26 +00:00
|
|
|
start := time.Now()
|
2023-11-06 18:51:47 +00:00
|
|
|
requestMethod := r.Method
|
|
|
|
defer func(start time.Time, method *string) {
|
|
|
|
stats.VolumeServerRequestCounter.WithLabelValues(*method).Inc()
|
|
|
|
stats.VolumeServerRequestHistogram.WithLabelValues(*method).Observe(time.Since(start).Seconds())
|
|
|
|
}(start, &requestMethod)
|
2013-12-02 09:37:36 +00:00
|
|
|
switch r.Method {
|
2023-11-06 18:51:47 +00:00
|
|
|
case http.MethodGet, http.MethodHead:
|
2014-03-25 20:46:59 +00:00
|
|
|
stats.ReadRequest()
|
2021-08-09 06:25:16 +00:00
|
|
|
vs.inFlightDownloadDataLimitCond.L.Lock()
|
2022-08-25 03:03:34 +00:00
|
|
|
inFlightDownloadSize := atomic.LoadInt64(&vs.inFlightDownloadDataSize)
|
|
|
|
for vs.concurrentDownloadLimit != 0 && inFlightDownloadSize > vs.concurrentDownloadLimit {
|
2022-03-15 14:55:22 +00:00
|
|
|
select {
|
|
|
|
case <-r.Context().Done():
|
|
|
|
glog.V(4).Infof("request cancelled from %s: %v", r.RemoteAddr, r.Context().Err())
|
2022-09-07 06:56:29 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
vs.inFlightDownloadDataLimitCond.L.Unlock()
|
2022-03-15 14:55:22 +00:00
|
|
|
return
|
|
|
|
default:
|
2022-08-25 03:03:34 +00:00
|
|
|
glog.V(4).Infof("wait because inflight download data %d > %d", inFlightDownloadSize, vs.concurrentDownloadLimit)
|
2022-03-15 14:55:22 +00:00
|
|
|
vs.inFlightDownloadDataLimitCond.Wait()
|
|
|
|
}
|
2022-08-25 03:03:34 +00:00
|
|
|
inFlightDownloadSize = atomic.LoadInt64(&vs.inFlightDownloadDataSize)
|
2021-08-09 06:25:16 +00:00
|
|
|
}
|
2021-08-10 21:34:13 +00:00
|
|
|
vs.inFlightDownloadDataLimitCond.L.Unlock()
|
2014-05-27 04:15:05 +00:00
|
|
|
vs.GetOrHeadHandler(w, r)
|
2023-11-06 18:51:47 +00:00
|
|
|
case http.MethodDelete:
|
|
|
|
stats.VolumeServerRequestCounter.WithLabelValues(r.Method).Inc()
|
2014-03-25 20:46:59 +00:00
|
|
|
stats.DeleteRequest()
|
2015-02-26 07:59:07 +00:00
|
|
|
vs.guard.WhiteList(vs.DeleteHandler)(w, r)
|
2023-11-06 18:51:47 +00:00
|
|
|
case http.MethodPut, http.MethodPost:
|
|
|
|
stats.VolumeServerRequestCounter.WithLabelValues(r.Method).Inc()
|
2021-03-30 09:10:50 +00:00
|
|
|
contentLength := getContentLength(r)
|
2022-03-24 08:54:42 +00:00
|
|
|
// exclude the replication from the concurrentUploadLimitMB
|
2022-06-16 08:26:36 +00:00
|
|
|
if r.URL.Query().Get("type") != "replicate" && vs.concurrentUploadLimit != 0 {
|
2022-05-31 01:40:25 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
vs.inFlightUploadDataLimitCond.L.Lock()
|
2022-08-25 03:03:34 +00:00
|
|
|
inFlightUploadDataSize := atomic.LoadInt64(&vs.inFlightUploadDataSize)
|
|
|
|
for inFlightUploadDataSize > vs.concurrentUploadLimit {
|
2022-05-31 01:40:25 +00:00
|
|
|
//wait timeout check
|
2022-05-20 10:18:20 +00:00
|
|
|
if startTime.Add(vs.inflightUploadDataTimeout).Before(time.Now()) {
|
|
|
|
vs.inFlightUploadDataLimitCond.L.Unlock()
|
2022-08-25 03:03:34 +00:00
|
|
|
err := fmt.Errorf("reject because inflight upload data %d > %d, and wait timeout", inFlightUploadDataSize, vs.concurrentUploadLimit)
|
2022-05-20 10:18:20 +00:00
|
|
|
glog.V(1).Infof("too many requests: %v", err)
|
|
|
|
writeJsonError(w, r, http.StatusTooManyRequests, err)
|
|
|
|
return
|
|
|
|
}
|
2022-08-25 03:03:34 +00:00
|
|
|
glog.V(4).Infof("wait because inflight upload data %d > %d", inFlightUploadDataSize, vs.concurrentUploadLimit)
|
2022-05-20 06:33:47 +00:00
|
|
|
vs.inFlightUploadDataLimitCond.Wait()
|
2022-08-25 03:03:34 +00:00
|
|
|
inFlightUploadDataSize = atomic.LoadInt64(&vs.inFlightUploadDataSize)
|
2022-05-20 06:33:47 +00:00
|
|
|
}
|
2022-05-31 01:40:25 +00:00
|
|
|
vs.inFlightUploadDataLimitCond.L.Unlock()
|
2021-03-30 09:10:50 +00:00
|
|
|
}
|
2022-05-31 01:40:25 +00:00
|
|
|
atomic.AddInt64(&vs.inFlightUploadDataSize, contentLength)
|
2021-03-30 09:10:50 +00:00
|
|
|
defer func() {
|
2022-05-31 01:40:25 +00:00
|
|
|
atomic.AddInt64(&vs.inFlightUploadDataSize, -contentLength)
|
2022-06-16 08:26:36 +00:00
|
|
|
if vs.concurrentUploadLimit != 0 {
|
2022-06-16 06:07:11 +00:00
|
|
|
vs.inFlightUploadDataLimitCond.Signal()
|
|
|
|
}
|
2021-03-30 09:10:50 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// processs uploads
|
2014-03-25 20:46:59 +00:00
|
|
|
stats.WriteRequest()
|
2015-02-26 07:59:07 +00:00
|
|
|
vs.guard.WhiteList(vs.PostHandler)(w, r)
|
2021-03-30 09:10:50 +00:00
|
|
|
|
2023-11-06 18:51:47 +00:00
|
|
|
case http.MethodOptions:
|
2020-10-31 23:31:39 +00:00
|
|
|
stats.ReadRequest()
|
2020-10-31 23:44:03 +00:00
|
|
|
w.Header().Add("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")
|
|
|
|
w.Header().Add("Access-Control-Allow-Headers", "*")
|
2023-11-06 18:51:47 +00:00
|
|
|
default:
|
|
|
|
requestMethod = "INVALID"
|
|
|
|
writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("unsupported method %s", r.Method))
|
2013-12-02 09:37:36 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-14 07:13:18 +00:00
|
|
|
|
2021-03-30 09:10:50 +00: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 08:10:01 +00:00
|
|
|
func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) {
|
2020-09-20 23:01:56 +00:00
|
|
|
w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
|
2020-10-31 23:31:39 +00:00
|
|
|
if r.Header.Get("Origin") != "" {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
|
|
}
|
2015-02-26 07:59:07 +00:00
|
|
|
switch r.Method {
|
2023-11-06 18:51:47 +00:00
|
|
|
case http.MethodGet, http.MethodHead:
|
2015-02-26 07:59:07 +00:00
|
|
|
stats.ReadRequest()
|
2021-08-10 21:34:13 +00:00
|
|
|
vs.inFlightDownloadDataLimitCond.L.Lock()
|
2022-08-25 03:03:34 +00: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-10 21:34:13 +00:00
|
|
|
vs.inFlightDownloadDataLimitCond.Wait()
|
2022-08-25 03:03:34 +00:00
|
|
|
inFlightDownloadSize = atomic.LoadInt64(&vs.inFlightDownloadDataSize)
|
2021-08-10 21:34:13 +00:00
|
|
|
}
|
|
|
|
vs.inFlightDownloadDataLimitCond.L.Unlock()
|
2015-02-26 07:59:07 +00:00
|
|
|
vs.GetOrHeadHandler(w, r)
|
2023-11-06 18:51:47 +00:00
|
|
|
case http.MethodOptions:
|
2020-10-31 23:31:39 +00:00
|
|
|
stats.ReadRequest()
|
2020-10-31 23:44:03 +00:00
|
|
|
w.Header().Add("Access-Control-Allow-Methods", "GET, OPTIONS")
|
|
|
|
w.Header().Add("Access-Control-Allow-Headers", "*")
|
2014-04-14 08:00:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-14 08:08:20 +00:00
|
|
|
|
2019-06-06 07:29:02 +00:00
|
|
|
func (vs *VolumeServer) maybeCheckJwtAuthorization(r *http.Request, vid, fid string, isWrite bool) bool {
|
2019-02-14 08:08:20 +00:00
|
|
|
|
2019-06-06 07:29:02 +00:00
|
|
|
var signingKey security.SigningKey
|
|
|
|
|
|
|
|
if isWrite {
|
|
|
|
if len(vs.guard.SigningKey) == 0 {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
signingKey = vs.guard.SigningKey
|
|
|
|
}
|
2019-06-11 04:33:32 +00:00
|
|
|
} else {
|
2019-06-06 07:29:02 +00:00
|
|
|
if len(vs.guard.ReadSigningKey) == 0 {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
signingKey = vs.guard.ReadSigningKey
|
|
|
|
}
|
2019-02-14 08:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tokenStr := security.GetJwt(r)
|
|
|
|
if tokenStr == "" {
|
|
|
|
glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-29 11:40:41 +00:00
|
|
|
token, err := security.DecodeJwt(signingKey, tokenStr, &security.SeaweedFileIdClaims{})
|
2019-02-14 08:08:20 +00: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-03 18:17:44 +00:00
|
|
|
if sepIndex := strings.LastIndex(fid, "_"); sepIndex > 0 {
|
|
|
|
fid = fid[:sepIndex]
|
|
|
|
}
|
2019-02-14 08:08:20 +00:00
|
|
|
return sc.Fid == vid+","+fid
|
|
|
|
}
|
|
|
|
glog.V(1).Infof("unexpected jwt from %s: %v", r.RemoteAddr, tokenStr)
|
|
|
|
return false
|
|
|
|
}
|