-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom-agent.go
69 lines (57 loc) · 1.56 KB
/
random-agent.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
package main
import (
"bufio"
"embed"
"fmt"
"math/rand"
"strings"
"time"
flag "github.com/spf13/pflag"
)
//go:embed user-agents.txt
var userAgentsFS embed.FS
var (
userAgentsList []string
)
func main() {
var matchStrings, filterStrings []string
flag.StringSliceVarP(&matchStrings, "match", "m", nil, "Only return user-agents matching the specified string. Can be specified multiple times.")
flag.StringSliceVarP(&filterStrings, "filter", "f", nil, "Filter out user-agents matching the specified string. Can be specified multiple times.")
flag.Parse()
userAgentsList, _ = loadUserAgents()
random_source := rand.NewSource(time.Now().UnixNano())
rand := rand.New(random_source)
rand.Shuffle(len(userAgentsList), func(i, j int) {
userAgentsList[i], userAgentsList[j] = userAgentsList[j], userAgentsList[i]
})
for _, userAgent := range userAgentsList {
if len(matchStrings) == 0 && len(filterStrings) == 0 {
fmt.Print(userAgent)
break
} else if matchesFilter(userAgent, matchStrings) && !matchesFilter(userAgent, filterStrings) {
fmt.Print(userAgent)
break
}
}
}
func loadUserAgents() ([]string, error) {
file, err := userAgentsFS.Open("user-agents.txt")
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func matchesFilter(userAgent string, filters []string) bool {
for _, filter := range filters {
if strings.Contains(userAgent, filter) {
return true
}
}
return len(filters) == 0
}