-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
128 lines (117 loc) · 3.03 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"golang.org/x/term"
"os"
"strings"
)
func errAndExit(message string, err error) {
if err == nil {
fmt.Println(message)
} else {
fmt.Printf(message, err)
}
os.Exit(1)
}
func parseArgs() (*os.File, string) {
for _, arg := range os.Args {
if arg == "-h" || strings.Contains(arg, "help") {
fmt.Printf("A simple tool to convert exported (and encrypted) JSON from Aegis to KeePass database.\n\nUsage:\natk /path/to/aegis-export.json /path/to/output.kdbx\n")
os.Exit(0)
}
}
if len(os.Args) < 2 {
errAndExit("Please pass the path to the encrypted Aegis JSON file as parameter", nil)
}
if len(os.Args) < 3 {
errAndExit("Please pass the path to the desired output file", nil)
}
file, err := os.Open(os.Args[1])
if err != nil {
errAndExit("Failed to open exported file: %v", err)
}
outputPath := os.Args[2]
if !strings.HasSuffix(outputPath, ".kdbx") {
outputPath += ".kdbx"
}
return file, outputPath
}
func readRuneFromStdin(prompt string) rune {
fmt.Println(prompt)
var reader = bufio.NewReader(os.Stdin)
r, _, err := reader.ReadRune()
if err != nil {
errAndExit("Failed to read confirmation from stdin: %v", err)
}
return r
}
func isOverwriteOk(kdbxPath string) bool {
stat, err := os.Stat(kdbxPath)
if stat != nil || os.IsExist(err) {
r := readRuneFromStdin("A file already exists at the specified output path. Are sure you want to rewrite it completely? Y/N")
if r != 'Y' && r != 'y' {
return false
}
}
return true
}
func readStyle() OtpStyle {
r := readRuneFromStdin(`What OTP-style should we use?
1. KeePassXC/KeeTrayTotp (Default, uses TOTP Seed/TOTP Settings fields)
2. KeePass 2 (TimeOtp-Secret-Base32/HmacOtp-Secret-Base32)
3. KeeWeb/Key URI (single "otp" field in a format otpauth://type/label?secret=abcd)`)
switch r {
case '\n':
fallthrough
case '1':
return KeeTrayTotp
case '2':
return KeePass2
case '3':
return KeeWebOtp
default:
fmt.Printf("%v is not a valid option, please input 1, 2 or 3", r)
return readStyle()
}
}
func main() {
file, kdbxPath := parseArgs()
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(file)
if err != nil {
errAndExit("Failed to read exported file: %v", err)
}
data := buf.Bytes()
exported := aegis{}
err = json.Unmarshal(data, &exported)
if err != nil {
errAndExit("Improperly formatted JSON file: %v", err)
}
fmt.Println("Please input your master password")
password, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
errAndExit("Failed to read password from input: %v", err)
}
if len(password) < 1 {
errAndExit("Empty password", nil)
}
aegisDb, err := exported.Decrypt(password)
if err != nil {
errAndExit("Failed to decrypt Aegis database: %v", err)
}
if len(aegisDb.Entries) == 0 {
errAndExit("No entries in the database, nothing to save", nil)
}
if !isOverwriteOk(kdbxPath) {
os.Exit(0)
}
style := readStyle()
err = aegisDb.ToKeePass(kdbxPath, password, style)
if err != nil {
errAndExit("Conversion process failed: %v", err)
}
fmt.Println("Done")
}