|
|
|
@ -5,10 +5,14 @@ import ( |
|
|
|
|
"net/http" |
|
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
"bytes" |
|
|
|
|
"io/ioutil" |
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/logger" |
|
|
|
|
"github.com/ethereum/go-ethereum/logger/glog" |
|
|
|
|
"github.com/ethereum/go-ethereum/rpc/api" |
|
|
|
|
"github.com/ethereum/go-ethereum/rpc/codec" |
|
|
|
|
"github.com/ethereum/go-ethereum/rpc/shared" |
|
|
|
|
"github.com/rs/cors" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
@ -65,13 +69,19 @@ func StopHttp() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type httpClient struct { |
|
|
|
|
address string |
|
|
|
|
port uint |
|
|
|
|
codec codec.ApiCoder |
|
|
|
|
lastRes interface{} |
|
|
|
|
lastErr error |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create a new in process client
|
|
|
|
|
func NewHttpClient(cfg HttpConfig, codec codec.Codec) *httpClient { |
|
|
|
|
func NewHttpClient(cfg HttpConfig, c codec.Codec) *httpClient { |
|
|
|
|
return &httpClient{ |
|
|
|
|
codec: codec.New(nil), |
|
|
|
|
address: cfg.ListenAddress, |
|
|
|
|
port: cfg.ListenPort, |
|
|
|
|
codec: c.New(nil), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -80,9 +90,103 @@ func (self *httpClient) Close() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *httpClient) Send(req interface{}) error { |
|
|
|
|
var body []byte |
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
self.lastRes = nil |
|
|
|
|
self.lastErr = nil |
|
|
|
|
|
|
|
|
|
if body, err = self.codec.Encode(req); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
httpReq, err := http.NewRequest("POST", fmt.Sprintf("%s:%d", self.address, self.port), bytes.NewBuffer(body)) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
httpReq.Header.Set("Content-Type", "application/json") |
|
|
|
|
|
|
|
|
|
client := http.Client{} |
|
|
|
|
resp, err := client.Do(httpReq) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
defer resp.Body.Close() |
|
|
|
|
|
|
|
|
|
if resp.Status == "200 OK" { |
|
|
|
|
reply, _ := ioutil.ReadAll(resp.Body) |
|
|
|
|
var rpcSuccessResponse shared.SuccessResponse |
|
|
|
|
if err = self.codec.Decode(reply, &rpcSuccessResponse); err == nil { |
|
|
|
|
self.lastRes = rpcSuccessResponse.Result |
|
|
|
|
self.lastErr = err |
|
|
|
|
return nil |
|
|
|
|
} else { |
|
|
|
|
var rpcErrorResponse shared.ErrorResponse |
|
|
|
|
if err = self.codec.Decode(reply, &rpcErrorResponse); err == nil { |
|
|
|
|
self.lastRes = rpcErrorResponse.Error |
|
|
|
|
self.lastErr = err |
|
|
|
|
return nil |
|
|
|
|
} else { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return fmt.Errorf("Not implemented") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *httpClient) Recv() (interface{}, error) { |
|
|
|
|
return nil, nil |
|
|
|
|
return self.lastRes, self.lastErr |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *httpClient) SupportedModules() (map[string]string, error) { |
|
|
|
|
var body []byte |
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
payload := shared.Request{ |
|
|
|
|
Id: 1, |
|
|
|
|
Jsonrpc: "2.0", |
|
|
|
|
Method: "modules", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if body, err = self.codec.Encode(payload); err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf("%s:%d", self.address, self.port), bytes.NewBuffer(body)) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
|
|
|
|
|
|
|
client := http.Client{} |
|
|
|
|
resp, err := client.Do(req) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
defer resp.Body.Close() |
|
|
|
|
|
|
|
|
|
if resp.Status == "200 OK" { |
|
|
|
|
reply, _ := ioutil.ReadAll(resp.Body) |
|
|
|
|
var rpcRes shared.SuccessResponse |
|
|
|
|
if err = self.codec.Decode(reply, &rpcRes); err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
result := make(map[string]string) |
|
|
|
|
if modules, ok := rpcRes.Result.(map[string]interface{}); ok { |
|
|
|
|
for a, v := range modules { |
|
|
|
|
result[a] = fmt.Sprintf("%s", v) |
|
|
|
|
} |
|
|
|
|
return result, nil |
|
|
|
|
} |
|
|
|
|
err = fmt.Errorf("Unable to parse module response - %v", rpcRes.Result) |
|
|
|
|
} else { |
|
|
|
|
fmt.Printf("resp.Status = %s\n", resp.Status) |
|
|
|
|
fmt.Printf("err = %v\n", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|