149 lines
4.0 KiB
Go
149 lines
4.0 KiB
Go
package node
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net"
|
|
"net/http"
|
|
_ "net/http/pprof"
|
|
"runtime"
|
|
"strconv"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
logging "github.com/ipfs/go-log/v2"
|
|
"github.com/multiformats/go-multiaddr"
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
|
"go.opencensus.io/tag"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
|
"github.com/filecoin-project/go-jsonrpc/auth"
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
"github.com/filecoin-project/lotus/api/v0api"
|
|
"github.com/filecoin-project/lotus/api/v1api"
|
|
"github.com/filecoin-project/lotus/metrics"
|
|
"github.com/filecoin-project/lotus/node/impl"
|
|
)
|
|
|
|
var rpclog = logging.Logger("rpc")
|
|
|
|
// ServeRPC serves the full node API over the supplied listen multiaddr.
|
|
//
|
|
// It returns the stop function to be called to terminate the endpoint.
|
|
//
|
|
// This function spawns a goroutine to run the server, and returns immediately.
|
|
func ServeRPC(a v1api.FullNode, addr multiaddr.Multiaddr, maxRequestSize int64) (StopFunc, error) {
|
|
serverOptions := make([]jsonrpc.ServerOption, 0)
|
|
if maxRequestSize != 0 { // config set
|
|
serverOptions = append(serverOptions, jsonrpc.WithMaxRequestSize(maxRequestSize))
|
|
}
|
|
serveRpc := func(path string, hnd interface{}) {
|
|
rpcServer := jsonrpc.NewServer(serverOptions...)
|
|
rpcServer.Register("Filecoin", hnd)
|
|
|
|
ah := &auth.Handler{
|
|
Verify: a.AuthVerify,
|
|
Next: rpcServer.ServeHTTP,
|
|
}
|
|
|
|
http.Handle(path, ah)
|
|
}
|
|
|
|
pma := api.PermissionedFullAPI(metrics.MetricedFullAPI(a))
|
|
|
|
serveRpc("/rpc/v1", pma)
|
|
serveRpc("/rpc/v0", &v0api.WrapperV1Full{FullNode: pma})
|
|
|
|
importAH := &auth.Handler{
|
|
Verify: a.AuthVerify,
|
|
Next: handleImport(a.(*impl.FullNodeAPI)),
|
|
}
|
|
|
|
http.Handle("/rest/v0/import", importAH)
|
|
|
|
http.Handle("/debug/metrics", metrics.Exporter())
|
|
http.Handle("/debug/pprof-set/block", handleFractionOpt("BlockProfileRate", runtime.SetBlockProfileRate))
|
|
http.Handle("/debug/pprof-set/mutex", handleFractionOpt("MutexProfileFraction", func(x int) {
|
|
runtime.SetMutexProfileFraction(x)
|
|
}))
|
|
|
|
// Start listening to the addr; if invalid or occupied, we will fail early.
|
|
lst, err := manet.Listen(addr)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("could not listen: %w", err)
|
|
}
|
|
|
|
// Instantiate the server and start listening.
|
|
srv := &http.Server{
|
|
Handler: http.DefaultServeMux,
|
|
BaseContext: func(listener net.Listener) context.Context {
|
|
ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-daemon"))
|
|
return ctx
|
|
},
|
|
}
|
|
|
|
go func() {
|
|
err = srv.Serve(manet.NetListener(lst))
|
|
if err != http.ErrServerClosed {
|
|
log.Warnf("rpc server failed: %s", err)
|
|
}
|
|
}()
|
|
|
|
return srv.Shutdown, err
|
|
}
|
|
|
|
func handleImport(a *impl.FullNodeAPI) func(w http.ResponseWriter, r *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "PUT" {
|
|
w.WriteHeader(404)
|
|
return
|
|
}
|
|
if !auth.HasPerm(r.Context(), nil, api.PermWrite) {
|
|
w.WriteHeader(401)
|
|
_ = json.NewEncoder(w).Encode(struct{ Error string }{"unauthorized: missing write permission"})
|
|
return
|
|
}
|
|
|
|
c, err := a.ClientImportLocal(r.Context(), r.Body)
|
|
if err != nil {
|
|
w.WriteHeader(500)
|
|
_ = json.NewEncoder(w).Encode(struct{ Error string }{err.Error()})
|
|
return
|
|
}
|
|
w.WriteHeader(200)
|
|
err = json.NewEncoder(w).Encode(struct{ Cid cid.Cid }{c})
|
|
if err != nil {
|
|
log.Errorf("/rest/v0/import: Writing response failed: %+v", err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func handleFractionOpt(name string, setter func(int)) http.HandlerFunc {
|
|
return func(rw http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(rw, "only POST allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
asfr := r.Form.Get("x")
|
|
if len(asfr) == 0 {
|
|
http.Error(rw, "parameter 'x' must be set", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
fr, err := strconv.Atoi(asfr)
|
|
if err != nil {
|
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
log.Infof("setting %s to %d", name, fr)
|
|
setter(fr)
|
|
}
|
|
}
|