-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (84 loc) · 1.98 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
package main
import (
"fmt"
"log"
"os"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
func usage() {
fmt.Println("Usage: " + os.Args[0] + " /path/to/firefox-profile/cookies.sql [output_name]")
}
func stringBool(expr bool) string {
if expr {
return "TRUE"
} else {
return "FALSE"
}
}
func stringInt(expr int) string {
if expr == 1 {
return "TRUE"
} else {
return "FALSE"
}
}
func rowToLine(row *sql.Rows) (string, error) {
var (
host, path, subdomains, isSecure, name, value string
expiry, secure int
)
if err := row.Scan(&host, &path, &name, &value, &secure, &expiry); err != nil {
return "", err
}
isSecure = stringInt(secure)
subdomains = stringBool(host[0] == '.')
const format string = "%s\t%s\t%s\t%s\t%d\t%s\t%s\n"
if subdomains == "TRUE" {
return fmt.Sprintf(format, host[1:], subdomains, path, isSecure, expiry, name, value), nil
} else {
return fmt.Sprintf(format, host, subdomains, path, isSecure, expiry, name, value), nil
}
}
func main() {
if len(os.Args) < 2 {
usage()
return
}
db, err := sql.Open("sqlite3", os.Args[1])
if err != nil {
log.Fatal(err.Error())
return
}
defer db.Close()
err = db.Ping()
if err != nil {
log.Fatal(err.Error())
return
}
var txt string
if len(os.Args) == 3 {
txt = os.Args[2]
} else {
txt = "cookies.txt"
}
file, err := os.Create(txt)
if err != nil {
log.Fatal(err.Error())
return
}
defer file.Close()
rows, err := db.Query("SELECT host, path, name, value, isSecure, expiry FROM moz_cookies")
if err != nil {
log.Fatal(err.Error())
return
}
defer rows.Close()
for rows.Next() {
line, err := rowToLine(rows)
if err != nil {
log.Fatal(err.Error())
}
file.WriteString(line)
}
}