Skip to content

Commit 31329b3

Browse files
committed
fix: default bypass
1 parent cb64ab4 commit 31329b3

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

cmd/serve/main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
size := flag.Int("s", 2, "size of the pool")
1717
maxWait := flag.Duration("w", 3*time.Second, "max wait time for a page rendering")
1818

19-
var bypassUAs StringsFlag
19+
var bypassUAs StringsFlag = bartender.DefaultBypassUserAgentNames
2020
flag.Var(&bypassUAs, "u", "bypass the specified user-agent names")
2121

2222
var blockList StringsFlag
@@ -30,6 +30,8 @@ func main() {
3030
}
3131

3232
log.Printf("Bartender started %s -> %s\n", *port, *target)
33+
log.Printf("Block list: %v\n", blockList)
34+
log.Printf("Bypass user-agent names: %v\n", bypassUAs)
3335

3436
b := bartender.New(*port, *target, *size)
3537
b.BlockRequests(blockList...)

service.go

+27-19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ import (
1616
"github.com/mileusna/useragent"
1717
)
1818

19+
var DefaultBypassUserAgentNames = []string{
20+
useragent.Opera,
21+
useragent.OperaMini,
22+
useragent.OperaTouch,
23+
useragent.Chrome,
24+
useragent.HeadlessChrome,
25+
useragent.Firefox,
26+
useragent.InternetExplorer,
27+
useragent.Safari,
28+
useragent.Edge,
29+
useragent.Vivaldi,
30+
}
31+
1932
type Bartender struct {
2033
addr string
2134
target *url.URL
@@ -35,32 +48,18 @@ func New(addr, target string, poolSize int) *Bartender {
3548
proxy := httputil.NewSingleHostReverseProxy(u)
3649

3750
return &Bartender{
38-
addr: addr,
39-
target: u,
40-
proxy: proxy,
41-
bypassList: map[string]bool{
42-
useragent.Opera: true,
43-
useragent.OperaMini: true,
44-
useragent.OperaTouch: true,
45-
useragent.Chrome: true,
46-
useragent.HeadlessChrome: true,
47-
useragent.Firefox: true,
48-
useragent.InternetExplorer: true,
49-
useragent.Safari: true,
50-
useragent.Edge: true,
51-
useragent.Vivaldi: true,
52-
},
51+
addr: addr,
52+
target: u,
53+
proxy: proxy,
54+
bypassList: strToMap(DefaultBypassUserAgentNames),
5355
pool: rod.NewPagePool(poolSize),
5456
blockRequests: []string{},
5557
maxWait: 3 * time.Second,
5658
}
5759
}
5860

5961
func (b *Bartender) BypassUserAgentNames(list ...string) {
60-
b.bypassList = map[string]bool{}
61-
for _, ua := range list {
62-
b.bypassList[ua] = true
63-
}
62+
b.bypassList = strToMap(list)
6463
}
6564

6665
func (b *Bartender) BlockRequests(patterns ...string) {
@@ -198,3 +197,12 @@ func getHeader(ctx context.Context, u string) (int, http.Header) {
198197
func htmlContentType(h http.Header) bool {
199198
return strings.Contains(h.Get("Content-Type"), "text/html")
200199
}
200+
201+
func strToMap(list []string) map[string]bool {
202+
m := map[string]bool{}
203+
for _, s := range list {
204+
m[s] = true
205+
}
206+
207+
return m
208+
}

0 commit comments

Comments
 (0)