-
Notifications
You must be signed in to change notification settings - Fork 0
/
forward.go
120 lines (94 loc) · 2.93 KB
/
forward.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
package api
import (
"bytes"
"fmt"
"log"
"github.com/valyala/fasthttp"
"github.com/cash-track/gateway/headers"
"github.com/cash-track/gateway/headers/cookie"
"github.com/cash-track/gateway/logger"
)
func (s *HttpService) ForwardRequest(ctx *fasthttp.RequestCtx, body []byte) error {
// prepare req based on incoming ctx.Request
req := fasthttp.AcquireRequest()
defer func() {
fasthttp.ReleaseRequest(req)
}()
remoteIp := headers.GetClientIPFromContext(ctx)
req.Header.SetMethodBytes(bytes.Clone(ctx.Request.Header.Method()))
s.copyRequestURI(ctx.Request.URI(), req.URI())
req.Header.SetContentTypeBytes(headers.ContentTypeJson)
req.Header.SetBytesV(headers.Accept, headers.ContentTypeJson)
req.Header.Set(headers.XForwardedFor, remoteIp)
headers.CopyFromRequest(ctx, req, []string{
headers.AcceptLanguage,
headers.AccessControlRequestHeaders,
headers.AccessControlRequestMethod,
headers.ContentType,
headers.UserAgent,
headers.Referer,
headers.Origin,
})
headers.CopyCloudFlareHeaders(ctx, req)
// propagate authentication
auth := cookie.ReadAuthCookie(ctx)
if auth.IsLogged() {
headers.WriteBearerToken(req, auth.AccessToken)
}
// copy Body if method allows
if _, ok := methodsWithBody[string(ctx.Method())]; ok {
if body == nil {
req.SetBody(bytes.Clone(ctx.Request.Body()))
} else {
req.SetBody(bytes.Clone(body))
}
}
logger.DebugRequest(req, ServiceId)
// execute request
resp := fasthttp.AcquireResponse()
defer func() {
fasthttp.ReleaseResponse(resp)
}()
if err := s.http.Do(req, resp); err != nil {
return fmt.Errorf("API request error: %w", err)
}
logger.DebugResponse(resp, ServiceId)
logger.FullForwarded(ctx, req, resp, ServiceId)
if !auth.IsLogged() || !auth.CanRefresh() || resp.StatusCode() != fasthttp.StatusUnauthorized {
return forwardResponse(ctx, resp)
}
// perform refresh token
newAuth, err := s.refreshToken(auth)
if err != nil {
log.Printf("[%s] refresh token attempt: %s", remoteIp, err.Error())
}
if newAuth.IsLogged() {
headers.WriteBearerToken(req, newAuth.AccessToken)
// execute request 2nd attempt
if err := s.http.Do(req, resp); err != nil {
return fmt.Errorf("API request with fresh token error: %w", err)
}
logger.DebugResponse(resp, ServiceId)
}
newAuth.WriteCookie(ctx)
return forwardResponse(ctx, resp)
}
func forwardResponse(ctx *fasthttp.RequestCtx, resp *fasthttp.Response) error {
ctx.SetStatusCode(resp.StatusCode())
ctx.SetBody(bytes.Clone(resp.Body()))
headers.CopyFromResponse(resp, ctx, []string{
headers.AccessControlAllowOrigin,
headers.AccessControlAllowMethods,
headers.AccessControlAllowHeaders,
headers.AccessControlMaxAge,
headers.ContentType,
headers.RetryAfter,
headers.Vary,
headers.XRateLimit,
headers.XRateLimitRemaining,
})
if val := ctx.Response.Header.Peek(headers.AccessControlAllowOrigin); val != nil {
ctx.Response.Header.Set(headers.AccessControlAllowCredentials, "true")
}
return nil
}