diff --git a/cmd/root.go b/cmd/root.go index d57e8ca..642b8cb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,8 +9,9 @@ import ( ) var ( - cfgFile string - dataDir string + cfgFile string + dataDir string + connTimeout int ) // rootCmd represents the base command when called without any subcommands diff --git a/cmd/send.go b/cmd/send.go index 6abb2b2..418d544 100644 --- a/cmd/send.go +++ b/cmd/send.go @@ -20,10 +20,11 @@ messages can be piped into this command.`, return nil }, Run: func(cmd *cobra.Command, args []string) { - brbrclient.Send(dataDir, args[0]) + brbrclient.Send(dataDir, args[0], connTimeout) }, } func init() { + sendCmd.Flags().IntVarP(&connTimeout, "timeout", "t", 60, "Specify connection timeout in seconds") rootCmd.AddCommand(sendCmd) } diff --git a/lib/brbrclient/client.go b/lib/brbrclient/client.go index 0e5fe3d..b2834e8 100644 --- a/lib/brbrclient/client.go +++ b/lib/brbrclient/client.go @@ -19,7 +19,7 @@ import ( "google.golang.org/protobuf/types/known/timestamppb" ) -func Send(dataDir, rcptAddr string) { +func Send(dataDir, rcptAddr string, timeout int) { var rcptId, msg string if strings.HasSuffix(rcptAddr, ".onion") { @@ -57,7 +57,9 @@ func Send(dataDir, rcptAddr string) { msg = strings.Join(msgLines, "\n") - ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + connTimeout := time.Duration(timeout) * time.Second + + ctx, cancel := context.WithTimeout(context.Background(), connTimeout) defer cancel() // use embedded tor (only on linux) @@ -80,7 +82,7 @@ func Send(dataDir, rcptAddr string) { //} //defer t.Close() - log.Printf("Connecting to onion service %s", rcptAddr) + log.Printf("Connecting to onion service %s (timeout: %s)", rcptAddr, connTimeout) dialer, err := t.Dialer(ctx, nil) if err != nil {