-
Notifications
You must be signed in to change notification settings - Fork 1
/
reproxied.go
126 lines (112 loc) · 3.32 KB
/
reproxied.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
// Package reproxied is a plugin
package reproxied
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
)
// Config the plugin configuration.
type Config struct {
Proxy string `json:"proxy"`
TargetHost string `json:"targetHost"`
KeepHostHeader bool `json:"keepHostHeader"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
// reProxied a Traefik plugin.
type reProxied struct {
next http.Handler
client HTTPClient
targetHostURL *url.URL
keepHostHeader bool
}
// HTTPClient is a reduced interface for http.Client.
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
// New creates a new reProxied plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
proxyURL, err := url.Parse(config.Proxy)
if err != nil {
return nil, fmt.Errorf("unable to parse proxy url: %w", err)
}
return NewWithClient(ctx, next, config, name, &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}})
}
// NewWithClient creates a new reProxied plugin.
func NewWithClient(ctx context.Context, next http.Handler, config *Config, name string, client HTTPClient) (http.Handler, error) {
targetHostURL, err := url.Parse(config.TargetHost)
if err != nil {
return nil, fmt.Errorf("unable to parse target host url: %w", err)
}
return &reProxied{
next: next,
targetHostURL: targetHostURL,
client: client,
keepHostHeader: config.KeepHostHeader,
}, nil
}
func (c *reProxied) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
proxyRequest := c.createProxyRequest(req)
resp, err := c.client.Do(proxyRequest)
if err != nil {
rw.WriteHeader(http.StatusBadGateway)
_, _ = rw.Write([]byte(err.Error()))
return
}
defer func() { _ = resp.Body.Close() }()
for key, values := range resp.Header {
for _, value := range values {
rw.Header().Add(key, value)
}
}
rw.WriteHeader(resp.StatusCode)
buf := make([]byte, 1024)
_, _ = io.CopyBuffer(rw, resp.Body, buf)
}
func (c *reProxied) createProxyRequest(req *http.Request) *http.Request {
hostHeader := c.computeHostHeader(req.Host)
proxyRequest := &http.Request{
Method: req.Method,
URL: &url.URL{
Scheme: c.targetHostURL.Scheme,
Opaque: req.URL.Opaque,
User: c.targetHostURL.User,
Host: c.targetHostURL.Host,
Path: req.URL.Path,
ForceQuery: req.URL.ForceQuery,
RawQuery: req.URL.RawQuery,
Fragment: req.URL.Fragment,
RawFragment: req.URL.RawFragment,
},
Proto: req.Proto,
ProtoMajor: req.ProtoMajor,
ProtoMinor: req.ProtoMinor,
Header: req.Header,
Body: req.Body,
ContentLength: req.ContentLength,
TransferEncoding: req.TransferEncoding,
Close: req.Close,
Host: hostHeader,
Form: req.Form,
PostForm: req.PostForm,
MultipartForm: req.MultipartForm,
Trailer: req.Trailer,
RemoteAddr: req.RemoteAddr,
TLS: req.TLS,
Response: req.Response,
}
return proxyRequest
}
func (c *reProxied) computeHostHeader(originalHostHeader string) string {
var hostHeader string
if c.keepHostHeader {
hostHeader = originalHostHeader
} else {
hostHeader = c.targetHostURL.Host
}
return hostHeader
}