Merge pull request #288 from meshplus/fix/cmd-https-error

fix(cmd/client): fix cmd client is not working when https is enabled
This commit is contained in:
Alexader 2020-12-21 15:55:22 +08:00 committed by GitHub
commit 45985a7b28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 73 additions and 31 deletions

View File

@ -3,6 +3,7 @@ SHELL := /bin/bash
CURRENT_PATH = $(shell pwd)
APP_NAME = bitxhub
APP_VERSION = 1.4.0
export GODEBUG=x509ignoreCN=0
# build with verison infos
VERSION_DIR = github.com/meshplus/${APP_NAME}

View File

@ -24,7 +24,7 @@ func getAccount(ctx *cli.Context) error {
if err != nil {
return err
}
data, err := httpGet(url)
data, err := httpGet(ctx, url)
if err != nil {
return err
}

View File

@ -39,7 +39,7 @@ func getBlockByHeight(ctx *cli.Context, height uint64) error {
if err != nil {
return err
}
data, err := httpGet(url)
data, err := httpGet(ctx, url)
if err != nil {
return err
}
@ -54,7 +54,7 @@ func getBlockByHash(ctx *cli.Context, hash string) error {
if err != nil {
return err
}
data, err := httpGet(url)
data, err := httpGet(ctx, url)
if err != nil {
return err
}

View File

@ -31,7 +31,7 @@ func getChainMeta(ctx *cli.Context) error {
return err
}
data, err := httpGet(url)
data, err := httpGet(ctx, url)
if err != nil {
return fmt.Errorf("http get: %w", err)
}
@ -47,7 +47,7 @@ func getChainStatus(ctx *cli.Context) error {
return err
}
data, err := httpGet(url)
data, err := httpGet(ctx, url)
if err != nil {
return fmt.Errorf("http get: %w", err)
}

View File

@ -9,13 +9,17 @@ var clientCMD = cli.Command{
cli.StringFlag{
Name: "gateway",
Usage: "Specific gateway address",
Value: "localhost:9091",
Value: "http://localhost:9091/v1/",
},
cli.StringFlag{
Name: "grpc",
Usage: "Specific grpc address",
Value: "localhost:60011",
},
cli.StringFlag{
Name: "cert",
Usage: "Specific ca cert file if https is enabled",
},
},
Subcommands: cli.Commands{
accountCMD(),

View File

@ -2,18 +2,32 @@ package client
import (
"bytes"
"fmt"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net/http"
"strings"
"github.com/meshplus/bitxhub/internal/repo"
"github.com/urfave/cli"
)
func httpGet(url string) ([]byte, error) {
func httpGet(ctx *cli.Context, url string) ([]byte, error) {
/* #nosec */
resp, err := http.Get(url)
var (
client *http.Client
err error
)
certPath := ctx.GlobalString("cert")
if certPath != "" {
client, err = getHttpsClient(certPath)
if err != nil {
return nil, err
}
} else {
client = http.DefaultClient
}
resp, err := client.Get(url)
if err != nil {
return nil, err
}
@ -31,11 +45,24 @@ func httpGet(url string) ([]byte, error) {
return c, nil
}
func httpPost(url string, data []byte) ([]byte, error) {
func httpPost(ctx *cli.Context, url string, data []byte) ([]byte, error) {
var (
client *http.Client
err error
)
certPath := ctx.GlobalString("cert")
if certPath != "" {
client, err = getHttpsClient(certPath)
if err != nil {
return nil, err
}
} else {
client = http.DefaultClient
}
buffer := bytes.NewBuffer(data)
/* #nosec */
resp, err := http.Post(url, "application/json", buffer)
resp, err := client.Post(url, "application/json", buffer)
if err != nil {
return nil, err
}
@ -52,18 +79,29 @@ func httpPost(url string, data []byte) ([]byte, error) {
return c, nil
}
func getURL(ctx *cli.Context, path string) (string, error) {
repoRoot, err := repo.PathRootWithDefault(ctx.GlobalString("repo"))
func getHttpsClient(certPath string) (*http.Client, error) {
caCert, err := ioutil.ReadFile(certPath)
if err != nil {
return "", err
return nil, err
}
api, err := repo.GetAPI(repoRoot)
if err != nil {
return "", fmt.Errorf("get api file: %w", err)
}
api = strings.TrimSpace(api)
return api + path, nil
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
return &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
},
},
}, nil
}
func getURL(ctx *cli.Context, p string) (string, error) {
api := ctx.GlobalString("gateway")
api = strings.TrimSpace(api)
if api[len(api)-1:] != "/" {
api = api + "/"
}
return api + p, nil
}

View File

@ -20,7 +20,7 @@ func network(ctx *cli.Context) error {
return err
}
data, err := httpGet(url)
data, err := httpGet(ctx, url)
if err != nil {
return fmt.Errorf("http get: %w", err)
}

View File

@ -22,7 +22,7 @@ func getValidators(ctx *cli.Context) error {
return err
}
data, err := httpGet(url)
data, err := httpGet(ctx, url)
if err != nil {
return fmt.Errorf("http get: %w", err)
}
@ -69,7 +69,7 @@ func delVPNode(ctx *cli.Context) error {
p := pb.DelVPNodeRequest{Pid: pid}
reqData, err := json.Marshal(p)
data, err := httpPost(url, reqData)
data, err := httpPost(ctx, url, reqData)
if err != nil {
return err
}

View File

@ -24,7 +24,7 @@ func getReceipt(ctx *cli.Context) error {
return err
}
data, err := httpGet(url)
data, err := httpGet(ctx, url)
if err != nil {
return err
}

View File

@ -62,7 +62,7 @@ func getTransaction(ctx *cli.Context) error {
return err
}
data, err := httpGet(url)
data, err := httpGet(ctx, url)
if err != nil {
return err
}
@ -130,7 +130,7 @@ func sendTransaction(ctx *cli.Context) error {
return err
}
resp, err := httpPost(url, reqData)
resp, err := httpPost(ctx, url, reqData)
if err != nil {
return err
}

View File

@ -1 +0,0 @@
http://localhost:9091/v1/

View File

@ -58,4 +58,4 @@ addresses = [
"0x79a1215469FaB6f9c63c1816b45183AD3624bE34",
"0x97c8B516D19edBf575D72a172Af7F418BE498C37",
"0xc0Ff2e0b3189132D815b8eb325bE17285AC898f8"
]
]