-
Notifications
You must be signed in to change notification settings - Fork 7
/
proxy.go
41 lines (33 loc) · 824 Bytes
/
proxy.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
package main
import (
"flag"
"net"
"net/http"
"github.com/hailiang/gosocks"
)
func init() {
flag.StringVar(&proxyAddress, "proxyAddress", "", "Address of a SOCKS5 proxy to use.")
}
var proxyAddress string
func useProxy() bool {
return len(proxyAddress) > 0
}
func proxyHttpGet(url string) (r *http.Response, e error) {
return proxyHttpClient().Get(url)
}
func proxyNetDial(netType, addr string) (net.Conn, error) {
if useProxy() {
return socks.DialSocksProxy(socks.SOCKS5, proxyAddress)(netType, addr)
}
return net.Dial(netType, addr)
}
func proxyHttpClient() (client *http.Client) {
if useProxy() {
dialSocksProxy := socks.DialSocksProxy(socks.SOCKS5, proxyAddress)
tr := &http.Transport{Dial: dialSocksProxy}
client = &http.Client{Transport: tr}
} else {
client = &http.Client{}
}
return
}