-
Notifications
You must be signed in to change notification settings - Fork 820
/
otp_gen.go
86 lines (74 loc) · 1.95 KB
/
otp_gen.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
package main
import (
"flag"
"fmt"
"log"
"time"
"github.com/pquerna/otp/totp"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/core"
)
const defaultSleepTime = time.Second * 30
func containsOTP(cfg *config.Config) bool {
for x := range cfg.Exchanges {
if cfg.Exchanges[x].API.Credentials.OTPSecret != "" {
return true
}
}
return false
}
func main() {
var cfgFile, code string
var single bool
var err error
flag.StringVar(&cfgFile, "config", config.DefaultFilePath(), "The config input file to process.")
flag.BoolVar(&single, "single", false, "prompt for single use OTP code gen")
flag.Parse()
log.Println("GoCryptoTrader: OTP code generator tool.")
log.Println(core.Copyright)
// Handle single use OTP code gen
if single {
var input string
for {
log.Println("Please enter in your OTP secret:")
if _, err = fmt.Scanln(&input); err != nil {
log.Printf("Failed to read input. Err: %s\n", err)
continue
}
if input != "" {
break
}
}
for {
code, err = totp.GenerateCode(input, time.Now())
if err != nil {
log.Fatalf("Unable to generate OTP code. Err: %s", err)
}
log.Printf("OTP code: %s\n", code)
time.Sleep(defaultSleepTime)
}
}
// Otherwise default to loading the config file and generating OTP codes from it
var cfg config.Config
err = cfg.LoadConfig(cfgFile, true)
if err != nil {
log.Fatal(err)
}
log.Println("Loaded config file.")
if !containsOTP(&cfg) {
log.Fatal("No exchanges with OTP code stored. Exiting.")
}
for {
for x := range cfg.Exchanges {
if cfg.Exchanges[x].API.Credentials.OTPSecret != "" {
code, err = totp.GenerateCode(cfg.Exchanges[x].API.Credentials.OTPSecret, time.Now())
if err != nil {
log.Printf("Exchange %s: Failed to generate OTP code. Err: %s\n", cfg.Exchanges[x].Name, err)
continue
}
log.Printf("%s: %s\n", cfg.Exchanges[x].Name, code)
}
}
time.Sleep(defaultSleepTime)
}
}