Skip to content

Commit

Permalink
Robustness, speed and efficiency
Browse files Browse the repository at this point in the history
This commit switches to the `visitors` branch on my fork of kooky
(https://github.com/barnardb/kooky/tree/visitors). This brings with it
the robustness improvements from browserutils/kooky#43
and also allows us to rewrite our finding code to avoid decrypting
cookie values we don't use, to get more concurrency, and to avoid
finding cookie stores for browsers we aren't interested in.

I need to work on making upstream PRs for the new functionality.
  • Loading branch information
barnardb committed Jan 25, 2021
1 parent 659ada5 commit 75110da
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 39 deletions.
79 changes: 40 additions & 39 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,54 @@ import (
_ "github.com/zellyn/kooky/allbrowsers"
)

func storesForBrowsers(names []string) []kooky.CookieStore {
// kooky doesn't currently expose its CookieStoreFinder implementations,
// so we have to filter the stores after running the finders,
// rather than being able to select only the finders we're interested.
// (kooky currently supports selecting which finders to use based on what
// modules are imported, which doesn't meet our use case.)
stores := kooky.FindAllCookieStores()
if names == nil {
return stores
}
n := 0
STORES:
for _, store := range stores {
browser := store.Browser()
for _, name := range names {
if browser == name {
stores[n] = store
n++
continue STORES
}
func contains(values []string, value string) bool {
for _, v := range values {
if v == value {
return true
}
}
return stores[:n]
return false
}

func findCookies(url *url.URL, name string, browsers []string, logger *Logger) (cookies []*kooky.Cookie) {
func findCookies(url *url.URL, name string, browsers []string, logger *Logger) (results []*kooky.Cookie) {
logger.Printf("Looking for browsers %v", browsers)
logger.Printf("Looking for cookies for URL %s", url)

stores := storesForBrowsers(browsers)
logger.Printf("Found %v cookie store(s)", len(stores))

filter := currentlyAppliesToURLAndName(url, name, logger.RequireVerbosity(2))
for _, store := range stores {
logger.Printf("Loading cookies from %v", store)
cookies, err := store.ReadCookies(filter)
if err != nil {
logger.Printf("Error loading cookies from %v: %s", store, err)
continue
}
logger.Printf("Found %d matching cookie(s)", len(cookies))

if len(cookies) > 0 {
return cookies
}
cookies := make(chan *kooky.Cookie)
go func() {
kooky.ConcurrentlyVisitFinders(func(name string, finder kooky.CookieStoreFinder) {
if !contains(browsers, name) {
return
}
logger.Printf("Looking for %s cookie stores", name)
kooky.ConcurrentlyVisitStores(finder, func(store kooky.CookieStore) {
logger.Printf("Loading cookies from %v", store)
err := store.VisitCookies(func(cookie *kooky.Cookie, initializeValue kooky.CookieValueInitializer) error {
if !filter(cookie) {
return nil
}
err := initializeValue(cookie)
if err == nil {
cookies <- cookie
}
return err
})
if err != nil {
logger.Printf("Error loading cookies from %v: %s", store, err)
} else {
logger.Printf("Done loading cookies from %v", store)
}
})
logger.Printf("Done loading from %s cookie stores", name)
})
close(cookies)
}()
for cookie := range cookies {
results = append(results, cookie)
}

return []*kooky.Cookie{}
logger.Printf("Found %d matching cookie(s)", len(results))
return
}

func currentlyAppliesToURLAndName(url *url.URL, name string, logger *Logger) kooky.Filter {
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ require (
github.com/spf13/pflag v1.0.5
github.com/zellyn/kooky v0.0.0-20201108220156-bec09c12c339
)

// We are currently using the head of <https://github.com/barnardb/kooky/tree/visitors>.
// I decided to use my fork while waiting for https://github.com/zellyn/kooky/pull/43
// to be merged, but then decided to do some work to allow the kind of
// parallelism and defered decrypting I wanted to have. The result is a
// speed-up taking a typical use case of mine from ~1200ms to ~100ms.
// I intend to try to contribute these changes back upstream.
replace github.com/zellyn/kooky => github.com/barnardb/kooky v0.0.0-20210125015714-b0182bf77e67
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/barnardb/kooky v0.0.0-20210125015714-b0182bf77e67 h1:sBislMisEMpXDgZwsfKDe+iIqZD5wjBaGCTSwrs+0cI=
github.com/barnardb/kooky v0.0.0-20210125015714-b0182bf77e67/go.mod h1:QM4+3a3KkwFXtuTz+w41LBKU+MW7KecHBn0bNalcPKA=
github.com/bobesa/go-domain-util v0.0.0-20190911083921-4033b5f7dd89 h1:2pkAuIM8OF1fy4ToFpMnI4oE+VeUNRbGrpSLKshK0oQ=
github.com/bobesa/go-domain-util v0.0.0-20190911083921-4033b5f7dd89/go.mod h1:/09nEjna1UMoasyyQDhOrIn8hi2v2kiJglPWed1idck=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down

0 comments on commit 75110da

Please sign in to comment.