-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
87 lines (77 loc) · 1.72 KB
/
utils.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
package googleit
import (
"context"
"net/http"
"time"
"github.com/cretz/bine/tor"
log "github.com/schollz/logger"
)
// ListToSet convers a list to a set (removing duplicates)
// but preserving order
func ListToSet(s []string) (t []string) {
m := make(map[string]struct{})
t = make([]string, len(s))
i := 0
for _, v := range s {
if _, ok := m[v]; ok {
continue
}
m[v] = struct{}{}
t[i] = v
i++
}
if i == 0 {
return []string{}
}
t = t[:i]
return
}
type HTTPClient struct {
Client *http.Client
torconnection *tor.Tor
}
func (h *HTTPClient) Get(url string) (resp *http.Response, err error) {
return h.Client.Get(url)
}
func (h *HTTPClient) Do(req *http.Request) (resp *http.Response, err error) {
return h.Client.Do(req)
}
func (h *HTTPClient) Close() (err error) {
if h.torconnection != nil {
err = h.torconnection.Close()
}
return
}
func GetClient(useTor bool) (httpClient *HTTPClient, err error) {
httpClient = &HTTPClient{Client: &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: 20,
},
Timeout: 10 * time.Second,
},
}
if !useTor {
return
}
log.Trace("starting tor")
httpClient.torconnection, err = tor.Start(nil, nil)
if err != nil {
log.Error(err)
return
}
dialCtx, dialCancel := context.WithTimeout(context.Background(), 3000*time.Hour)
defer dialCancel()
// Make connection
log.Trace("dialing tor...")
dialer, err := httpClient.torconnection.Dialer(dialCtx, nil)
if err != nil {
log.Error(err)
return
}
log.Trace("making transport...")
httpClient.Client.Transport = &http.Transport{
DialContext: dialer.DialContext,
MaxIdleConnsPerHost: 20,
}
return
}