diff --git a/src/search/useragent/useragent.go b/src/search/useragent/useragent.go index 134c756b..6a6fea18 100644 --- a/src/search/useragent/useragent.go +++ b/src/search/useragent/useragent.go @@ -1,10 +1,17 @@ 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 @@ -12,43 +19,45 @@ type userAgentWithHeaders struct { 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 {