updated indexing & user agent generator
This commit is contained in:
parent
13e1d6119b
commit
a9a6948a44
3 changed files with 73 additions and 35 deletions
43
agent.go
43
agent.go
|
@ -3,7 +3,7 @@ package main
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"sort"
|
||||
|
@ -40,13 +40,33 @@ var (
|
|||
func fetchLatestBrowserVersions() (BrowserData, error) {
|
||||
url := "https://raw.githubusercontent.com/Fyrd/caniuse/master/fulldata-json/data-2.0.json"
|
||||
|
||||
resp, err := http.Get(url)
|
||||
// // Optional: skip TLS verification to avoid certificate errors
|
||||
// transport := &http.Transport{
|
||||
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
// }
|
||||
|
||||
// Increase the HTTP client timeout
|
||||
client := &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
// Transport: transport,
|
||||
}
|
||||
|
||||
// Build the request manually to set headers
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return BrowserData{}, err
|
||||
}
|
||||
// Custom user agent and English language preference
|
||||
req.Header.Set("User-Agent", "MyCustomAgent/1.0 (compatible; +https://example.com)")
|
||||
req.Header.Set("Accept-Language", "en-US,en;q=0.9")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return BrowserData{}, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return BrowserData{}, err
|
||||
}
|
||||
|
@ -109,7 +129,7 @@ func randomUserAgent() (string, error) {
|
|||
return "", err
|
||||
}
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
rand := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
// Simulated browser usage statistics (in percentages)
|
||||
usageStats := map[string]float64{
|
||||
|
@ -161,6 +181,7 @@ func randomUserAgent() (string, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// Fallback to the last version if none matched
|
||||
if version == "" {
|
||||
version = versions[len(versions)-1].Version
|
||||
}
|
||||
|
@ -240,11 +261,11 @@ func updateUserAgentVersion(userAgent string, newVersions BrowserData) string {
|
|||
browserType = "Firefox"
|
||||
}
|
||||
|
||||
// Get the latest version for the browser type
|
||||
// Get the latest version for that browser
|
||||
var latestVersion string
|
||||
if browserType == "Firefox" {
|
||||
if browserType == "Firefox" && len(newVersions.Firefox) > 0 {
|
||||
latestVersion = newVersions.Firefox[0].Version
|
||||
} else if browserType == "Chromium" {
|
||||
} else if browserType == "Chromium" && len(newVersions.Chromium) > 0 {
|
||||
latestVersion = newVersions.Chromium[0].Version
|
||||
}
|
||||
|
||||
|
@ -252,7 +273,7 @@ func updateUserAgentVersion(userAgent string, newVersions BrowserData) string {
|
|||
return generateUserAgent(browserType, latestVersion)
|
||||
}
|
||||
|
||||
func periodicUpdate() {
|
||||
func periodicAgentUpdate() {
|
||||
for {
|
||||
// Sleep for a random interval between 1 and 2 days
|
||||
time.Sleep(time.Duration(24+rand.Intn(24)) * time.Hour)
|
||||
|
@ -309,12 +330,8 @@ func GetNewUserAgent(cacheKey string) (string, error) {
|
|||
return userAgent, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
go periodicUpdate()
|
||||
}
|
||||
|
||||
// func main() {
|
||||
// go periodicUpdate() // not needed here
|
||||
// go periodicAgentUpdate() // not needed here
|
||||
|
||||
// cacheKey := "image-search"
|
||||
// userAgent, err := GetUserAgent(cacheKey)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue