-
Notifications
You must be signed in to change notification settings - Fork 0
/
firefox.go
151 lines (139 loc) · 3.5 KB
/
firefox.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package browsercookies
import (
"database/sql"
"errors"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"path/filepath"
"runtime"
"time"
"github.com/go-ini/ini"
_ "github.com/mattn/go-sqlite3"
"github.com/mitchellh/go-homedir"
"golang.org/x/net/publicsuffix"
)
// FireFox is the cookie jar which loads all your cookies in FireFox.
type FireFox struct {
http.CookieJar
}
func (ff *FireFox) parseProfile(profile string) (string, error) {
cfg, err := ini.Load(profile)
if err != nil {
return "", err
}
// TODO: return first profile when no default default profile set.
for _, section := range cfg.Sections() {
if section.Key("Default").String() != "1" {
continue
}
path := section.Key("Path").String()
if section.Key("IsRelative").String() == "1" {
path = filepath.Join(filepath.Dir(profile), path)
}
return path, nil
}
return "", errors.New("No default Firefox profile found")
}
func (ff *FireFox) findDefaultProfile() (string, error) {
homedir, err := homedir.Dir()
if err != nil {
return "", err
}
switch runtime.GOOS {
case "darwin":
return filepath.Join(homedir, filepath.FromSlash("Library/Application Support/Firefox/profiles.ini")), nil
case "linux":
return filepath.Join(homedir, filepath.FromSlash(".mozilla/firefox/profiles.ini")), nil
case "win32":
return filepath.Join(os.Getenv("APPDATA"), filepath.FromSlash("Mozilla/Firefox/profiles.ini")), nil
}
return "", errors.New("Unsupported operating system: " + runtime.GOOS)
}
func (ff *FireFox) findCookieFiles() (string, error) {
profile, err := ff.findDefaultProfile()
if err != nil {
return "", nil
}
profile, err = ff.parseProfile(profile)
if err != nil {
return "", nil
}
return filepath.Join(profile, "cookies.sqlite"), nil
}
func (ff *FireFox) getCookiesFromFile(file string) ([]*http.Cookie, error) {
tempfile, err := ioutil.TempFile("", "go-webcookies")
if err != nil {
return nil, err
}
defer os.Remove(tempfile.Name())
origin, err := os.Open(file)
if err != nil {
return nil, err
}
if _, err := io.Copy(tempfile, origin); err != nil {
return nil, err
}
tempfile.Close()
db, err := sql.Open("sqlite3", tempfile.Name())
if err != nil {
return nil, err
}
defer db.Close()
rows, err := db.Query("SELECT host, path, isSecure, expiry, name, value FROM moz_cookies")
if err != nil {
return nil, err
}
cookies := []*http.Cookie{}
for rows.Next() {
var host, path, name, value string
var secure, expiry int
if err := rows.Scan(&host, &path, &secure, &expiry, &name, &value); err != nil {
return cookies, err
}
cookie := &http.Cookie{
Name: name,
Value: value,
Path: path,
Domain: host,
Expires: time.Unix(int64(expiry), 0),
Secure: secure == 1,
}
cookies = append(cookies, cookie)
}
return cookies, nil
}
func (ff *FireFox) getCookies() ([]*http.Cookie, error) {
file, err := ff.findCookieFiles()
if err != nil {
return nil, err
}
return ff.getCookiesFromFile(file)
}
// LoadFireFox will load all your cookies from FireFox.
func LoadFireFox() (*FireFox, error) {
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return nil, err
}
ff := &FireFox{jar}
cookies, err := ff.getCookies()
if err != nil {
return nil, err
}
for _, cookie := range cookies {
scheme := "http://"
if cookie.Secure {
scheme = "https://"
}
u, err := url.Parse(scheme + cookie.Domain + cookie.Path)
if err != nil {
return nil, err
}
ff.SetCookies(u, []*http.Cookie{cookie})
}
return ff, nil
}