-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.go
145 lines (120 loc) · 2.82 KB
/
browser.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
package gocookie
import (
"errors"
"fmt"
"net/http"
"os"
"strings"
)
type Browser interface {
GetName() string
GetCookies(domainFilter) ([]*http.Cookie, error)
}
type BrowserType string
const (
Safari BrowserType = "Safari"
Chrome BrowserType = "Chrome"
Edge BrowserType = "Edge"
Chromium BrowserType = "Chromium"
Firefox BrowserType = "Firefox"
)
func NewBrowser(browserType BrowserType, cookiePath string, secretKey []byte) (Browser, error) {
if cookiePath == "" {
cookiePath = GetCookieFilePath(browserType)
}
if cookiePath == "" {
return nil, errors.New("empty cookie path")
}
if _, err := os.Stat(cookiePath); os.IsNotExist(err) {
return nil, fmt.Errorf("cookie file not found: %s", cookiePath)
}
switch browserType {
case Safari:
return newSafari(cookiePath), nil
case Chrome:
if len(secretKey) == 0 {
var err error
if secretKey, err = GetChromeSecretKey(cookiePath); err != nil {
return nil, err
}
}
return newChromium(string(Chrome), cookiePath, secretKey)
case Edge:
if len(secretKey) == 0 {
var err error
if secretKey, err = GetEdgeSecretKey(cookiePath); err != nil {
return nil, err
}
}
return newChromium(string(Edge), cookiePath, secretKey)
case Chromium:
return newChromium(string(Chromium), cookiePath, secretKey)
case Firefox:
return newFirefox(cookiePath), nil
default:
return nil, errors.New("unsupported browser type")
}
}
type Option func(*Options)
type Options struct {
CookiePath string
SecretKey []byte
Domains []string
DomainSuffix []string
DomainContains []string
}
type domainFilter func(domain string) bool
func (o *Options) DomainFilter() domainFilter {
if len(o.Domains) == 0 && len(o.DomainSuffix) == 0 && len(o.DomainContains) == 0 {
return nil
}
return func(domain string) bool {
if len(o.Domains) > 0 {
for _, d := range o.Domains {
if d == domain {
return true
}
}
}
if len(o.DomainSuffix) > 0 {
for _, suffix := range o.DomainSuffix {
if strings.HasSuffix(domain, suffix) {
return true
}
}
}
if len(o.DomainContains) > 0 {
for _, contains := range o.DomainContains {
if strings.Contains(domain, contains) {
return true
}
}
}
return false
}
}
func WithCookiePath(path string) Option {
return func(o *Options) {
o.CookiePath = path
}
}
func WithSecretKey(secretKey []byte) Option {
return func(o *Options) {
o.SecretKey = secretKey
}
}
func WithDomain(domains ...string) Option {
return func(o *Options) {
o.Domains = append(o.Domains, domains...)
}
}
func WithDomainSuffix(suffixes ...string) Option {
return func(o *Options) {
o.DomainSuffix = append(o.DomainSuffix, suffixes...)
}
}
func WithDomainContains(contains ...string) Option {
return func(o *Options) {
o.DomainContains = append(o.DomainContains, contains...)
}
}