From 15c48084cb151f40bd97da4a06091da4a77f81fe Mon Sep 17 00:00:00 2001 From: Srinivas Baride Date: Sun, 27 Jun 2021 03:11:05 +0530 Subject: [PATCH 1/7] Use openresolv instead of resolvconf --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5be4527..c3053c8 100644 --- a/README.md +++ b/README.md @@ -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 From 0e44f172b66e759b16624c0d963e0318154165f1 Mon Sep 17 00:00:00 2001 From: Srinivas Baride Date: Sun, 27 Jun 2021 03:47:24 +0530 Subject: [PATCH 2/7] Added version column for nodes table --- x/node/client/cmd/query.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x/node/client/cmd/query.go b/x/node/client/cmd/query.go index 139ac8f..86e1dbe 100644 --- a/x/node/client/cmd/query.go +++ b/x/node/client/cmd/query.go @@ -31,6 +31,7 @@ var ( "Speed test", "Peers", "Handshake", + "Version", "Status", } ) @@ -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, }, ) @@ -222,6 +224,7 @@ func QueryNodes() *cobra.Command { item.Bandwidth.String(), fmt.Sprintf("%d", item.Peers), fmt.Sprintf("%t", item.Handshake.Enable), + item.Version, item.Status, }, ) From 55e891e63b921d8e26e72c4f1031ac65d90ef12e Mon Sep 17 00:00:00 2001 From: Srinivas Baride Date: Sun, 27 Jun 2021 11:03:31 +0530 Subject: [PATCH 3/7] Added deposit types --- x/deposit/types/deposit.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 x/deposit/types/deposit.go diff --git a/x/deposit/types/deposit.go b/x/deposit/types/deposit.go new file mode 100644 index 0000000..d50b31f --- /dev/null +++ b/x/deposit/types/deposit.go @@ -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 +} From 3564aade7e83066eb5870e0644f9e5e52fecaa48 Mon Sep 17 00:00:00 2001 From: Srinivas Baride Date: Sun, 27 Jun 2021 11:04:16 +0530 Subject: [PATCH 4/7] Added session query commands --- cmd/query.go | 3 + x/deposit/client/cmd/query.go | 125 ++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 x/deposit/client/cmd/query.go diff --git a/cmd/query.go b/cmd/query.go index ddde479..b933f90 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -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" @@ -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()) diff --git a/x/deposit/client/cmd/query.go b/x/deposit/client/cmd/query.go new file mode 100644 index 0000000..f840f40 --- /dev/null +++ b/x/deposit/client/cmd/query.go @@ -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 +} From e8157792f3002739dab82541e847761db32dba25 Mon Sep 17 00:00:00 2001 From: Srinivas Baride Date: Sun, 27 Jun 2021 11:13:06 +0530 Subject: [PATCH 5/7] Fixed some lint errors --- .golangci.yml | 2 +- cmd/connect.go | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 4fb19d0..b01a79a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -27,7 +27,7 @@ linters: - deadcode - depguard - dogsled - - dupl + # - dupl - errcheck - funlen # - gochecknoglobals diff --git a/cmd/connect.go b/cmd/connect.go index 21c24ed..3915b33 100644 --- a/cmd/connect.go +++ b/cmd/connect.go @@ -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 } @@ -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, @@ -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(), From 025870df8d5430f0e8ec73c42f92372db0f81b35 Mon Sep 17 00:00:00 2001 From: Srinivas Baride Date: Sun, 27 Jun 2021 11:27:52 +0530 Subject: [PATCH 6/7] Added shell escape --- go.mod | 1 + go.sum | 2 ++ services/wireguard/wireguard.go | 9 +++++---- services/wireguard/wireguard_darwin.go | 5 ++++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index c33b587..ed5845b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 1dc9f80..78dedb6 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/services/wireguard/wireguard.go b/services/wireguard/wireguard.go index bb9e76d..b066400 100644 --- a/services/wireguard/wireguard.go +++ b/services/wireguard/wireguard.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" + "github.com/alessio/shellescape" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/spf13/viper" @@ -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 } @@ -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 @@ -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 @@ -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 } diff --git a/services/wireguard/wireguard_darwin.go b/services/wireguard/wireguard_darwin.go index fc8fae5..9ec7e37 100644 --- a/services/wireguard/wireguard_darwin.go +++ b/services/wireguard/wireguard_darwin.go @@ -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 } From 6261b517ac0100dfd4024c12ae4bef4aeed8fe2e Mon Sep 17 00:00:00 2001 From: Srinivas Baride Date: Sun, 27 Jun 2021 11:33:01 +0530 Subject: [PATCH 7/7] Prevent data race while querying nodes --- x/node/client/cmd/query.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/x/node/client/cmd/query.go b/x/node/client/cmd/query.go index 86e1dbe..5b75473 100644 --- a/x/node/client/cmd/query.go +++ b/x/node/client/cmd/query.go @@ -199,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, @@ -231,7 +235,7 @@ func QueryNodes() *cobra.Command { }(i) } - wg.Wait() + group.Wait() table.Render() return nil