/
proxyfactory.go
91 lines (74 loc) · 2.87 KB
/
proxyfactory.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
package proxy
import (
"crypto/tls"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"github.com/kyma-project/kyma/components/application-gateway/pkg/authorization"
"github.com/kyma-project/kyma/components/application-gateway/pkg/apperrors"
"github.com/kyma-project/kyma/components/application-gateway/pkg/httpconsts"
"github.com/kyma-project/kyma/components/application-gateway/pkg/httptools"
log "github.com/sirupsen/logrus"
)
func makeProxy(targetUrl string, requestParameters *authorization.RequestParameters, id string, skipVerify bool) (*httputil.ReverseProxy, apperrors.AppError) {
target, err := url.Parse(targetUrl)
if err != nil {
log.Errorf("failed to parse target url '%s': '%s'", targetUrl, err.Error())
return nil, apperrors.Internal("failed to parse target url '%s': '%s'", targetUrl, err.Error())
}
targetQuery := target.RawQuery
director := func(req *http.Request) {
log.Infof("Proxy call for service '%s' to '%s'", id, targetUrl)
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.Host = target.Host
combinedPath := joinPaths(target.Path, req.URL.Path)
req.URL.RawPath = combinedPath
req.URL.Path = combinedPath
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if requestParameters != nil {
setCustomQueryParameters(req.URL, requestParameters.QueryParameters)
setCustomHeaders(req.Header, requestParameters.Headers)
}
removeForbiddenHeaders(req.Header)
log.Infof("Modified request url : '%s', schema : '%s', path : '%s'", req.URL.String(), req.URL.Scheme, req.URL.Path)
}
newProxy := &httputil.ReverseProxy{Director: director}
newProxy.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: skipVerify}}
return newProxy, nil
}
func joinPaths(a, b string) string {
if b == "" {
return a
}
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
func setCustomQueryParameters(reqURL *url.URL, customQueryParams *map[string][]string) {
httptools.SetQueryParameters(reqURL, customQueryParams)
}
func removeForbiddenHeaders(reqHeaders http.Header) {
httptools.RemoveHeader(reqHeaders, httpconsts.HeaderXForwardedProto)
httptools.RemoveHeader(reqHeaders, httpconsts.HeaderXForwardedFor)
httptools.RemoveHeader(reqHeaders, httpconsts.HeaderXForwardedHost)
httptools.RemoveHeader(reqHeaders, httpconsts.HeaderXForwardedClientCert)
}
func setCustomHeaders(reqHeaders http.Header, customHeaders *map[string][]string) {
if _, ok := reqHeaders[httpconsts.HeaderUserAgent]; !ok {
// explicitly disable User-Agent so it's not set to default value
reqHeaders.Set(httpconsts.HeaderUserAgent, "")
}
httptools.SetHeaders(reqHeaders, customHeaders)
}