-
Notifications
You must be signed in to change notification settings - Fork 46
/
kooky.go
294 lines (273 loc) · 6.87 KB
/
kooky.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package kooky
import (
"context"
"encoding/json"
"errors"
"iter"
"net/http"
"sync"
"time"
)
// Cookie is an http.Cookie augmented with information obtained through the scraping process.
type Cookie struct {
http.Cookie
Creation time.Time
Container string
Browser BrowserInfo
}
// Cookie retrieving functions in this package like TraverseCookies(), ReadCookies(), AllCookies()
// use registered cookiestore finders to read cookies.
// Erronous reads are skipped.
//
// Register cookie store finders for all browsers like this:
//
// import _ "github.com/browserutils/kooky/browser/all"
//
// Or only a specific browser:
//
// import _ "github.com/browserutils/kooky/browser/chrome"
func ReadCookies(ctx context.Context, filters ...Filter) (Cookies, error) {
return TraverseCookies(ctx).ReadAllCookies(ctx)
}
func AllCookies(filters ...Filter) Cookies {
// for convenience...
ctx := context.Background()
return TraverseCookies(ctx).Collect(ctx)
}
// adjustments to the json marshaling to allow dates with more than 4 year digits
// https://github.com/golang/go/issues/4556
// https://github.com/golang/go/issues/54580
// encoding/json/v2 "format"(?) might make this unnecessary
func (c *Cookie) MarshalJSON() ([]byte, error) {
if c == nil {
return []byte(`null`), nil
}
c2 := &struct {
// net/http.Cookie
Name string `json:"name"`
Value string `json:"value"`
Quoted bool `json:"quoted"`
Path string `json:"path"`
Domain string `json:"domain"`
Expires jsonTime `json:"expires"`
RawExpires string `json:"raw_expires,omitempty"`
MaxAge int `json:"max_age"`
Secure bool `json:"secure"`
HttpOnly bool `json:"http_only"`
SameSite http.SameSite `json:"same_site"`
Partitioned bool `json:"partitioned"`
Raw string `json:"raw,omitempty"`
Unparsed []string `json:"unparsed,omitempty"`
// extra fields
Creation jsonTime `json:"creation"`
Browser string `json:"browser,omitempty"`
Profile string `json:"profile,omitempty"`
IsDefaultProfile bool `json:"is_default_profile"`
Container string `json:"container,omitempty"`
FilePath string `json:"file_path,omitempty"`
}{
Name: c.Cookie.Name,
Value: c.Cookie.Value,
Quoted: c.Cookie.Quoted,
Path: c.Cookie.Path,
Domain: c.Cookie.Domain,
Expires: jsonTime{c.Cookie.Expires},
RawExpires: c.Cookie.RawExpires,
MaxAge: c.Cookie.MaxAge,
Secure: c.Cookie.Secure,
HttpOnly: c.Cookie.HttpOnly,
SameSite: c.Cookie.SameSite,
Partitioned: c.Cookie.Partitioned,
Raw: c.Cookie.Raw,
Unparsed: c.Cookie.Unparsed,
Creation: jsonTime{c.Creation},
Container: c.Container,
}
if c.Browser != nil {
c2.Browser = c.Browser.Browser()
c2.Profile = c.Browser.Profile()
c2.IsDefaultProfile = c.Browser.IsDefaultProfile()
c2.FilePath = c.Browser.FilePath()
}
return json.Marshal(c2)
}
type jsonTime struct{ time.Time }
// MarshalJSON implements the [json.Marshaler] interface.
// The time is a quoted string in the RFC 3339 format with sub-second precision.
// the timestamp might be represented as invalid RFC 3339 if necessary (year with more than 4 digits).
func (t jsonTime) MarshalJSON() ([]byte, error) {
if b, err := t.Time.MarshalJSON(); err == nil {
return b, nil
}
return []byte(t.Time.Format(`"` + time.RFC3339 + `"`)), nil
}
// for-rangeable cookie retriever
type CookieSeq iter.Seq2[*Cookie, error]
func TraverseCookies(ctx context.Context, filters ...Filter) CookieSeq {
return TraverseCookieStores(ctx).TraverseCookies(ctx, filters...)
}
// Collect() is the same as ReadAllCookies but ignores the error
func (s CookieSeq) Collect(ctx context.Context) Cookies {
cookies, _ := s.ReadAllCookies(ctx)
return cookies
}
func (s CookieSeq) ReadAllCookies(ctx context.Context) (Cookies, error) {
if s == nil {
return nil, errors.New(`nil receiver`)
}
var (
errs []error
cookies []*Cookie
)
Outer:
for cookie, err := range s {
if err != nil {
errs = append(errs, err)
continue
}
if cookie != nil {
cookies = append(cookies, cookie)
}
select {
case <-ctx.Done():
errs = append(errs, errors.New(`context cancel`))
break Outer
default:
}
}
return cookies, errors.Join(errs...)
}
// sequence of non-nil cookies and nil errors
func (s CookieSeq) OnlyCookies() CookieSeq {
return func(yield func(*Cookie, error) bool) {
if s == nil {
return
}
for cookie, err := range s {
if err != nil || cookie == nil {
continue
}
if !yield(cookie, nil) {
return
}
}
}
}
func (s CookieSeq) Filter(ctx context.Context, filters ...Filter) CookieSeq {
return func(yield func(*Cookie, error) bool) {
if s == nil {
yield(nil, errors.New(`nil receiver`))
return
}
for cookie, errCookie := range s {
if errCookie != nil {
if !yield(nil, errCookie) {
return
}
continue
}
if cookie == nil {
continue
}
select {
case <-ctx.Done():
return
default:
}
if !FilterCookie(ctx, cookie, filters...) {
continue
}
if !yield(cookie, nil) {
return
}
}
}
}
func (s CookieSeq) FirstMatch(ctx context.Context, filters ...Filter) *Cookie {
if s == nil {
return nil
}
for cookie, _ := range s.OnlyCookies() {
select {
case <-ctx.Done():
return nil
default:
}
if FilterCookie(ctx, cookie, filters...) {
return cookie
}
}
return nil
}
func (s CookieSeq) Merge(seqs ...CookieSeq) CookieSeq { return MergeCookieSeqs(append(seqs, s)...) }
func MergeCookieSeqs(seqs ...CookieSeq) CookieSeq {
var sq []iter.Seq2[*Cookie, error]
for _, s := range seqs {
sq = append(sq, iter.Seq2[*Cookie, error](s))
}
return CookieSeq(mergeSeqs(sq...))
}
func mergeSeqs[S iter.Seq2[T, error], T any](seqs ...S) S {
seqs0 := func(yield func(T, error) bool) {}
seqs2 := func(yield func(T, error) bool) {
var wg sync.WaitGroup
defer wg.Wait()
wg.Add(len(seqs) + 1)
runner := func(seq S) {
defer wg.Done()
if seq == nil {
return
}
for v, error := range seq {
if !yield(v, error) {
return
}
}
}
for _, seq := range seqs {
go runner(seq)
}
}
switch len(seqs) {
case 0:
return seqs0
case 1:
return seqs[0]
default:
return seqs2
}
}
func (s CookieSeq) Chan(ctx context.Context) <-chan *Cookie {
cookieChan := make(chan *Cookie)
go func() {
defer close(cookieChan)
for cookie, err := range s {
select {
case <-ctx.Done():
return
default:
}
if err != nil || cookie == nil {
continue
}
cookieChan <- cookie
}
}()
return cookieChan
}
type Cookies []*Cookie
func (c Cookies) Seq() CookieSeq {
return func(yield func(*Cookie, error) bool) {
if c == nil {
return
}
for _, cookie := range c {
if cookie == nil {
continue
}
if !yield(cookie, nil) {
return
}
}
}
}