-
Notifications
You must be signed in to change notification settings - Fork 1
/
tgun.go
277 lines (236 loc) · 6.39 KB
/
tgun.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2017 The tgun Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package tgun provides a TCP/http(s) client with common options
package tgun
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"path"
"runtime"
"strings"
"time"
"golang.org/x/net/proxy"
)
const version = "0.1.6"
// DefaultTimeout is used if c.Timeout is not set
var DefaultTimeout = time.Second * 30
// DefaultProxy is used when c.Proxy is "1080" or "socks" or "proxy" or "true" or "1"
var DefaultProxy = "socks5://127.0.0.1:1080"
// DefaultUserAgent is used when c.UserAgent is empty
var DefaultUserAgent = fmt.Sprintf("tgun/%s", version)
// DefaultTor proxy is used when c.Proxy is set to "tor"
var DefaultTor = func() string {
if runtime.GOOS == "windows" {
return "socks5://127.0.0.1:9150"
}
return "socks5://127.0.0.1:9050"
}()
// Client holds connection options
type Client struct {
DirectConnect bool // Set to true to bypass proxy
Proxy string // In the format: socks5://localhost:1080
UserAgent string // In the format: "MyThing/0.1" or "MyThing/0.1 (http://example.org)"
Timeout time.Duration // If unset, DefaultTimeout is used.
AuthUser string
AuthPassword string
Headers map[string]string
httpClient *http.Client
dialer proxy.Dialer
}
// HTTPClient returns a http.Client with proxy (but no headers, auth, user-agent)
func (c Client) HTTPClient() (*http.Client, error) {
err := c.refresh()
return c.httpClient, err
}
func Join(s ...string) string {
if len(s) == 0 {
return ""
}
if len(s) == 1 {
return s[0]
}
// "https://" + Join("api.example.com/v1", "users")
// "https://" + Join("api.example.com", "v1", "users")
if !strings.Contains(s[0], "://") {
return path.Join(s...)
}
// Join("http://", "example.com", "index.php")
// Join("http://api.example.com/v1", "users")
// Join("http://api.example.com", "v1", "users")
if strings.HasSuffix(s[0], "://") {
s[1] = s[0] + s[1]
s = s[1:]
}
u, err := url.Parse(s[0])
if err != nil {
return ""
}
u.Path = path.Join(append([]string{u.Path}, s[1:]...)...)
return u.String()
}
func (c *Client) Join(s ...string) string {
return Join(s...)
}
// DialTCP an address using c.Proxy if set
func (c *Client) DialTCP(addr string) (net.Conn, error) {
return c.Dial("tcp", addr)
}
// Dial an address using c.Proxy if set
func (c *Client) Dial(network string, addr string) (net.Conn, error) {
// Refresh http client, proxy
if err := c.refresh(); err != nil {
return nil, err
}
return c.dialer.Dial(network, addr)
}
// Do returns an http response.
// The request's config is *fortified* with http.Client, proxy, headers, authentication, and user agent.
func (c *Client) Do(req *http.Request) (*http.Response, error) {
// Refresh http client, proxy
if err := c.refresh(); err != nil {
return nil, err
}
// Create request headers using tgun.Client config
if c.Headers != nil {
for header, value := range c.Headers {
req.Header.Set(header, value)
}
}
// Set Basic Auth
if c.AuthUser != "" && c.AuthPassword != "" {
req.SetBasicAuth(c.AuthUser, c.AuthPassword)
}
// Set User Agent, over previous headers
req.Header.Set("User-Agent", c.UserAgent)
// Do with new http client request
return c.httpClient.Do(req)
}
// Unmarshal JSON (GET, no body)
func (c *Client) Unmarshal(url string, ptr interface{}) error {
return c.unmarshal(url, http.MethodGet, ptr, nil)
}
// Unmarshal JSON (POST, with body param)
func (c *Client) UnmarshalPost(url string, ptr interface{}, body io.Reader) error {
return c.unmarshal(url, http.MethodPost, ptr, body)
}
func (c *Client) unmarshal(url string, method string, ptr interface{}, body io.Reader) error {
if err := c.refresh(); err != nil {
return err
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
resp.Body.Close()
return err
}
err = json.Unmarshal(b, ptr)
if err != nil {
resp.Body.Close()
return fmt.Errorf("%d: %s", resp.StatusCode, string(b))
}
resp.Body.Close()
return nil
}
// Get connects returns an http response
func (c *Client) Get(url string) (*http.Response, error) {
if err := c.refresh(); err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
// GetBytes connects and returns an http response body in the form of bytes
func (c *Client) GetBytes(url string) ([]byte, error) {
resp, err := c.Get(url)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = resp.Body.Close()
return b, err
}
// getDialer is called by proxify to return a proxy.Dialer
func getDialer(proxyurl string) (proxy.Dialer, error) {
switch proxyurl {
case "":
return proxy.Direct, nil
case "tor":
proxyurl = DefaultTor
case "socks", "1080", "proxy", "true", "1":
proxyurl = DefaultProxy
}
u, err := url.Parse(proxyurl)
if err != nil {
return nil, err
}
return proxy.FromURL(u, proxy.Direct)
}
// proxify is called by refresh to return a *http.Transport
func (c *Client) proxify() (*http.Transport, error) {
t := &http.Transport{}
proxypath := c.Proxy
if c.DirectConnect {
proxypath = ""
}
dialer, err := getDialer(proxypath)
if err != nil {
return nil, fmt.Errorf("Dialer Error: %s", err.Error())
}
c.dialer = dialer
t.Dial = c.dialer.Dial
return t, nil
}
// refresh gets called every time we use any method of Client
// its responsibility is:
// to sanity check the tgun.Client config
// to apply c.Proxy, and fix redirect useragent/header leak.
func (c *Client) refresh() error {
// default user agent
if c.UserAgent == "" {
c.UserAgent = DefaultUserAgent
}
// default timeout
if c.Timeout == 0 {
c.Timeout = DefaultTimeout
}
// create transport
transport, err := c.proxify()
if err != nil {
return err
}
// forge http client
httpClient := &http.Client{
Transport: transport,
Timeout: c.Timeout,
Jar: nil,
}
// create redirect policy to set UA even during redirects
httpClient.CheckRedirect = func(req *http.Request, reqs []*http.Request) error {
for h, v := range c.Headers {
req.Header.Set(h, v)
}
req.Header.Set("User-Agent", c.UserAgent)
return nil
}
c.httpClient = httpClient
return nil
}