From ff92edd9e55b7dccd50b9b75f083e17c46c4ce5e Mon Sep 17 00:00:00 2001 From: NormalPunch <39510490+NormalPunch@users.noreply.github.com> Date: Tue, 29 Sep 2020 15:12:14 +0800 Subject: [PATCH] add normalized user-agent --- examples_test.go | 10 ++++++++-- main.go | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/examples_test.go b/examples_test.go index c12a159..5d7ee0b 100644 --- a/examples_test.go +++ b/examples_test.go @@ -2,6 +2,7 @@ package bypass_test import ( "fmt" + "strings" "time" "github.com/go-rod/bypass" @@ -26,7 +27,7 @@ func Example_main() { js file size: 112395 - User Agent (Old): Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 + User Agent (Old): true WebDriver (New): missing (passed) @@ -56,7 +57,12 @@ func printReport(page *rod.Page) { el := page.MustElement("#broken-image-dimensions.passed") for _, row := range el.MustParents("table").First().MustElements("tr:nth-child(n+2)") { cells := row.MustElements("td") - fmt.Printf("\t\t%s: %s\n\n", cells[0].MustProperty("textContent"), cells[1].MustProperty("textContent")) + key := cells[0].MustProperty("textContent") + if strings.HasPrefix(key.String(), "User Agent") { + fmt.Printf("\t\t%s: %t\n\n", key, !strings.Contains(cells[1].MustProperty("textContent").String(), "HeadlessChrome/")) + } else { + fmt.Printf("\t\t%s: %s\n\n", key, cells[1].MustProperty("textContent")) + } } page.MustScreenshot("") diff --git a/main.go b/main.go index 6eec74c..27350a9 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,8 @@ package bypass import ( "github.com/go-rod/rod" + "github.com/go-rod/rod/lib/proto" + "strings" ) // Page creates a stealth page that can't be detected as bot. @@ -18,7 +20,14 @@ func Page(b *rod.Browser) (*rod.Page, error) { return nil, err } - err = p.SetUserAgent(nil) + ua, err := NormalizeUA(b) + if err != nil { + return nil, err + } + + err = p.SetUserAgent(&proto.NetworkSetUserAgentOverride{ + UserAgent: ua, + }) if err != nil { return nil, err } @@ -34,3 +43,12 @@ func MustPage(b *rod.Browser) *rod.Page { } return p } + +// NormalizeUA normalize the default user-agent, as a head mode browser. +func NormalizeUA(b *rod.Browser) (string, error) { + v, err := proto.BrowserGetVersion{}.Call(b) + if err != nil { + return "", err + } + return strings.ReplaceAll(v.UserAgent, "HeadlessChrome/", "Chrome/"), nil +}