-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
40 lines (34 loc) · 814 Bytes
/
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
package client
import (
"errors"
"fmt"
"net/http"
"net/url"
)
func NewClient(proxyUrl, proxyAuth string) *http.Client {
return &http.Client{
Transport: &transport{
proxyUrl: proxyUrl,
proxyAuth: proxyAuth,
Transport: http.DefaultTransport,
},
}
}
type transport struct {
proxyUrl string
proxyAuth string
Transport http.RoundTripper
}
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
pUrl, err := url.Parse(t.proxyUrl)
if err != nil {
return nil, errors.New("parse proxy target error")
}
origin := fmt.Sprintf("%s://%s", req.URL.Scheme, req.URL.Host)
req.Host = pUrl.Host
req.URL.Scheme = pUrl.Scheme
req.URL.Host = pUrl.Host
req.Header.Add("X-Proxy-Target", origin)
req.Header.Add("X-Proxy-Auth", t.proxyAuth)
return t.Transport.RoundTrip(req)
}