From 31329b3b734380d938465e22ce0d01585ecef4c8 Mon Sep 17 00:00:00 2001 From: Yad Smood Date: Tue, 25 Jul 2023 13:22:55 +0800 Subject: [PATCH] fix: default bypass --- cmd/serve/main.go | 4 +++- service.go | 46 +++++++++++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/cmd/serve/main.go b/cmd/serve/main.go index 7bf78ad..de2b309 100644 --- a/cmd/serve/main.go +++ b/cmd/serve/main.go @@ -16,7 +16,7 @@ func main() { size := flag.Int("s", 2, "size of the pool") maxWait := flag.Duration("w", 3*time.Second, "max wait time for a page rendering") - var bypassUAs StringsFlag + var bypassUAs StringsFlag = bartender.DefaultBypassUserAgentNames flag.Var(&bypassUAs, "u", "bypass the specified user-agent names") var blockList StringsFlag @@ -30,6 +30,8 @@ func main() { } log.Printf("Bartender started %s -> %s\n", *port, *target) + log.Printf("Block list: %v\n", blockList) + log.Printf("Bypass user-agent names: %v\n", bypassUAs) b := bartender.New(*port, *target, *size) b.BlockRequests(blockList...) diff --git a/service.go b/service.go index 211e2fc..d998d28 100644 --- a/service.go +++ b/service.go @@ -16,6 +16,19 @@ import ( "github.com/mileusna/useragent" ) +var DefaultBypassUserAgentNames = []string{ + useragent.Opera, + useragent.OperaMini, + useragent.OperaTouch, + useragent.Chrome, + useragent.HeadlessChrome, + useragent.Firefox, + useragent.InternetExplorer, + useragent.Safari, + useragent.Edge, + useragent.Vivaldi, +} + type Bartender struct { addr string target *url.URL @@ -35,21 +48,10 @@ func New(addr, target string, poolSize int) *Bartender { proxy := httputil.NewSingleHostReverseProxy(u) return &Bartender{ - addr: addr, - target: u, - proxy: proxy, - bypassList: map[string]bool{ - useragent.Opera: true, - useragent.OperaMini: true, - useragent.OperaTouch: true, - useragent.Chrome: true, - useragent.HeadlessChrome: true, - useragent.Firefox: true, - useragent.InternetExplorer: true, - useragent.Safari: true, - useragent.Edge: true, - useragent.Vivaldi: true, - }, + addr: addr, + target: u, + proxy: proxy, + bypassList: strToMap(DefaultBypassUserAgentNames), pool: rod.NewPagePool(poolSize), blockRequests: []string{}, maxWait: 3 * time.Second, @@ -57,10 +59,7 @@ func New(addr, target string, poolSize int) *Bartender { } func (b *Bartender) BypassUserAgentNames(list ...string) { - b.bypassList = map[string]bool{} - for _, ua := range list { - b.bypassList[ua] = true - } + b.bypassList = strToMap(list) } func (b *Bartender) BlockRequests(patterns ...string) { @@ -198,3 +197,12 @@ func getHeader(ctx context.Context, u string) (int, http.Header) { func htmlContentType(h http.Header) bool { return strings.Contains(h.Get("Content-Type"), "text/html") } + +func strToMap(list []string) map[string]bool { + m := map[string]bool{} + for _, s := range list { + m[s] = true + } + + return m +}