forked from martinraynov/gauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgauth.go
115 lines (96 loc) · 2.72 KB
/
gauth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"bufio"
"bytes"
"encoding/csv"
"fmt"
"log"
"os"
"os/exec"
"os/user"
"path"
"strings"
"syscall"
"text/tabwriter"
"time"
"github.com/pcarrier/gauth/gauth"
"golang.org/x/crypto/ssh/terminal"
)
func main() {
cfgPath := os.Getenv("GAUTH_CONFIG")
if cfgPath == "" {
user, err := user.Current()
if err != nil {
log.Fatal(err)
}
cfgPath = path.Join(user.HomeDir, ".gauth/gauth.csv")
if _, err := os.Stat(cfgPath); err != nil {
log.Println("gauth.csv doesn't exist : ", err)
log.Println("Do you wans to init the gauth.csv config file ? [y/N]")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
if scanner.Text() == "y" {
errCreate := os.MkdirAll(path.Join(user.HomeDir, ".gauth"), 0700)
if errCreate != nil {
log.Fatalln("Error when creating folder : ", path.Join(user.HomeDir, ".gauth"), errCreate)
}
_, errCreate = os.OpenFile(path.Join(user.HomeDir, ".gauth/gauth.csv"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if errCreate != nil {
log.Fatalln("Error when creating gauth.csv file : ", path.Join(user.HomeDir, ".gauth/gauth.csv"), errCreate)
}
log.Println("Config file created at : ", path.Join(user.HomeDir, ".gauth/gauth.csv"))
log.Println("You can now modify your config file and add your auth information (1 per line)")
log.Println("Press the Enter Key to stop anytime")
break
} else {
os.Exit(1)
}
}
fmt.Scanln()
os.Exit(1)
}
}
cfgContent, err := gauth.LoadConfigFile(cfgPath, getPassword)
if err != nil {
log.Fatalf("Loading config: %v", err)
}
cfgReader := csv.NewReader(bytes.NewReader(cfgContent))
// Unix-style tabular
cfgReader.Comma = ':'
cfg, err := cfgReader.ReadAll()
if err != nil {
log.Fatalf("Decoding CSV: %v", err)
}
go func() {
for {
// Clear terminal For windows
c := exec.Command("cmd", "/c", "cls")
c.Stdout = os.Stdout
c.Run()
// Clear terminal For linux
fmt.Print("\033[H\033[2J")
currentTS, progress := gauth.IndexNow()
tw := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0)
fmt.Fprintln(tw, "\tprev\tcurr\tnext")
for _, record := range cfg {
name, secret := record[0], record[1]
prev, curr, next, err := gauth.Codes(secret, currentTS)
if err != nil {
log.Fatalf("Generating codes: %v", err)
}
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", name, prev, curr, next)
}
tw.Flush()
fmt.Printf("[%-29s]\n\n\n", strings.Repeat("=", progress))
log.Println("Press the Enter Key to stop anytime")
time.Sleep(200 * time.Millisecond)
}
}()
fmt.Scanln()
os.Exit(1)
}
func getPassword() ([]byte, error) {
fmt.Printf("Encryption password: ")
defer fmt.Println()
return terminal.ReadPassword(int(syscall.Stdin))
}