lotus/cmd/lotus-shed/rpc.go

174 lines
3.2 KiB
Go
Raw Normal View History

2020-11-02 18:09:28 +00:00
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
2020-11-27 14:57:00 +00:00
"os"
2020-11-02 18:09:28 +00:00
"strings"
"text/scanner"
"github.com/chzyer/readline"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/node/repo"
)
var rpcCmd = &cli.Command{
Name: "rpc",
Usage: "Interactive JsonPRC shell",
2021-02-04 19:06:00 +00:00
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "miner",
},
2021-03-23 18:32:42 +00:00
&cli.StringFlag{
Name: "version",
Value: "v0",
},
2021-02-04 19:06:00 +00:00
},
2020-11-02 18:09:28 +00:00
Action: func(cctx *cli.Context) error {
2022-02-10 16:33:38 +00:00
var rt repo.RepoType
2021-02-04 19:06:00 +00:00
if cctx.Bool("miner") {
rt = repo.StorageMiner
2022-02-10 16:33:38 +00:00
} else {
rt = repo.FullNode
2021-02-04 19:06:00 +00:00
}
2021-03-23 18:32:42 +00:00
addr, headers, err := lcli.GetRawAPI(cctx, rt, cctx.String("version"))
2020-11-02 18:09:28 +00:00
if err != nil {
return err
}
u, err := url.Parse(addr)
if err != nil {
return xerrors.Errorf("parsing api URL: %w", err)
}
switch u.Scheme {
case "ws":
u.Scheme = "http"
case "wss":
u.Scheme = "https"
}
addr = u.String()
ctx := lcli.ReqContext(cctx)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
afmt := lcli.NewAppFmt(cctx.App)
cs := readline.NewCancelableStdin(afmt.Stdin)
go func() {
<-ctx.Done()
cs.Close() // nolint:errcheck
}()
2020-11-27 14:57:00 +00:00
send := func(method, params string) error {
2020-11-02 18:09:28 +00:00
jreq, err := json.Marshal(struct {
Jsonrpc string `json:"jsonrpc"`
ID int `json:"id"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
}{
Jsonrpc: "2.0",
Method: "Filecoin." + method,
Params: json.RawMessage(params),
ID: 0,
})
if err != nil {
return err
}
req, err := http.NewRequest("POST", addr, bytes.NewReader(jreq))
if err != nil {
return err
}
req.Header = headers
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
rb, err := io.ReadAll(resp.Body)
2020-11-02 18:09:28 +00:00
if err != nil {
return err
}
fmt.Println(string(rb))
if err := resp.Body.Close(); err != nil {
return err
}
2020-11-27 14:57:00 +00:00
return nil
}
if cctx.Args().Present() {
2022-09-14 18:33:29 +00:00
if cctx.NArg() > 2 {
return lcli.ShowHelp(cctx, xerrors.Errorf("expected 1 or 2 arguments: method [params]"))
2020-11-27 14:57:00 +00:00
}
params := cctx.Args().Get(1)
if params == "" {
// TODO: try to be smart and use zero-values for method
params = "[]"
}
return send(cctx.Args().Get(0), params)
2020-11-30 13:26:42 +00:00
}
2020-11-27 14:57:00 +00:00
cctx.App.Metadata["repoType"] = repo.FullNode
2020-11-30 13:26:42 +00:00
if err := lcli.VersionCmd.Action(cctx); err != nil {
return err
}
fmt.Println("Usage: > Method [Param1, Param2, ...]")
2020-11-27 14:57:00 +00:00
2020-11-30 13:26:42 +00:00
rl, err := readline.NewEx(&readline.Config{
Stdin: cs,
HistoryFile: "/tmp/lotusrpc.tmp",
Prompt: "> ",
EOFPrompt: "exit",
HistorySearchFold: true,
2020-11-27 14:57:00 +00:00
2020-11-30 13:26:42 +00:00
// TODO: Some basic auto completion
})
if err != nil {
return err
}
for {
line, err := rl.Readline()
if err == readline.ErrInterrupt {
if len(line) == 0 {
2020-11-27 14:57:00 +00:00
break
2020-11-30 13:26:42 +00:00
} else {
continue
2020-11-27 14:57:00 +00:00
}
2020-11-30 13:26:42 +00:00
} else if err == io.EOF {
break
}
2020-11-27 14:57:00 +00:00
2020-11-30 13:26:42 +00:00
var s scanner.Scanner
s.Init(strings.NewReader(line))
s.Scan()
method := s.TokenText()
2020-11-27 14:57:00 +00:00
2020-11-30 13:26:42 +00:00
s.Scan()
params := line[s.Position.Offset:]
2020-11-27 14:57:00 +00:00
2020-11-30 13:26:42 +00:00
if err := send(method, params); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v", err)
2020-11-27 14:57:00 +00:00
}
2020-11-02 18:09:28 +00:00
}
return nil
},
}