From fcae0fc903eb4cd89b31d628deda215ac24e4a89 Mon Sep 17 00:00:00 2001 From: Finn Date: Thu, 22 Jul 2021 21:42:55 -0700 Subject: [PATCH] Add session reset command --- cmd/signaldctl/cmd/root.go | 2 + cmd/signaldctl/cmd/session/root.go | 30 +++++++ cmd/signaldctl/cmd/session/session.go | 122 ++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 cmd/signaldctl/cmd/session/root.go create mode 100644 cmd/signaldctl/cmd/session/session.go diff --git a/cmd/signaldctl/cmd/root.go b/cmd/signaldctl/cmd/root.go index e6e5c36..ac78c5b 100644 --- a/cmd/signaldctl/cmd/root.go +++ b/cmd/signaldctl/cmd/root.go @@ -28,6 +28,7 @@ import ( "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/key" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/message" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/session" "gitlab.com/signald/signald-go/cmd/signaldctl/common" "gitlab.com/signald/signald-go/cmd/signaldctl/config" "gitlab.com/signald/signald-go/signald" @@ -83,6 +84,7 @@ func init() { RootCmd.AddCommand(group.GroupCmd) RootCmd.AddCommand(key.KeyCmd) RootCmd.AddCommand(message.MessageCmd) + RootCmd.AddCommand(session.SessionCmd) } // initConfig reads in config file and ENV variables if set. diff --git a/cmd/signaldctl/cmd/session/root.go b/cmd/signaldctl/cmd/session/root.go new file mode 100644 index 0000000..29673b5 --- /dev/null +++ b/cmd/signaldctl/cmd/session/root.go @@ -0,0 +1,30 @@ +// Copyright © 2021 Finn Herzfeld +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package session + + +import ( + "github.com/spf13/cobra" +) + +var SessionCmd = &cobra.Command{ + Use: "session", + Aliases: []string{"sessions"}, +} + +func init() { + SessionCmd.AddCommand(ResetCmd) +} diff --git a/cmd/signaldctl/cmd/session/session.go b/cmd/signaldctl/cmd/session/session.go new file mode 100644 index 0000000..e0b035b --- /dev/null +++ b/cmd/signaldctl/cmd/session/session.go @@ -0,0 +1,122 @@ +// Copyright © 2021 Finn Herzfeld +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package session + +import ( + "encoding/json" + "fmt" + "log" + "os" + "strings" + + "github.com/jedib0t/go-pretty/v6/table" + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" + + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/cmd/signaldctl/config" + "gitlab.com/signald/signald-go/signald/client-protocol/v1" +) + +var ( + account string + + ResetCmd = &cobra.Command{ + Use: "reset ", + Short: "reset secure session with another Signal user", + PreRun: func(cmd *cobra.Command, args []string) { + if account == "" { + account = config.Config.DefaultAccount + } + if account == "" { + common.Must(cmd.Help()) + log.Fatal("No account specified. Please specify with --account or set a default") + } + if len(args) == 0 { + common.Must(cmd.Help()) + log.Fatal("must specify a phone number or UUID to reset session with") + } + }, + Run: func(_ *cobra.Command, args []string) { + go common.Signald.Listen(nil) + + req := v1.ResetSessionRequest{Account: account} + + if strings.HasPrefix(args[0], "+") { + req.Address = &v1.JsonAddress{Number: args[0]} + } else { + req.Address = &v1.JsonAddress{UUID: args[0]} + } + + resp, err := req.Submit(common.Signald) + if err != nil { + log.Fatal("error sending request to signald: ", err) + } + + switch common.OutputFormat { + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatYAML: + err := yaml.NewEncoder(os.Stdout).Encode(resp) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault: + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + t.AppendHeader(table.Row{"Number", "UUID", "Duration", "Send Error"}) + for _, result := range resp.Results { + if result.Success != nil { + t.AppendRow(table.Row{ + result.Address.Number, + result.Address.UUID, + fmt.Sprintf("%dms", result.Success.Duration), + "", + }) + } else { + var sendError string + if result.IdentityFailure != "" { + sendError = fmt.Sprintf("identity failure: %s\n", result.IdentityFailure) + } + if result.NetworkFailure { + sendError = "network failure" + } + if result.UnregisteredFailure { + sendError = "user not registered" + } + t.AppendRow(table.Row{result.Address.Number, result.Address.UUID, "", sendError}) + } + } + + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + common.StylizeTable(t) + t.Render() + } + default: + log.Fatal("Unsupported output format") + } + }, + } +) + +func init() { + ResetCmd.Flags().StringVarP(&account, "account", "a", "", "local account to use") +}