From 87bfdf05059b7c0b3bcb020c1ddd6e171e096593 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sun, 1 Sep 2019 17:00:19 +0200 Subject: [PATCH] feat: add timeout to close wallet for the security --- CHANGELOG.md | 1 + gpm/cli.go | 67 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4913b0..4242953 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/) - Test build with travis - Add entry's fields Create and LastUpdate +- Add timeout to close wallet for the security ### Changed diff --git a/gpm/cli.go b/gpm/cli.go index f453fcb..adfdeff 100644 --- a/gpm/cli.go +++ b/gpm/cli.go @@ -24,6 +24,7 @@ import ( "os" "strconv" "syscall" + "time" ) // Cli contain config and wallet to use @@ -112,12 +113,23 @@ func (c *Cli) selectEntry() Entry { return entries[0] } - for true { - index, err := strconv.Atoi(c.input("Select the entry: ", "", true)) - if err == nil && index >= 0 && index+1 <= len(entries) { - break + c1 := make(chan int, 1) + go func(max int) { + for true { + index, err := strconv.Atoi(c.input("Select the entry: ", "", true)) + if err == nil && index >= 0 && index+1 <= max { + break + } + fmt.Println("your choice is not an integer or is out of range") } - fmt.Println("your choice is not an integer or is out of range") + c1 <- index + }(len(entries)) + + select { + case res := <-c1: + index = res + case <-time.After(30 * time.Second): + os.Exit(1) } return entries[index] @@ -244,25 +256,34 @@ func (c *Cli) copyEntry() { c.loadWallet() entry := c.selectEntry() - for true { - choice := c.input("select one action: ", "", true) - switch choice { - case "l": - clipboard.WriteAll(entry.User) - case "p": - clipboard.WriteAll(entry.Password) - case "o": - code, time, _ := entry.OTPCode() - fmt.Printf("this OTP code is available for %d seconds\n", time) - clipboard.WriteAll(code) - case "q": - os.Exit(0) - default: - fmt.Println("l -> copy login") - fmt.Println("p -> copy password") - fmt.Println("o -> copy OTP code") - fmt.Println("q -> quit") + go func() { + for true { + choice := c.input("select one action: ", "", true) + switch choice { + case "l": + clipboard.WriteAll(entry.User) + case "p": + clipboard.WriteAll(entry.Password) + case "o": + code, time, _ := entry.OTPCode() + fmt.Printf("this OTP code is available for %d seconds\n", time) + clipboard.WriteAll(code) + case "q": + clipboard.WriteAll("") + os.Exit(0) + default: + fmt.Println("l -> copy login") + fmt.Println("p -> copy password") + fmt.Println("o -> copy OTP code") + fmt.Println("q -> quit") + } } + }() + + select { + case <-time.After(90 * time.Second): + clipboard.WriteAll("") + os.Exit(1) } }