-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
257 lines (223 loc) · 5.73 KB
/
main.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Package foxtrot is meant to help bump the numbers of the Firefox User-Agent against US Gov Websites
package main
import (
"context"
"encoding/csv"
"expvar"
"fmt"
"io"
"log"
"math/rand"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/rs/dnscache"
"github.com/spf13/cobra"
)
var (
concurrency int
delay int
userAgent string
totalRequests int64
successfulRequests map[string]int64
failedRequests map[string]int64
mu sync.Mutex
location *time.Location
)
func init() {
successfulRequests = make(map[string]int64)
failedRequests = make(map[string]int64)
setTimeZone()
}
func setTimeZone() {
tz := os.Getenv("TZ")
if tz == "" {
tz = "UTC" // Default to UTC if no timezone is set
}
loc, err := time.LoadLocation(tz)
if err != nil {
log.Fatalf("Failed to load timezone %s: %v", tz, err)
}
location = loc
}
func sendRequest(client *http.Client, website string) {
req, err := http.NewRequest("GET", website, nil)
if err != nil {
log.Printf("Error creating request for %s: %v", website, err)
incrementFailedRequests(website)
return
}
req.Header.Set("User-Agent", userAgent)
resp, err := client.Do(req)
if err != nil {
log.Printf("Error sending request to %s: %v", website, err)
incrementFailedRequests(website)
return
}
defer resp.Body.Close()
incrementTotalRequests()
if resp.StatusCode == http.StatusOK {
incrementSuccessfulRequests(website)
} else {
incrementFailedRequests(website)
}
log.Printf("Request to %s completed with status code: %d", website, resp.StatusCode)
}
func incrementTotalRequests() {
mu.Lock()
totalRequests++
mu.Unlock()
}
func incrementSuccessfulRequests(website string) {
mu.Lock()
successfulRequests[website]++
mu.Unlock()
}
func incrementFailedRequests(website string) {
mu.Lock()
failedRequests[website]++
mu.Unlock()
}
func metricsHandler(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
fmt.Fprintf(w, "total_requests %d\n", totalRequests)
for site, count := range successfulRequests {
fmt.Fprintf(w, "successful_requests{website=\"%s\"} %d\n", site, count)
}
for site, count := range failedRequests {
fmt.Fprintf(w, "failed_requests{website=\"%s\"} %d\n", site, count)
}
}
func downloadWebsites(url string) ([]string, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
reader := csv.NewReader(resp.Body)
var websites []string
for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
website := record[0]
if !strings.HasPrefix(website, "http://") && !strings.HasPrefix(website, "https://") {
website = "https://" + website
}
websites = append(websites, website)
}
return websites, nil
}
func selectRandomWebsites(websites []string, count int) []string {
rand.NewSource(time.Now().UnixNano())
rand.Shuffle(len(websites), func(i, j int) {
websites[i], websites[j] = websites[j], websites[i]
})
if count > len(websites) {
count = len(websites)
}
return websites[:count]
}
func run(downloadFunc func(string) ([]string, error), sendFunc func(*http.Client, string)) {
allWebsites, err := downloadFunc("https://analytics.usa.gov/data/live/sites.csv")
if err != nil {
log.Fatalf("Error downloading websites: %v", err)
}
resolver := &dnscache.Resolver{}
go func() {
for {
time.Sleep(10 * time.Minute)
resolver.Refresh(true)
}
}()
transport := &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
separator := strings.LastIndex(addr, ":")
host := addr[:separator]
ips, err := resolver.LookupHost(ctx, host)
if err != nil {
return nil, err
}
if len(ips) == 0 {
return nil, fmt.Errorf("no IPs found for host %s", host)
}
return net.Dial(network, ips[0]+addr[separator:])
},
}
client := &http.Client{Transport: transport}
quit := make(chan struct{})
ticker := time.NewTicker(1 * time.Hour)
wg := &sync.WaitGroup{}
selectedWebsites := selectRandomWebsites(allWebsites, concurrency)
for _, website := range selectedWebsites {
wg.Add(1)
go func(site string) {
defer wg.Done()
for {
select {
case <-quit:
return
default:
sendFunc(client, site)
time.Sleep(time.Duration(rand.Intn(delay-1)+1) * time.Second)
}
}
}(website)
}
go func() {
for {
select {
case <-ticker.C:
close(quit)
quit = make(chan struct{})
wg.Wait()
wg = &sync.WaitGroup{}
selectedWebsites = selectRandomWebsites(allWebsites, concurrency)
for _, website := range selectedWebsites {
wg.Add(1)
go func(site string) {
defer wg.Done()
for {
select {
case <-quit:
return
default:
sendFunc(client, site)
time.Sleep(time.Duration(rand.Intn(delay-1)+1) * time.Second)
}
}
}(website)
}
}
}
}()
wg.Wait()
ticker.Stop()
}
func main() {
var cmd = &cobra.Command{
Use: "foxtrot",
Short: "A simple golang script that will help bump Firefox's overall numbers on US Gov websites",
Run: func(cmd *cobra.Command, args []string) {
go func() {
http.Handle("/metrics", expvar.Handler())
log.Fatal(http.ListenAndServe(":9120", nil))
}()
run(downloadWebsites, sendRequest)
},
}
cmd.Flags().IntVarP(&concurrency, "concurrency", "c", 10, "Number of goroutines and random websites to select")
cmd.Flags().IntVarP(&delay, "delay", "d", 45, "Total time to sleep between requests in seconds")
cmd.Flags().StringVarP(&userAgent, "user-agent", "u", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:123.0) Gecko/20100101 Firefox/123.0", "User-Agent for HTTP requests")
if err := cmd.Execute(); err != nil {
log.Fatalf("Command execution error: %v", err)
}
}