/
uabot.go
196 lines (166 loc) · 5.2 KB
/
uabot.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package scenariolib
import (
"errors"
"math/rand"
"time"
)
// DEFAULTTIMEBETWEENVISITS The time for the bot to wait between visits, between 0 and X Seconds
const DEFAULTTIMEBETWEENVISITS int = 300
// DEFAULT_STANDARD_DEVIATION_BETWEEN_VISITS The standard deviation when updating time between visits
const DEFAULT_STANDARD_DEVIATION_BETWEEN_VISITS int = 150
// WEEKEND_MODIFIER The modifier to multiply DEFAULTTIMEBETWEENVISITS during weekends
const WEEKEND_MODIFIER = 10
// Uabot is the interface that allows you to run a bot.
type Uabot interface {
Run(quitChannel chan bool) error
}
type uabot struct {
local bool
scenarioURL string
searchToken string
analyticsToken string
WaitBetweenVisits bool
}
// NewUabot will start a bot to run some scenarios. It needs the url/path where to find the scenarions {scenarioURL},
// the searchToken, the analyticsToken and a randomizer.
func NewUabot(local bool, scenarioURL string, searchToken string, analyticsToken string) Uabot {
return &uabot{
local,
scenarioURL,
searchToken,
analyticsToken,
true,
}
}
func (bot *uabot) Run(quitChannel chan bool) error {
var (
conf *Config
err error
timeVisits int
)
// Init from path instead of URL, for testing purposes
if bot.local {
conf, err = NewConfigFromPath(bot.scenarioURL)
} else {
conf, err = NewConfigFromURL(bot.scenarioURL)
}
if err != nil {
return err
}
bot.WaitBetweenVisits = !conf.DontWaitBetweenVisits
// Refresh the scenario files every 5 hours automatically.
// This way, no need to stop the bot to update the possible scenarios.
bot.continuallyRefreshScenariosEvery(5*time.Hour, conf)
if conf.TimeBetweenVisits > 0 {
timeVisits = conf.TimeBetweenVisits
} else {
timeVisits = DEFAULTTIMEBETWEENVISITS
bot.continuallyUpdateTimeVisitsEvery(24*time.Hour, &timeVisits)
}
count := 0
for { // Run forever
select { // select on the quitChannel
default: // default means there is no quit signal
scenario, err := randomScenario(conf.ScenarioMap)
if err != nil {
return err
}
if scenario.UserAgent == "" {
if scenario.Mobile {
scenario.UserAgent, err = randomUserAgent(conf.RandomData.MobileUserAgents)
} else {
scenario.UserAgent, err = randomUserAgent(append(conf.RandomData.UserAgents, conf.RandomData.MobileUserAgents...))
}
if err != nil {
return err
}
}
// New visit
visit, err := NewVisit(bot.searchToken, bot.analyticsToken, scenario.UserAgent, scenario.Language, conf)
if err != nil {
return err
}
// Setup specific stuff for NTO
//visit.SetupNTO()
// Use this line instead outside of NTO
visit.SetupGeneral()
visit.LastQuery.CQ = conf.GlobalFilter
err = visit.ExecuteScenario(*scenario, conf)
if err != nil {
Warning.Print(err)
}
visit.UAClient.DeleteVisit()
if bot.WaitBetweenVisits {
// Minimum wait time of 500ms between visits.
waitTime := (time.Duration(rand.Intn(timeVisits*1000)) + 500) * time.Millisecond
time.Sleep(waitTime)
}
count++
Info.Printf("Scenarios executed : %d \n =============================\n\n", count)
case <-quitChannel: // this means something was written on the quitChannel, stop everything and return
return nil
}
}
}
func (bot *uabot) continuallyUpdateTimeVisitsEvery(timeDuration time.Duration, timeVisits *int) {
ticker := time.NewTicker(timeDuration)
go func() {
for _ = range ticker.C {
var effectiveMeanTimeBetweenVisits = DEFAULTTIMEBETWEENVISITS
if time.Now().Weekday() == time.Saturday || time.Now().Weekday() == time.Sunday {
effectiveMeanTimeBetweenVisits = DEFAULTTIMEBETWEENVISITS * WEEKEND_MODIFIER
}
var randomPositiveTime int
for randomPositiveTime = 0; randomPositiveTime <= 0; randomPositiveTime = int(float64(DEFAULT_STANDARD_DEVIATION_BETWEEN_VISITS)*rand.NormFloat64()+0.5) + effectiveMeanTimeBetweenVisits {
}
*timeVisits = randomPositiveTime
Info.Println("Updating Time Visits to", *timeVisits)
}
}()
}
func (bot *uabot) continuallyRefreshScenariosEvery(timeDuration time.Duration, conf *Config) {
ticker := time.NewTicker(timeDuration)
go func() {
for _ = range ticker.C {
conf2 := refreshConfig(bot.scenarioURL, bot.local)
if conf2 != nil {
Info.Println("Refreshing scenario")
conf = conf2
}
}
}()
}
func refreshConfig(url string, isLocal bool) *Config {
Info.Println("Updating Scenario file")
var err error
var conf *Config
if isLocal {
conf, err = NewConfigFromPath(url)
} else {
conf, err = NewConfigFromURL(url)
}
if err != nil {
Warning.Println("Cannot update scenario file, keeping the old one")
Warning.Println(err)
return nil
}
return conf
}
func randomUserAgent(userAgents []string) (userAgent string, err error) {
if !(len(userAgents) > 0) {
err = errors.New("Cannot find any user agents")
} else {
userAgent = userAgents[rand.Intn(len(userAgents))]
}
return
}
// RandomScenario Returns a random scenario from the list of possible scenarios.
// returns an error if there are no scenarios
func randomScenario(scenarioMap []*Scenario) (scenario *Scenario, err error) {
if len(scenarioMap) < 1 {
err = errors.New("No scenarios detected")
return
}
scenario = scenarioMap[rand.Intn(len(scenarioMap))]
return
}