-
Notifications
You must be signed in to change notification settings - Fork 42
/
find.go
239 lines (219 loc) · 4.58 KB
/
find.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
package kooky
import (
"context"
"fmt"
"iter"
"net/http"
"sync"
)
// CookieStore represents a file, directory, etc containing cookies.
//
// Call CookieStore.Close() after using any of its methods.
type CookieStore interface {
http.CookieJar
BrowserInfo
SubJar(context.Context, ...Filter) (http.CookieJar, error)
TraverseCookies(...Filter) CookieSeq
Close() error
}
type BrowserInfo interface {
Browser() string
Profile() string
IsDefaultProfile() bool
FilePath() string
}
// CookieStoreFinder tries to find cookie stores at default locations.
type CookieStoreFinder interface {
FindCookieStores() CookieStoreSeq
}
var (
finders = map[string]CookieStoreFinder{}
muFinder sync.RWMutex
)
// RegisterFinder() registers CookieStoreFinder enabling automatic finding of
// cookie stores with FindAllCookieStores() and ReadCookies().
//
// RegisterFinder() is called by init() in the browser subdirectories.
func RegisterFinder(browser string, finder CookieStoreFinder) {
muFinder.Lock()
defer muFinder.Unlock()
if finder != nil {
finders[browser] = finder
}
}
// FindAllCookieStores() tries to find cookie stores at default locations.
//
// FindAllCookieStores() requires registered CookieStoreFinders.
//
// 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 FindAllCookieStores(ctx context.Context) []CookieStore {
return TraverseCookieStores(ctx).AllCookieStores(ctx)
}
type CookieStoreSeq iter.Seq2[CookieStore, error]
// sequence of non-nil cookie stores and nil errors
func (s CookieStoreSeq) OnlyCookieStores() CookieStoreSeq {
return func(yield func(CookieStore, error) bool) {
if s == nil {
return
}
for cookieStore, err := range s {
if err != nil || cookieStore == nil {
continue
}
if !yield(cookieStore, nil) {
return
}
}
}
}
func (s CookieStoreSeq) AllCookieStores(ctx context.Context) []CookieStore {
var ret []CookieStore
if s == nil {
return nil
}
Outer:
for cookieStore, _ := range s {
select {
case <-ctx.Done():
break Outer
default:
}
if cookieStore == nil {
continue
}
ret = append(ret, cookieStore)
}
return ret
}
func (s CookieStoreSeq) TraverseCookies(ctx context.Context, filters ...Filter) CookieSeq {
if s == nil {
return func(yield func(*Cookie, error) bool) {}
}
ctx, cancel := context.WithCancel(ctx)
type ce struct {
c *Cookie
e error
}
startChan := make(chan struct{}, 1)
cookieChan := make(chan ce, 1)
go func() {
select {
case <-ctx.Done():
return
case <-startChan: // wait for iteration start
}
var wg sync.WaitGroup
defer func() {
wg.Wait()
cancel()
close(cookieChan)
}()
for cookieStore, err := range s {
if err != nil {
select {
case <-ctx.Done():
return
case cookieChan <- ce{e: fmt.Errorf(`cookie store: %w`, err)}:
}
continue
}
if cookieStore == nil {
continue
}
wg.Add(1)
go func(cookieStore CookieStore) {
defer wg.Done()
for cookie, err := range cookieStore.TraverseCookies(filters...) {
select {
case <-ctx.Done():
return
case cookieChan <- ce{c: cookie, e: err}:
}
}
}(cookieStore)
}
}()
return func(yield func(*Cookie, error) bool) {
startChan <- struct{}{}
for {
select {
case <-ctx.Done():
return
case c, ok := <-cookieChan:
if !ok {
cancel()
return
}
if !yield(c.c, c.e) {
cancel()
return
}
}
}
}
}
func TraverseCookieStores(ctx context.Context) CookieStoreSeq {
ctx, cancel := context.WithCancel(ctx)
type se struct {
s CookieStore
e error
}
startChan := make(chan struct{}, 1)
storeChan := make(chan se, 1)
go func() {
select {
case <-ctx.Done():
return
case <-startChan:
}
var wg sync.WaitGroup
wg.Add(len(finders))
defer func() {
wg.Wait()
cancel()
close(storeChan)
}()
muFinder.RLock()
defer muFinder.RUnlock()
for _, finder := range finders {
if finder == nil {
wg.Done()
continue
}
go func(finder CookieStoreFinder) {
defer wg.Done()
for cookieStore, err := range finder.FindCookieStores() {
select {
case <-ctx.Done():
return
case storeChan <- se{s: cookieStore, e: err}:
}
}
}(finder)
}
}()
return func(yield func(CookieStore, error) bool) {
startChan <- struct{}{}
for {
select {
case <-ctx.Done():
return
case s, ok := <-storeChan:
if !ok {
cancel()
return
}
if !yield(s.s, s.e) {
cancel()
return
}
}
}
}
}