-
Notifications
You must be signed in to change notification settings - Fork 14
/
proxy.go
69 lines (57 loc) · 1.28 KB
/
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
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
package twitterdownloader
import (
"fmt"
"net/http"
"net/url"
"os"
"golang.org/x/net/proxy"
)
const (
DIRECT = iota
SOCKS5
PROXY
)
///Refer to https://www.cnblogs.com/baicaiyoudu/p/5929138.html
//Set URL Proxy
func (c *Client) ClientWithProxy(proxyUrl string) (*http.Client, error) {
u := url.URL{}
urlProxy, err := u.Parse(proxyUrl)
if err != nil {
return nil, err
}
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(urlProxy),
},
}
c.client = client
c.proxyUrl = proxyUrl
c.proxyType = PROXY
return client, nil
}
//Set Environment
func (c Client) SetProxyEnvironment(httpUrl, httpsUrl string) {
os.Setenv("HTTP_PROXY", httpUrl)
os.Setenv("HTTPS_PROXY", httpsUrl)
}
//Set SOCKS
func (c *Client) ClientWithSOCKS5(proto, ipport string) (*http.Client, error) {
//create a socks5 dialer
dialer, err := proxy.SOCKS5(proto, ipport, nil, proxy.Direct)
if err != nil {
fmt.Fprintln(os.Stderr, "can't connect to the proxy:", err)
os.Exit(1)
}
//setup a http client
httpTransport := &http.Transport{}
httpClient := &http.Client{
Transport: httpTransport,
}
//set our socks5 as the dialer
httpTransport.Dial = dialer.Dial
c.client = httpClient
c.socks5Proto = proto
c.socks5IpPort = ipport
c.proxyType = SOCKS5
return httpClient, nil
}