useragents/useragents.go

88 lines
1.9 KiB
Go
Raw Normal View History

2023-10-27 16:03:42 +02:00
package useragents
import (
"encoding/json"
"math/rand"
2023-10-28 15:42:01 +02:00
"strings"
2023-10-27 16:03:42 +02:00
"time"
"github.com/gocolly/colly"
)
type userAgentEntry struct {
UserAgent string `json:"ua"`
Percentage float64 `json:"pct"`
}
var defaultUserAgent string = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
var userAgents []string = getFreshUserAgents()
func GetRandomUserAgent() string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return userAgents[r.Intn(len(userAgents))]
}
func getFreshUserAgents() []string {
var freshUserAgents []string = []string{}
c := colly.NewCollector(
colly.MaxDepth(1),
colly.AllowedDomains("www.useragents.me"),
colly.UserAgent(defaultUserAgent),
)
c.OnRequest(func(r *colly.Request) {
r.Headers.Set("Accept-Language", "de,en-US;q=0.7,en;q=0.3")
})
c.OnHTML("#most-common-desktop-useragents-json-csv", func(e *colly.HTMLElement) {
e.ForEach("textarea", func(n int, textarea *colly.HTMLElement) {
if n > 0 {
return
}
var rawData []userAgentEntry
err := json.Unmarshal([]byte(textarea.Text), &rawData)
if err != nil {
return
}
for _, e := range rawData {
2023-10-28 15:42:01 +02:00
if !shouldBeValidUseragent(e.UserAgent) {
continue
2023-10-27 16:03:42 +02:00
}
2023-10-28 15:42:01 +02:00
freshUserAgents = append(freshUserAgents, e.UserAgent)
2023-10-27 16:03:42 +02:00
}
})
})
c.Visit("https://www.useragents.me/")
if len(freshUserAgents) == 0 {
return []string{defaultUserAgent}
}
return freshUserAgents
}
2023-10-28 15:42:01 +02:00
func shouldBeValidUseragent(s string) bool {
if !strings.Contains(s, "Mozilla/") {
return false
}
if !strings.Contains(s, "Linux") && !strings.Contains(s, "Macintosh") && !strings.Contains(s, "Windows NT") {
return false
}
if !strings.Contains(s, "AppleWebKit") && !strings.Contains(s, "Gecko") {
return false
}
if !strings.Contains(s, "Firefox") && !strings.Contains(s, "Chrome") && !strings.Contains(s, "Safari") {
return false
}
return true
}