-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxytransport.go
More file actions
56 lines (48 loc) · 1.03 KB
/
proxytransport.go
File metadata and controls
56 lines (48 loc) · 1.03 KB
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
package proxytransport
import (
"encoding/base64"
"net/http"
"sync"
)
const Host = "proxy.deploys.app"
type Auth struct {
User string
Password string
}
func (a Auth) basicAuth() string {
if a.User == "" {
return ""
}
auth := a.User + ":" + a.Password
s := base64.StdEncoding.EncodeToString([]byte(auth))
return "Basic " + s
}
type Transport struct {
http.RoundTripper
Auth Auth
init sync.Once
basicAuth string
}
func (t *Transport) tr() http.RoundTripper {
if t.RoundTripper == nil {
return http.DefaultTransport
}
return t.RoundTripper
}
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
t.init.Do(func() {
t.basicAuth = t.Auth.basicAuth()
})
req.Header.Set("X-Proxy-Method", req.Method)
req.Header.Set("X-Proxy-URL", req.URL.String())
if t.basicAuth != "" {
req.Header.Set("X-Proxy-Authorization", t.basicAuth)
}
req.Method = http.MethodPost
req.Host = Host
req.URL.Scheme = "https"
req.URL.Host = Host
req.URL.Path = "/"
req.URL.RawQuery = ""
return t.tr().RoundTrip(req)
}