-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
81 lines (66 loc) · 1.93 KB
/
client.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
package robospider
import (
"fmt"
"log"
"net/http"
"net/url"
"regexp"
"time"
)
type httpClient struct {
proxyServer string
}
// Ensure the domain has the protocol
func BuildDomainURL(input string) string {
match, _ := regexp.MatchString("^(https?://)", input)
if match == false {
input = fmt.Sprintf("http://%v", input)
}
return input
}
func (cli *httpClient) Fetch(targetUrl *url.URL, result chan<- Resource) error {
hc := &http.Client{
Timeout: time.Duration(5 * time.Second),
}
if cli.proxyServer != "" {
// Parse the proxy address
log.Println("[d]: Trying to validate the proxy address before using it.")
parsedURL, parseErr := url.Parse(BuildDomainURL(cli.proxyServer))
// Warn the proxy error and stop the execution to prevent any unwanted request
if parseErr != nil {
log.Println("[e]: Invalid proxy address:", parseErr)
return parseErr
}
// Set the http client proxy and increase default timeout since proxy can be slow as fuck
log.Println("[i]: Setting up transport with proxy server at address:", parsedURL)
hc.Transport = &http.Transport{Proxy: http.ProxyURL(parsedURL)}
hc.Timeout = time.Duration(10 * time.Second)
}
log.Println("[i]: fetching", targetUrl.String(), "...")
resp, err := hc.Get(targetUrl.String())
// if request failed show the error and exit
if err != nil && resp.StatusCode != http.StatusNotFound {
log.Println("[e]: Failed to fetch resource:", err)
return err
}
// DO NOT close the response, as the body is going to be put in a channel
// the caller must close it after use
//defer resp.Body.Close()
log.Println("[i]: fetch completed", targetUrl.String())
result <- Resource{
Name: targetUrl.Path,
Found: (resp.StatusCode == http.StatusOK),
Body: resp.Body,
}
return nil
}
func NewHttpClient() *httpClient {
return &httpClient{
proxyServer: "",
}
}
func NewHttpClientWithProxy(proxyURL string) *httpClient {
return &httpClient{
proxyServer: proxyURL,
}
}