2015-12-16 03:58:01 -06:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2016-04-14 11:18:24 -05:00
|
|
|
// This file is part of go-ethereum.
|
2015-12-16 03:58:01 -06:00
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2015-12-16 03:58:01 -06:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2015-12-16 03:58:01 -06:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-04-14 11:18:24 -05:00
|
|
|
// GNU General Public License for more details.
|
2015-12-16 03:58:01 -06:00
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2015-12-16 03:58:01 -06:00
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewRemoteRPCClient returns a RPC client which connects to a running geth instance.
|
|
|
|
// Depending on the given context this can either be a IPC or a HTTP client.
|
|
|
|
func NewRemoteRPCClient(ctx *cli.Context) (rpc.Client, error) {
|
|
|
|
if ctx.Args().Present() {
|
|
|
|
endpoint := ctx.Args().First()
|
|
|
|
return NewRemoteRPCClientFromString(endpoint)
|
|
|
|
}
|
|
|
|
// use IPC by default
|
2016-02-09 05:24:42 -06:00
|
|
|
return rpc.NewIPCClient(node.DefaultIPCEndpoint())
|
2015-12-16 03:58:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRemoteRPCClientFromString returns a RPC client which connects to the given
|
|
|
|
// endpoint. It must start with either `ipc:` or `rpc:` (HTTP).
|
|
|
|
func NewRemoteRPCClientFromString(endpoint string) (rpc.Client, error) {
|
|
|
|
if strings.HasPrefix(endpoint, "ipc:") {
|
|
|
|
return rpc.NewIPCClient(endpoint[4:])
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(endpoint, "rpc:") {
|
|
|
|
return rpc.NewHTTPClient(endpoint[4:])
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(endpoint, "http://") {
|
|
|
|
return rpc.NewHTTPClient(endpoint)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(endpoint, "ws:") {
|
|
|
|
return rpc.NewWSClient(endpoint)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("invalid endpoint")
|
|
|
|
}
|