2021-05-21 20:34:17 +00:00
|
|
|
package node
|
2019-07-18 23:18:26 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-17 14:23:08 +00:00
|
|
|
"context"
|
2019-10-23 09:18:22 +00:00
|
|
|
"encoding/json"
|
2020-10-21 08:37:50 +00:00
|
|
|
"net"
|
2019-07-18 23:18:26 +00:00
|
|
|
"net/http"
|
2019-09-30 23:25:45 +00:00
|
|
|
_ "net/http/pprof"
|
2020-10-02 16:42:44 +00:00
|
|
|
"runtime"
|
2021-05-21 20:34:17 +00:00
|
|
|
"strconv"
|
2019-07-18 23:18:26 +00:00
|
|
|
|
2019-10-22 17:18:06 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-10-22 17:18:06 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2020-08-25 23:46:31 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2020-10-21 08:37:50 +00:00
|
|
|
"go.opencensus.io/tag"
|
2019-10-22 17:18:06 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-02-23 22:35:29 +00:00
|
|
|
|
2020-05-20 17:43:22 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
2020-05-20 18:23:51 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc/auth"
|
2020-05-20 17:43:22 +00:00
|
|
|
|
2021-03-25 14:09:50 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2021-03-23 15:40:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v0api"
|
|
|
|
"github.com/filecoin-project/lotus/api/v1api"
|
2020-10-21 08:10:27 +00:00
|
|
|
"github.com/filecoin-project/lotus/metrics"
|
2020-05-20 17:43:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/impl"
|
2019-07-18 23:18:26 +00:00
|
|
|
)
|
|
|
|
|
2021-05-21 20:34:17 +00:00
|
|
|
var rpclog = logging.Logger("rpc")
|
2019-09-17 14:23:08 +00:00
|
|
|
|
2021-05-23 11:40:33 +00:00
|
|
|
// 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.
|
2021-05-21 20:34:17 +00:00
|
|
|
func ServeRPC(a v1api.FullNode, addr multiaddr.Multiaddr, maxRequestSize int64) (StopFunc, error) {
|
2020-11-17 13:39:53 +00:00
|
|
|
serverOptions := make([]jsonrpc.ServerOption, 0)
|
|
|
|
if maxRequestSize != 0 { // config set
|
|
|
|
serverOptions = append(serverOptions, jsonrpc.WithMaxRequestSize(maxRequestSize))
|
|
|
|
}
|
2021-03-23 15:40:22 +00:00
|
|
|
serveRpc := func(path string, hnd interface{}) {
|
|
|
|
rpcServer := jsonrpc.NewServer(serverOptions...)
|
|
|
|
rpcServer.Register("Filecoin", hnd)
|
2019-07-24 13:40:42 +00:00
|
|
|
|
2021-03-23 15:40:22 +00:00
|
|
|
ah := &auth.Handler{
|
|
|
|
Verify: a.AuthVerify,
|
|
|
|
Next: rpcServer.ServeHTTP,
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Handle(path, ah)
|
2019-07-24 13:40:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 14:09:50 +00:00
|
|
|
pma := api.PermissionedFullAPI(metrics.MetricedFullAPI(a))
|
2021-03-23 15:40:22 +00:00
|
|
|
|
|
|
|
serveRpc("/rpc/v1", pma)
|
2021-03-23 16:20:56 +00:00
|
|
|
serveRpc("/rpc/v0", &v0api.WrapperV1Full{FullNode: pma})
|
2019-09-17 14:23:08 +00:00
|
|
|
|
2019-10-23 09:18:22 +00:00
|
|
|
importAH := &auth.Handler{
|
|
|
|
Verify: a.AuthVerify,
|
|
|
|
Next: handleImport(a.(*impl.FullNodeAPI)),
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Handle("/rest/v0/import", importAH)
|
|
|
|
|
2021-02-21 10:03:00 +00:00
|
|
|
http.Handle("/debug/metrics", metrics.Exporter())
|
2020-10-02 16:42:44 +00:00
|
|
|
http.Handle("/debug/pprof-set/block", handleFractionOpt("BlockProfileRate", runtime.SetBlockProfileRate))
|
2021-05-21 20:34:17 +00:00
|
|
|
http.Handle("/debug/pprof-set/mutex", handleFractionOpt("MutexProfileFraction", func(x int) {
|
|
|
|
runtime.SetMutexProfileFraction(x)
|
|
|
|
}))
|
2020-02-23 22:54:49 +00:00
|
|
|
|
2021-05-21 20:34:17 +00:00
|
|
|
// Start listening to the addr; if invalid or occupied, we will fail early.
|
2019-10-22 17:18:06 +00:00
|
|
|
lst, err := manet.Listen(addr)
|
|
|
|
if err != nil {
|
2021-05-21 20:34:17 +00:00
|
|
|
return nil, xerrors.Errorf("could not listen: %w", err)
|
2019-10-22 17:18:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 20:34:17 +00:00
|
|
|
// Instantiate the server and start listening.
|
2020-10-21 08:37:50 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
2019-09-17 14:23:08 +00:00
|
|
|
|
|
|
|
go func() {
|
2021-05-21 20:34:17 +00:00
|
|
|
err = srv.Serve(manet.NetListener(lst))
|
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
log.Warnf("rpc server failed: %s", err)
|
2020-06-02 18:54:24 +00:00
|
|
|
}
|
2019-09-17 14:23:08 +00:00
|
|
|
}()
|
|
|
|
|
2021-05-21 20:34:17 +00:00
|
|
|
return srv.Shutdown, err
|
2019-07-18 23:18:26 +00:00
|
|
|
}
|
2019-10-23 09:18:22 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-03-25 14:09:50 +00:00
|
|
|
if !auth.HasPerm(r.Context(), nil, api.PermWrite) {
|
2019-10-23 09:18:22 +00:00
|
|
|
w.WriteHeader(401)
|
2020-05-27 20:53:20 +00:00
|
|
|
_ = json.NewEncoder(w).Encode(struct{ Error string }{"unauthorized: missing write permission"})
|
2019-10-23 09:18:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := a.ClientImportLocal(r.Context(), r.Body)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
2020-05-27 20:53:20 +00:00
|
|
|
_ = json.NewEncoder(w).Encode(struct{ Error string }{err.Error()})
|
2019-10-23 09:18:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 20:34:17 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|