-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequester.go
More file actions
61 lines (53 loc) · 1.86 KB
/
requester.go
File metadata and controls
61 lines (53 loc) · 1.86 KB
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
package filechick
import (
"context"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"time"
)
// UserAgents is a list of user agents that can be used
var UserAgents = []string{
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36",
"Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.1 Safari/605.1.15",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36",
}
// RandomizeUserAgent randomizes the user agent
func RandomizeUserAgent() string {
// Create a new random source
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return UserAgents[r.Intn(len(UserAgents))]
}
// ApplyUserAgent applies the user agent to an HTTP request
func ApplyUserAgent(req *http.Request) {
req.Header.Add("User-Agent", RandomizeUserAgent())
}
// CustomRequest makes an HTTP GET request with a randomized user agent
func CustomRequest(ctx context.Context, url string, client *http.Client) (string, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return "", fmt.Errorf("error creating request: %w", err)
}
ApplyUserAgent(req)
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("error making request: %w", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Println("Error closing body:", err)
}
}()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("request failed with status code %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading body: %w", err)
}
return string(body), nil
}