-
Notifications
You must be signed in to change notification settings - Fork 0
/
roulette.go
101 lines (82 loc) · 1.5 KB
/
roulette.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package roulette
import (
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"sync"
"time"
)
var (
userAgents []string
userAgentsLen int
rng *rand.Rand
mu sync.Mutex
)
var domainName = []string{
"google",
"duckduckgo",
"yahoo",
"bing",
"baidu",
"yandex",
"reddit",
"pinterest",
"quora",
"twitter",
"facebook",
}
var tld = []string{
"com",
"net",
"co",
"org",
}
var (
domainLen = len(domainName)
tldLen = len(tld)
)
func init() {
randSource := rand.NewSource(time.Now().UnixNano())
rng = rand.New(randSource)
if err := InitUserAgents(); err != nil {
fmt.Println("Error fetching user agents:", err)
}
}
// Fetch user agents
func InitUserAgents() error {
resp, err := http.Get("https://cdn.jsdelivr.net/gh/microlinkhq/top-user-agents@master/src/index.json")
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
mu.Lock()
defer mu.Unlock()
if err := json.Unmarshal(body, &userAgents); err != nil {
return err
}
userAgentsLen = len(userAgents)
return nil
}
// Return random user agent
func GetUserAgent() string {
mu.Lock()
defer mu.Unlock()
if userAgentsLen == 0 {
return ""
}
return userAgents[rng.Intn(userAgentsLen)]
}
// Return random referrer
func GetReferrer() string {
mu.Lock()
defer mu.Unlock()
selectedDomain := domainName[rand.Intn(domainLen)]
selectedTLD := tld[rand.Intn(tldLen)]
return fmt.Sprintf("https://www.%s.%s/", selectedDomain, selectedTLD)
}