forked from cryptag/cryptag
/
client.go
58 lines (47 loc) · 1.51 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
// Steven Phillips / elimisteve
// 2016.04.18
package tor
import (
"fmt"
"net/http"
"net/url"
"os"
"golang.org/x/net/proxy"
)
var (
// TorBrowserBundleProxyURL is the URL to the local SOCKS5 proxy
// started by the Tor Browser Bundle. Set 'ProxyURL' to this if
// you're using the have Tor _installed_ on your machine.
TorBrowserBundleProxyURL = "socks5://127.0.0.1:9150"
// TorServiceProxyURL is the URL to the local SOCKS5 proxy started
// by the Tor service/daemon. Set 'ProxyURL' to this if you have
// Tor _installed_ on your machine.
TorServiceProxyURL = "socks5://127.0.0.1:9050"
// ProxyURL is the URL of the local SOCKS5 proxy used to make
// requests over Tor. Defaults to TorBrowserBundleProxyURL.
//
// Local proxy port used to make requests over Tor can be set with
// TOR_PORT environment variable (in which case ProxyURL gets set
// to "127.0.0.1:${TOR_PORT}").
ProxyURL = TorBrowserBundleProxyURL
)
func init() {
if p := os.Getenv("TOR_PORT"); p != "" {
ProxyURL = "socks5://127.0.0.1:" + p
}
}
// NewClient returns an HTTP client that does requests through Tor.
func NewClient() (*http.Client, error) {
proxyURL, err := url.Parse(ProxyURL)
if err != nil {
return nil, fmt.Errorf("Error parsing proxy URL: %v", err)
}
// Thank you https://gist.github.com/Yawning/bac58e08a05fc378a8cc
dialer, err := proxy.FromURL(proxyURL, proxy.Direct)
if err != nil {
return nil, err
}
transport := &http.Transport{Dial: dialer.Dial}
client := &http.Client{Transport: transport}
return client, nil
}