Skip to content

Commit

Permalink
Merge pull request #4
Browse files Browse the repository at this point in the history
Release v0.1.3
  • Loading branch information
bsrinivas8687 authored Jun 27, 2021
2 parents 1af0727 + 6261b51 commit 9bb52dd
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ linters:
- deadcode
- depguard
- dogsled
- dupl
# - dupl
- errcheck
- funlen
# - gochecknoglobals
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Download the latest version of CLI client software from the releases section [he

```sh
sudo apt-get update && \
sudo apt-get install resolvconf wireguard-tools
sudo apt-get install openresolv wireguard-tools
```

### Mac
Expand Down
10 changes: 6 additions & 4 deletions cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ func ConnectCmd() *cobra.Command {
return err
}

defer resp.Body.Close()

if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
return err
}
Expand Down Expand Up @@ -247,8 +249,8 @@ func ConnectCmd() *cobra.Command {
Name: wireguardtypes.DefaultInterface,
Interface: wireguardtypes.Interface{
Addresses: []wireguardtypes.IPNet{
{v4, 32},
{v6, 128},
{IP: v4, Net: 32},
{IP: v6, Net: 128},
},
ListenPort: listenPort,
PrivateKey: *wgPrivateKey,
Expand All @@ -260,8 +262,8 @@ func ConnectCmd() *cobra.Command {
{
PublicKey: *endpointWGPublicKey,
AllowedIPs: []wireguardtypes.IPNet{
{net.ParseIP("0.0.0.0"), 0},
{net.ParseIP("::"), 0},
{IP: net.ParseIP("0.0.0.0")},
{IP: net.ParseIP("::")},
},
Endpoint: wireguardtypes.Endpoint{
Host: endpointHost.String(),
Expand Down
3 changes: 3 additions & 0 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"

depositcmd "github.com/sentinel-official/cli-client/x/deposit/client/cmd"
nodecmd "github.com/sentinel-official/cli-client/x/node/client/cmd"
plancmd "github.com/sentinel-official/cli-client/x/plan/client/cmd"
providercmd "github.com/sentinel-official/cli-client/x/provider/client/cmd"
Expand All @@ -21,6 +22,8 @@ func QueryCommand() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(depositcmd.QueryDeposit())
cmd.AddCommand(depositcmd.QueryDeposits())
cmd.AddCommand(nodecmd.QueryNode())
cmd.AddCommand(nodecmd.QueryNodes())
cmd.AddCommand(providercmd.QueryProvider())
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/sentinel-official/cli-client
go 1.16

require (
github.com/alessio/shellescape v1.4.1
github.com/cosmos/cosmos-sdk v0.42.5
github.com/go-kit/kit v0.10.0
github.com/olekukonko/tablewriter v0.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0=
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
Expand Down
9 changes: 5 additions & 4 deletions services/wireguard/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"

"github.com/alessio/shellescape"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/viper"

Expand Down Expand Up @@ -41,7 +42,7 @@ func (w *WireGuard) IsUp() bool {
}

output, err := exec.Command("wg", strings.Split(
fmt.Sprintf("show %s", iFace), " ")...).CombinedOutput()
fmt.Sprintf("show %s", shellescape.Quote(iFace)), " ")...).CombinedOutput()
if err != nil {
return false
}
Expand All @@ -56,7 +57,7 @@ func (w *WireGuard) Up() error {
var (
path = filepath.Join(w.Home(), fmt.Sprintf("%s.conf", w.cfg.Name))
cmd = exec.Command("wg-quick", strings.Split(
fmt.Sprintf("up %s", path), " ")...)
fmt.Sprintf("up %s", shellescape.Quote(path)), " ")...)
)

cmd.Stdout = os.Stdout
Expand All @@ -71,7 +72,7 @@ func (w *WireGuard) Down() error {
var (
path = filepath.Join(w.Home(), fmt.Sprintf("%s.conf", w.cfg.Name))
cmd = exec.Command("wg-quick", strings.Split(
fmt.Sprintf("down %s", path), " ")...)
fmt.Sprintf("down %s", shellescape.Quote(path)), " ")...)
)

cmd.Stdout = os.Stdout
Expand All @@ -95,7 +96,7 @@ func (w *WireGuard) Transfer() (int64, int64, error) {
}

output, err := exec.Command("wg", strings.Split(
fmt.Sprintf("show %s transfer", iFace), " ")...).Output()
fmt.Sprintf("show %s transfer", shellescape.Quote(iFace)), " ")...).Output()
if err != nil {
return 0, 0, err
}
Expand Down
5 changes: 4 additions & 1 deletion services/wireguard/wireguard_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ func (w *WireGuard) PreUp() error {
}

func (w *WireGuard) RealInterface() (string, error) {
nameFile, err := os.Open(fmt.Sprintf("/var/run/wireguard/%s.name", w.cfg.Name))
nameFile, err := os.Open(
fmt.Sprintf("/var/run/wireguard/%s.name"),
shellescape.Quote(w.cfg.Name),
)
if err != nil {
return "", err
}
Expand Down
125 changes: 125 additions & 0 deletions x/deposit/client/cmd/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package cmd

import (
"context"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/olekukonko/tablewriter"
deposittypes "github.com/sentinel-official/hub/x/deposit/types"
"github.com/spf13/cobra"

"github.com/sentinel-official/cli-client/x/deposit/types"
)

var (
header = []string{
"Address",
"Amount",
}
)

func QueryDeposit() *cobra.Command {
cmd := &cobra.Command{
Use: "deposit [address]",
Short: "Query a deposit",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

address, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

var (
qsc = deposittypes.NewQueryServiceClient(ctx)
)

result, err := qsc.QueryDeposit(
context.Background(),
deposittypes.NewQueryDepositRequest(address),
)
if err != nil {
return err
}

var (
item = types.NewDepositFromRaw(&result.Deposit)
table = tablewriter.NewWriter(cmd.OutOrStdout())
)

table.SetHeader(header)
table.Append(
[]string{
item.Address,
item.Amount.Raw().String(),
},
)

table.Render()
return nil
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func QueryDeposits() *cobra.Command {
cmd := &cobra.Command{
Use: "deposits",
Short: "Query deposits",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

pagination, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

var (
qsc = deposittypes.NewQueryServiceClient(ctx)
)

result, err := qsc.QueryDeposits(
context.Background(),
deposittypes.NewQueryDepositsRequest(pagination),
)
if err != nil {
return err
}

var (
items = types.NewDepositsFromRaw(result.Deposits)
table = tablewriter.NewWriter(cmd.OutOrStdout())
)

table.SetHeader(header)
for i := 0; i < len(items); i++ {
table.Append(
[]string{
items[i].Address,
items[i].Amount.Raw().String(),
},
)
}

table.Render()
return nil
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "deposits")

return cmd
}
30 changes: 30 additions & 0 deletions x/deposit/types/deposit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package types

import (
deposittypes "github.com/sentinel-official/hub/x/deposit/types"

clienttypes "github.com/sentinel-official/cli-client/types"
)

type Deposit struct {
Address string `json:"address"`
Amount clienttypes.Coins `json:"amount"`
}

func NewDepositFromRaw(v *deposittypes.Deposit) Deposit {
return Deposit{
Address: v.Address,
Amount: clienttypes.NewCoinsFromRaw(v.Coins),
}
}

type Deposits []Deposit

func NewDepositsFromRaw(v deposittypes.Deposits) Deposits {
items := make(Deposits, 0, len(v))
for i := 0; i < len(v); i++ {
items = append(items, NewDepositFromRaw(&v[i]))
}

return items
}
15 changes: 11 additions & 4 deletions x/node/client/cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
"Speed test",
"Peers",
"Handshake",
"Version",
"Status",
}
)
Expand Down Expand Up @@ -117,6 +118,7 @@ func QueryNode() *cobra.Command {
item.Bandwidth.String(),
fmt.Sprintf("%d", item.Peers),
fmt.Sprintf("%t", item.Handshake.Enable),
item.Version,
item.Status,
},
)
Expand Down Expand Up @@ -197,21 +199,25 @@ func QueryNodes() *cobra.Command {
}

var (
wg = sync.WaitGroup{}
group = sync.WaitGroup{}
mutex = sync.Mutex{}
table = tablewriter.NewWriter(cmd.OutOrStdout())
)

table.SetHeader(header)
for i := 0; i < len(items); i++ {
wg.Add(1)
group.Add(1)
go func(i int) {
defer wg.Done()
defer group.Done()

var (
info, _ = fetchNodeInfo(items[i].RemoteURL)
item = types.NewNodeFromRaw(&items[i]).WithInfo(info)
)

mutex.Lock()
defer mutex.Unlock()

table.Append(
[]string{
item.Moniker,
Expand All @@ -222,13 +228,14 @@ func QueryNodes() *cobra.Command {
item.Bandwidth.String(),
fmt.Sprintf("%d", item.Peers),
fmt.Sprintf("%t", item.Handshake.Enable),
item.Version,
item.Status,
},
)
}(i)
}

wg.Wait()
group.Wait()

table.Render()
return nil
Expand Down

0 comments on commit 9bb52dd

Please sign in to comment.