-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathextract_cookie.go
More file actions
71 lines (62 loc) · 1.58 KB
/
extract_cookie.go
File metadata and controls
71 lines (62 loc) · 1.58 KB
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
package safari
import (
"fmt"
"os"
"sort"
"github.com/moond4rk/binarycookies"
"github.com/moond4rk/hackbrowserdata/types"
)
func extractCookies(path string) ([]types.CookieEntry, error) {
pages, err := decodeBinaryCookies(path)
if err != nil {
return nil, err
}
var cookies []types.CookieEntry
for _, page := range pages {
for _, c := range page.Cookies {
hasExpire := !c.Expires.IsZero()
// binarycookies returns time.Time in Local; normalize to UTC
// so exported JSON matches Chromium/Firefox cookie output.
cookies = append(cookies, types.CookieEntry{
Host: string(c.Domain),
Path: string(c.Path),
Name: string(c.Name),
Value: string(c.Value),
IsSecure: c.Secure,
IsHTTPOnly: c.HTTPOnly,
HasExpire: hasExpire,
IsPersistent: hasExpire,
ExpireAt: c.Expires.UTC(),
CreatedAt: c.Creation.UTC(),
})
}
}
sort.Slice(cookies, func(i, j int) bool {
return cookies[i].CreatedAt.After(cookies[j].CreatedAt)
})
return cookies, nil
}
func countCookies(path string) (int, error) {
pages, err := decodeBinaryCookies(path)
if err != nil {
return 0, err
}
var total int
for _, page := range pages {
total += len(page.Cookies)
}
return total, nil
}
func decodeBinaryCookies(path string) ([]binarycookies.Page, error) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("open cookies file: %w", err)
}
defer f.Close()
jar := binarycookies.New(f)
pages, err := jar.Decode()
if err != nil {
return nil, fmt.Errorf("decode cookies: %w", err)
}
return pages, nil
}