forked from tomnomnom/gron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
54 lines (47 loc) · 1.01 KB
/
url.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
package main
import (
"bufio"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"time"
)
func validURL(url string) bool {
r := regexp.MustCompile("(?i)^http(?:s)?://")
return r.MatchString(url)
}
func getURL(getURL string, insecure bool, proxy string) (io.Reader, error) {
var tr = &http.Transport{}
if proxy != "" {
proxyURL, err := url.Parse(proxy)
if err != nil {
return nil, err
}
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
Proxy: http.ProxyURL(proxyURL),
}
} else {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
}
}
client := http.Client{
Transport: tr,
Timeout: 20 * time.Second,
}
req, err := http.NewRequest("GET", getURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", fmt.Sprintf("gron/%s", gronVersion))
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return bufio.NewReader(resp.Body), err
}