-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
149 lines (124 loc) · 3.07 KB
/
utils.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package websitepoller
import (
"math/rand"
"net/http"
"net/url"
"strings"
"time"
randomdata "github.com/Pallinder/go-randomdata"
)
func parseHTTPMethod(method *string) (string, error) {
m := "GET"
if method != nil {
if len(*method) > 0 {
m = *method
}
}
switch m := strings.ToUpper(m); m {
case http.MethodGet:
return http.MethodGet, nil
case http.MethodHead:
return http.MethodHead, nil
case http.MethodPost:
return http.MethodPost, nil
case http.MethodPut:
return http.MethodPut, nil
case http.MethodPatch:
return http.MethodPatch, nil
case http.MethodDelete:
return http.MethodDelete, nil
case http.MethodConnect:
return http.MethodConnect, nil
case http.MethodOptions:
return http.MethodOptions, nil
case http.MethodTrace:
return http.MethodTrace, nil
default:
return "", ErrUnrecognizedHTTPMethod
}
}
func parseURL(rawurl string) (*url.URL, error) {
parsed, err := url.Parse(rawurl)
if err != nil {
return nil, err
}
if !parsed.IsAbs() {
return nil, ErrURLNoScheme
}
return parsed, nil
}
func parsePollOptions(id string, opts *PollOptions) (randFreq bool, freq int, offset int) {
l := log.With().Str("id", id).Logger()
randFreq, freq, offset = false, defaultFrequency, 0
if opts == nil {
return
}
if opts.Frequency >= minFrequency {
freq = opts.Frequency
} else {
l.Error().Int("frequency", freq).Int("default", defaultFrequency).Msg("invalid frequency provided, using default value...")
}
if !opts.RandomFrequency {
return
}
randFreq = true
offset = defaultOffsetRange
if opts.OffsetRange != nil {
if *opts.OffsetRange >= minOffset {
offset = *opts.OffsetRange
} else {
l.Warn().Int("offset", *opts.OffsetRange).Int("default", defaultOffsetRange).Msg("invalid offset range provided, reverting to default...")
}
}
if freq-offset >= minFrequency {
return
}
l.Warn().Int("range", offset).Int("frequency", freq).Msg("offset is too low, reverting to default...")
offset = defaultOffsetRange
freq = defaultFrequency
return
}
func parseUserAgentOptions(id string, opts *UserAgentOptions) (randUA bool, uas []string) {
l := log.With().Str("id", id).Logger()
randUA, uas = false, []string{}
if opts == nil {
l.Warn().Msg("no user agents provided, you should provide at least one or enable random user agents")
return
}
randUA = opts.RandomUA
if len(opts.UserAgents) > 0 {
uas = opts.UserAgents
return
}
if randUA {
return
}
l.Warn().Msg("no user agents provided, you should provide at least one or enable random user agents")
return
}
func nextRandomTick(min, max int) int {
rand.Seed(time.Now().UnixNano())
return min + rand.Intn(max-min)
}
func getNextUA(id string, userAgents []string, random bool, last int) (ua string, index int) {
ua, index = "", -1
if len(userAgents) == 0 {
if random {
ua = randomdata.UserAgentString()
}
return
}
length := len(userAgents)
index = last
if !random {
index = (index + 1) % length
ua = userAgents[index]
} else {
rand.Seed(time.Now().UnixNano())
for index == last {
index = rand.Intn(length - 1)
}
ua = userAgents[index]
}
return
}