Skip to content

Commit cb64ab4

Browse files
committed
optimize BypassUserAgentNames
1 parent 0674631 commit cb64ab4

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

cmd/serve/main.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ 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 block BlockRequestsFlag
20-
flag.Var(&block, "b", "block the requests that match the pattern, such as 'https://a.com/*', can set multiple ones")
19+
var bypassUAs StringsFlag
20+
flag.Var(&bypassUAs, "u", "bypass the specified user-agent names")
21+
22+
var blockList StringsFlag
23+
flag.Var(&blockList, "b",
24+
"block the requests that match the pattern, such as 'https://a.com/*', can set multiple ones")
2125

2226
flag.Parse()
2327

@@ -28,7 +32,8 @@ func main() {
2832
log.Printf("Bartender started %s -> %s\n", *port, *target)
2933

3034
b := bartender.New(*port, *target, *size)
31-
b.BlockRequest(block...)
35+
b.BlockRequests(blockList...)
36+
b.BypassUserAgentNames(bypassUAs...)
3237
b.MaxWait(*maxWait)
3338
b.WarnUp()
3439

@@ -38,13 +43,13 @@ func main() {
3843
}
3944
}
4045

41-
type BlockRequestsFlag []string
46+
type StringsFlag []string
4247

43-
func (i *BlockRequestsFlag) String() string {
48+
func (i *StringsFlag) String() string {
4449
return strings.Join(*i, ", ")
4550
}
4651

47-
func (i *BlockRequestsFlag) Set(value string) error {
52+
func (i *StringsFlag) Set(value string) error {
4853
*i = append(*i, value)
4954

5055
return nil

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/go-rod/bartender
33
go 1.20
44

55
require (
6-
github.com/go-rod/rod v0.114.0
6+
github.com/go-rod/rod v0.114.1
77
github.com/mileusna/useragent v1.3.3
88
github.com/ysmood/got v0.34.1
99
)

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/go-rod/rod v0.114.0 h1:P+zLOqsj+vKf4C86SfjP6ymyPl9VXoYKm+ceCeQms6Y=
2-
github.com/go-rod/rod v0.114.0/go.mod h1:aiedSEFg5DwG/fnNbUOTPMTTWX3MRj6vIs/a684Mthw=
1+
github.com/go-rod/rod v0.114.1 h1:osBWr88guzTXAIzwJWVmGZe3/utT9+lqKjkGSBsYMxw=
2+
github.com/go-rod/rod v0.114.1/go.mod h1:aiedSEFg5DwG/fnNbUOTPMTTWX3MRj6vIs/a684Mthw=
33
github.com/mileusna/useragent v1.3.3 h1:hrIVmPevJY3ICS1Ob4yjqJToQiv2eD9iHaJBjxMihWY=
44
github.com/mileusna/useragent v1.3.3/go.mod h1:3d8TOmwL/5I8pJjyVDteHtgDGcefrFUX4ccGOMKNYYc=
55
github.com/ysmood/fetchup v0.2.3 h1:ulX+SonA0Vma5zUFXtv52Kzip/xe7aj4vqT5AJwQ+ZQ=

service.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ func New(addr, target string, poolSize int) *Bartender {
5656
}
5757
}
5858

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

63-
func (b *Bartender) BlockRequest(patterns ...string) {
66+
func (b *Bartender) BlockRequests(patterns ...string) {
6467
b.blockRequests = patterns
6568
}
6669

0 commit comments

Comments
 (0)