forked from barnardb/cookies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.go
92 lines (84 loc) · 2.86 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
package main
import (
"net/url"
"strings"
"time"
"github.com/zellyn/kooky"
_ "github.com/zellyn/kooky/allbrowsers"
)
func contains(values []string, value string) bool {
for _, v := range values {
if v == value {
return true
}
}
return false
}
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)
filter := currentlyAppliesToURLAndName(url, name, logger.RequireVerbosity(2))
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)
}
logger.Printf("Found %d matching cookie(s)", len(results))
return
}
func currentlyAppliesToURLAndName(url *url.URL, name string, logger *Logger) kooky.Filter {
currentTime := time.Now()
logger.Printf("Current time is %v", currentTime)
return appliesToURLAndNameAtTime(url, name, currentTime, logger)
}
func appliesToURLAndNameAtTime(url *url.URL, name string, time time.Time, logger *Logger) kooky.Filter {
urlIsNotSecure := url.Scheme != "https"
return func(cookie *kooky.Cookie) bool {
if !hostMatchesDomain(url.Host, cookie.Domain) {
logger.Printf("Rejecting cookie for non-matching domain: %v", cookie)
} else if urlIsNotSecure && cookie.Secure {
logger.Printf("Rejecting secure cookie for non-HTTPS URL: %v", cookie)
} else if !(cookie.Expires.IsZero() || time.Before(cookie.Expires)) {
logger.Printf("Rejecting expired cookie: %v", cookie)
} else if url.Path != "" && !strings.HasPrefix(url.Path, cookie.Path) {
logger.Printf("Rejecting cookie due to unmatched path: %v", cookie)
} else if name != "" && cookie.Name != name {
logger.Printf("Rejecting cookie due to unmatched name: %v", cookie)
} else {
logger.Printf("Accepting: %v", cookie)
return true
}
return false
}
}
func hostMatchesDomain(host string, domain string) bool {
return host == domain ||
(strings.HasPrefix(domain, ".") && (strings.HasSuffix(host, domain) || host == domain[1:]))
}