Skip to content

Commit

Permalink
Merge pull request #387 from hearchco/as/fix/ua
Browse files Browse the repository at this point in the history
fix(useragent): better generation, with a fix
  • Loading branch information
aleksasiriski authored Sep 1, 2024
2 parents 9716d2e + 0ce0d61 commit 056af3b
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions src/search/useragent/useragent.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,63 @@
package useragent

import (
"fmt"
"math/rand"
"slices"
"time"

"github.com/rs/zerolog/log"
)

var browsers = [...]string{"chrome", "edge"}
var versions = [...]int{127, 128}

type userAgentWithHeaders struct {
UserAgent string
SecCHUA string
SecCHUAMobile string
SecCHUAPlatform string
}

// UserAgents used when making requests and their corresponding Sec-Ch-Ua headers.
var userAgentArray = [...]userAgentWithHeaders{
// Chrome 127.0.0, Mac OS X
{
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.3",
`"Google Chrome";v="127", "Chromium";v="127", "Not=A?Brand";v="24"`,
"?0",
`"Macintosh"`,
},
// Chrome 128.0.0, Windows
{
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.3",
`"Google Chrome";v="128", "Chromium";v="128", "Not=A?Brand";v="24"`,
"?0",
`"Windows"`,
},
// Chrome 127.0.0, Windows
{
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.3",
`"Google Chrome";v="127", "Chromium";v="127", "Not=A?Brand";v="24"`,
"?0",
`"Windows"`,
},
// Edge 128.0.0, Windows
{
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.",
`"Microsoft Edge";v="128", "Edg";v="128"`,
func userAgentStruct(browser string, version int) userAgentWithHeaders {
if !slices.Contains(browsers[:], browser) {
log.Panic().
Str("browser", browser).
Msg("Invalid browser")
// ^PANIC - This should never happen
}
if !slices.Contains(versions[:], version) {
log.Panic().
Int("version", version).
Msg("Invalid version")
// ^PANIC - This should never happen
}

const userAgentTemplate = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%d.0.0.0 Safari/537.36"
userAgent := fmt.Sprintf(userAgentTemplate, version)
if browser == "edge" {
userAgent = fmt.Sprintf("%s Edg/%d.0.0.0", userAgent, version)
}

const secCHUATemplate = `"Chromium";v="%d", "Not;A=Brand";v="24", "%s";v="%d"`
secCHUA := fmt.Sprintf(secCHUATemplate, version, "Google Chrome", version)
if browser == "edge" {
secCHUA = fmt.Sprintf(secCHUATemplate, version, "Microsoft Edge", version)
}

return userAgentWithHeaders{
userAgent,
secCHUA,
"?0",
`"Windows"`,
},
}
}

func randomUserAgentStruct() userAgentWithHeaders {
// WARNING: Will stop working after year 2262.
randSrc := rand.NewSource(time.Now().UnixNano())
randGen := rand.New(randSrc)
return userAgentArray[randGen.Intn(len(userAgentArray))]
return userAgentStruct(browsers[randGen.Intn(len(browsers))], versions[randGen.Intn(len(versions))])
}

func RandomUserAgent() string {
Expand Down

0 comments on commit 056af3b

Please sign in to comment.