diff --git a/cli/cmd.go b/cli/cmd.go index 92a366eb5e1..edcb69adc11 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "net/url" "os" "os/signal" "strings" @@ -47,14 +48,44 @@ func NewCliError(s string) error { type ApiConnector func() api.FullNode type APIInfo struct { - Addr multiaddr.Multiaddr + Addr string Token []byte } func (a APIInfo) DialArgs() (string, error) { - _, addr, err := manet.DialArgs(a.Addr) + ma, err := multiaddr.NewMultiaddr(a.Addr) + if err == nil { + _, addr, err := manet.DialArgs(ma) + if err != nil { + return "", err + } - return "ws://" + addr + "/rpc/v0", err + return "ws://" + addr + "/rpc/v0", nil + } + + _, err = url.Parse(a.Addr) + if err != nil { + return "", err + } + return a.Addr + "/rpc/v0", nil +} + +func (a APIInfo) Host() (string, error) { + ma, err := multiaddr.NewMultiaddr(a.Addr) + if err == nil { + _, addr, err := manet.DialArgs(ma) + if err != nil { + return "", err + } + + return addr, nil + } + + spec, err := url.Parse(a.Addr) + if err != nil { + return "", err + } + return spec.Host, nil } func (a APIInfo) AuthHeader() http.Header { @@ -72,11 +103,11 @@ func (a APIInfo) AuthHeader() http.Header { func flagForAPI(t repo.RepoType) string { switch t { case repo.FullNode: - return "api" + return "api-url" case repo.StorageMiner: - return "miner-api" + return "miner-api-url" case repo.Worker: - return "worker-api" + return "worker-api-url" default: panic(fmt.Sprintf("Unknown repo type: %v", t)) } @@ -130,11 +161,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) { strma := ctx.String(apiFlag) strma = strings.TrimSpace(strma) - apima, err := multiaddr.NewMultiaddr(strma) - if err != nil { - return APIInfo{}, err - } - return APIInfo{Addr: apima}, nil + return APIInfo{Addr: strma}, nil } envKey := envForRepo(t) @@ -152,12 +179,8 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) { if len(sp) != 2 { log.Warnf("invalid env(%s) value, missing token or address", envKey) } else { - ma, err := multiaddr.NewMultiaddr(sp[1]) - if err != nil { - return APIInfo{}, xerrors.Errorf("could not parse multiaddr from env(%s): %w", envKey, err) - } return APIInfo{ - Addr: ma, + Addr: sp[1], Token: []byte(sp[0]), }, nil } @@ -186,7 +209,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) { } return APIInfo{ - Addr: ma, + Addr: ma.String(), Token: token, }, nil } diff --git a/cli/paych_test.go b/cli/paych_test.go index 35f56d90e4a..18782b4e869 100644 --- a/cli/paych_test.go +++ b/cli/paych_test.go @@ -439,12 +439,12 @@ type mockCLI struct { } func newMockCLI(t *testing.T) *mockCLI { - // Create a CLI App with an --api flag so that we can specify which node + // Create a CLI App with an --api-url flag so that we can specify which node // the command should be executed against app := cli.NewApp() app.Flags = []cli.Flag{ &cli.StringFlag{ - Name: "api", + Name: "api-url", Hidden: true, }, } @@ -476,8 +476,8 @@ func (c *mockCLIClient) runCmd(cmd *cli.Command, input []string) string { } func (c *mockCLIClient) runCmdRaw(cmd *cli.Command, input []string) (string, error) { - // prepend --api= - apiFlag := "--api=" + c.addr.String() + // prepend --api-url= + apiFlag := "--api-url=" + c.addr.String() input = append([]string{apiFlag}, input...) fs := c.flagSet(cmd) @@ -493,7 +493,7 @@ func (c *mockCLIClient) runCmdRaw(cmd *cli.Command, input []string) (string, err } func (c *mockCLIClient) flagSet(cmd *cli.Command) *flag.FlagSet { - // Apply app level flags (so we can process --api flag) + // Apply app level flags (so we can process --api-url flag) fs := &flag.FlagSet{} for _, f := range c.cctx.App.Flags { err := f.Apply(fs) diff --git a/cli/pprof.go b/cli/pprof.go index 6819b362a91..dccb97f9a9f 100644 --- a/cli/pprof.go +++ b/cli/pprof.go @@ -9,7 +9,6 @@ import ( "golang.org/x/xerrors" "github.com/filecoin-project/lotus/node/repo" - manet "github.com/multiformats/go-multiaddr/net" ) var pprofCmd = &cli.Command{ @@ -37,7 +36,7 @@ var PprofGoroutines = &cli.Command{ if err != nil { return xerrors.Errorf("could not get API info: %w", err) } - _, addr, err := manet.DialArgs(ainfo.Addr) + addr, err := ainfo.Host() if err != nil { return err } diff --git a/cmd/lotus-shed/consensus.go b/cmd/lotus-shed/consensus.go index 59d9555dfe0..38a8cd8ef8b 100644 --- a/cmd/lotus-shed/consensus.go +++ b/cmd/lotus-shed/consensus.go @@ -111,7 +111,7 @@ var consensusCheckCmd = &cli.Command{ if err != nil { return err } - ainfo := lcli.APIInfo{Addr: apima} + ainfo := lcli.APIInfo{Addr: apima.String()} addr, err := ainfo.DialArgs() if err != nil { return err