package main import ( "context" "fmt" "net" "net/http" "os" "contrib.go.opencensus.io/exporter/prometheus" "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/lotus/gateway" "github.com/gorilla/mux" logging "github.com/ipfs/go-log/v2" promclient "github.com/prometheus/client_golang/prometheus" "github.com/urfave/cli/v2" "go.opencensus.io/stats/view" "go.opencensus.io/tag" "github.com/filecoin-project/go-address" "github.com/filecoin-project/go-jsonrpc" lapi "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api/client" "github.com/filecoin-project/lotus/api/v0api" "github.com/filecoin-project/lotus/api/v1api" "github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/chain/types" lcli "github.com/filecoin-project/lotus/cli" cliutil "github.com/filecoin-project/lotus/cli/util" "github.com/filecoin-project/lotus/lib/lotuslog" "github.com/filecoin-project/lotus/metrics" ) var log = logging.Logger("gateway") func main() { lotuslog.SetupLogLevels() local := []*cli.Command{ runCmd, checkCmd, } app := &cli.App{ Name: "lotus-gateway", Usage: "Public API server for lotus", Version: build.UserVersion(), Flags: []cli.Flag{ &cli.StringFlag{ Name: "repo", EnvVars: []string{"LOTUS_PATH"}, Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME }, }, Commands: local, } app.Setup() if err := app.Run(os.Args); err != nil { log.Errorf("%+v", err) os.Exit(1) return } } var checkCmd = &cli.Command{ Name: "check", Usage: "performs a simple check to verify that a connection can be made to a gateway", ArgsUsage: "[apiInfo]", Description: `Any valid value for FULLNODE_API_INFO is a valid argument to the check command. Examples - ws://127.0.0.1:2346 - http://127.0.0.1:2346 - /ip4/127.0.0.1/tcp/2346`, Flags: []cli.Flag{}, Action: func(cctx *cli.Context) error { ctx := lcli.ReqContext(cctx) ctx, cancel := context.WithCancel(ctx) defer cancel() ainfo := cliutil.ParseApiInfo(cctx.Args().First()) darg, err := ainfo.DialArgs("v1") if err != nil { return err } api, closer, err := client.NewFullNodeRPCV1(ctx, darg, nil) if err != nil { return err } defer closer() addr, err := address.NewIDAddress(100) if err != nil { return err } laddr, err := api.StateLookupID(ctx, addr, types.EmptyTSK) if err != nil { return err } if laddr != addr { return fmt.Errorf("looked up addresses does not match returned address, %s != %s", addr, laddr) } return nil }, } var runCmd = &cli.Command{ Name: "run", Usage: "Start api server", Flags: []cli.Flag{ &cli.StringFlag{ Name: "listen", Usage: "host address and port the api server will listen on", Value: "0.0.0.0:2346", }, &cli.IntFlag{ Name: "api-max-req-size", Usage: "maximum API request size accepted by the JSON RPC server", }, &cli.DurationFlag{ Name: "api-max-lookback", Usage: "maximum duration allowable for tipset lookbacks", Value: gateway.DefaultLookbackCap, }, &cli.Int64Flag{ Name: "api-wait-lookback-limit", Usage: "maximum number of blocks to search back through for message inclusion", Value: int64(gateway.DefaultStateWaitLookbackLimit), }, }, Action: func(cctx *cli.Context) error { log.Info("Starting lotus gateway") ctx := lcli.ReqContext(cctx) ctx, cancel := context.WithCancel(ctx) defer cancel() // Register all metric views if err := view.Register( metrics.ChainNodeViews..., ); err != nil { log.Fatalf("Cannot register the view: %v", err) } api, closer, err := lcli.GetFullNodeAPIV1(cctx) if err != nil { return err } defer closer() address := cctx.String("listen") mux := mux.NewRouter() log.Info("Setting up API endpoint at " + address) serveRpc := func(path string, hnd interface{}) { serverOptions := make([]jsonrpc.ServerOption, 0) if maxRequestSize := cctx.Int("api-max-req-size"); maxRequestSize != 0 { serverOptions = append(serverOptions, jsonrpc.WithMaxRequestSize(int64(maxRequestSize))) } rpcServer := jsonrpc.NewServer(serverOptions...) rpcServer.Register("Filecoin", hnd) mux.Handle(path, rpcServer) } lookbackCap := cctx.Duration("api-max-lookback") waitLookback := abi.ChainEpoch(cctx.Int64("api-wait-lookback-limit")) ma := metrics.MetricedGatewayAPI(gateway.NewNode(api, lookbackCap, waitLookback)) serveRpc("/rpc/v1", ma) serveRpc("/rpc/v0", lapi.Wrap(new(v1api.FullNodeStruct), new(v0api.WrapperV1Full), ma)) registry := promclient.DefaultRegisterer.(*promclient.Registry) exporter, err := prometheus.NewExporter(prometheus.Options{ Registry: registry, Namespace: "lotus_gw", }) if err != nil { return err } mux.Handle("/debug/metrics", exporter) mux.PathPrefix("/").Handler(http.DefaultServeMux) /*ah := &auth.Handler{ Verify: nodeApi.AuthVerify, Next: mux.ServeHTTP, }*/ srv := &http.Server{ Handler: mux, BaseContext: func(listener net.Listener) context.Context { ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-gateway")) return ctx }, } go func() { <-ctx.Done() log.Warn("Shutting down...") if err := srv.Shutdown(context.TODO()); err != nil { log.Errorf("shutting down RPC server failed: %s", err) } log.Warn("Graceful shutdown successful") }() nl, err := net.Listen("tcp", address) if err != nil { return err } return srv.Serve(nl) }, }