feat: add Safari cookie extraction from BinaryCookies format (#566)

* feat: add Safari cookie extraction from BinaryCookies format
* fix: use expiry presence instead of current time for HasExpire
This commit is contained in:
Roger
2026-04-12 01:16:59 +08:00
committed by GitHub
parent 509cdc2468
commit 7bf1759dd9
7 changed files with 284 additions and 4 deletions
+69
View File
@@ -0,0 +1,69 @@
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()
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,
CreatedAt: c.Creation,
})
}
}
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
}