Skip to content

Commit

Permalink
add normalized user-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
NormalPunch authored and ysmood committed Sep 29, 2020
1 parent a7d34c5 commit ff92edd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
10 changes: 8 additions & 2 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bypass_test

import (
"fmt"
"strings"
"time"

"github.com/go-rod/bypass"
Expand All @@ -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)
Expand Down Expand Up @@ -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("")
Expand Down
20 changes: 19 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand All @@ -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
}

0 comments on commit ff92edd

Please sign in to comment.