lwb/useragent.go
Frank Mayer b4bcc7406a
All checks were successful
Go Test / build (push) Successful in 9m59s
example
2025-02-06 19:31:32 +01:00

73 lines
2.0 KiB
Go

package lwb
import (
"fmt"
"runtime"
)
// getSystemInfo returns a string describing the OS and architecture
// in a style similar to what many browsers use in their UA string.
func getSystemInfo() string {
os := runtime.GOOS
arch := runtime.GOARCH
switch os {
case "windows":
// We assume Windows NT 10.0 for simplicity.
if arch == "amd64" {
return "Windows NT 10.0; Win64; x64"
} else if arch == "386" {
return "Windows NT 10.0"
} else {
return "Windows NT 10.0; " + arch
}
case "linux":
// Many Linux browsers use "X11" in the UA string.
if arch == "amd64" {
return "X11; Linux x86_64"
} else if arch == "386" {
return "X11; Linux i686"
} else {
return "X11; Linux " + arch
}
case "darwin":
// For macOS we have to invent a version number since Go doesn't provide one.
// Here we assume a recent macOS version.
if arch == "amd64" {
return "Macintosh; Intel Mac OS X 10_15_7"
} else if arch == "arm64" {
return "Macintosh; ARM Mac OS X 11_0"
} else {
return "Macintosh; Mac OS X"
}
default:
// For other operating systems, just output the raw GOOS and GOARCH.
return fmt.Sprintf("%s; %s", os, arch)
}
}
// BuildFirefoxUserAgent builds a Firefox UA string using the given version.
// Example output on Windows:
// "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0"
func BuildFirefoxUserAgent(firefoxVersion string) string {
systemInfo := getSystemInfo()
return fmt.Sprintf(
"Mozilla/5.0 (%s; rv:%s) Gecko/20100101 Firefox/%s",
systemInfo,
firefoxVersion,
firefoxVersion,
)
}
// BuildChromeUserAgent builds a Chrome UA string using the given version.
// Example output on Windows:
// "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.6943.53 Safari/537.36"
func BuildChromeUserAgent(chromeVersion string) string {
systemInfo := getSystemInfo()
return fmt.Sprintf(
"Mozilla/5.0 (%s) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.36",
systemInfo,
chromeVersion,
)
}