2013-12-02 09:37:36 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2020-10-31 23:31:39 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
|
2019-02-14 08:08:20 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/security"
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/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")
|
|
|
|
}
|
2013-12-02 09:37:36 +00:00
|
|
|
switch r.Method {
|
2018-07-22 17:27:10 +00:00
|
|
|
case "GET", "HEAD":
|
2014-03-25 20:46:59 +00:00
|
|
|
stats.ReadRequest()
|
2014-05-27 04:15:05 +00:00
|
|
|
vs.GetOrHeadHandler(w, r)
|
2013-12-02 09:37:36 +00:00
|
|
|
case "DELETE":
|
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)
|
2018-07-22 17:27:10 +00:00
|
|
|
case "PUT", "POST":
|
2021-03-30 09:10:50 +00:00
|
|
|
|
|
|
|
// wait until in flight data is less than the limit
|
|
|
|
contentLength := getContentLength(r)
|
|
|
|
vs.inFlightDataLimitCond.L.Lock()
|
|
|
|
for atomic.LoadInt64(&vs.inFlightDataSize) > vs.concurrentUploadLimit {
|
|
|
|
vs.inFlightDataLimitCond.Wait()
|
|
|
|
}
|
|
|
|
atomic.AddInt64(&vs.inFlightDataSize, contentLength)
|
|
|
|
vs.inFlightDataLimitCond.L.Unlock()
|
|
|
|
defer func() {
|
|
|
|
atomic.AddInt64(&vs.inFlightDataSize, -contentLength)
|
|
|
|
vs.inFlightDataLimitCond.Signal()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-10-31 23:31:39 +00:00
|
|
|
case "OPTIONS":
|
|
|
|
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", "*")
|
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 {
|
|
|
|
case "GET":
|
|
|
|
stats.ReadRequest()
|
|
|
|
vs.GetOrHeadHandler(w, r)
|
|
|
|
case "HEAD":
|
|
|
|
stats.ReadRequest()
|
|
|
|
vs.GetOrHeadHandler(w, r)
|
2020-10-31 23:31:39 +00:00
|
|
|
case "OPTIONS":
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-06 07:29:02 +00:00
|
|
|
token, err := security.DecodeJwt(signingKey, tokenStr)
|
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
|
|
|
|
}
|