-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.go
96 lines (83 loc) · 2.31 KB
/
utils.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
package main
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httputil"
"strings"
)
func BuildProxyRequest(req *http.Request, proxy *httputil.ReverseProxy, bodyBytes []byte) *http.Request {
// Copy and redirect request to EL endpoint
proxyReq := req.Clone(context.Background())
appendHostToXForwardHeader(proxyReq.Header, req.URL.Host)
proxyReq.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
proxy.Director(proxyReq)
return proxyReq
}
func SendProxyRequest(req *http.Request, proxy *httputil.ReverseProxy, bodyBytes []byte) (*http.Response, error) {
proxyReq := BuildProxyRequest(req, proxy, bodyBytes)
resp, err := proxy.Transport.RoundTrip(proxyReq)
if err != nil {
return nil, err
}
return resp, nil
}
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
func appendHostToXForwardHeader(header http.Header, host string) {
// X-Forwarded-For information to indicate it is forwarded from a BN
if prior, ok := header["X-Forwarded-For"]; ok {
host = strings.Join(prior, ", ") + ", " + host
}
header.Set("X-Forwarded-For", host)
}
func isEngineRequest(method string) bool {
return strings.HasPrefix(method, "engine_")
}
func getRemoteHost(r *http.Request) string {
var remoteHost string
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
remoteHost = xff
} else {
splitAddr := strings.Split(r.RemoteAddr, ":")
if len(splitAddr) > 0 {
remoteHost = splitAddr[0]
}
}
return remoteHost
}
func extractStatus(method string, response []byte) (string, error) {
var responseJSON JSONRPCResponse
switch {
case strings.HasPrefix(method, newPayload):
responseJSON.Result = new(PayloadStatusV1)
case strings.HasPrefix(method, fcU):
responseJSON.Result = new(ForkChoiceResponse)
default:
return "", nil // not interested in other engine api calls
}
if err := json.Unmarshal(response, &responseJSON); err != nil {
return "", err
}
switch v := responseJSON.Result.(type) {
case *ForkChoiceResponse:
return v.PayloadStatus.Status, nil
case *PayloadStatusV1:
return v.Status, nil
default:
return "", nil // not interested in other engine api calls
}
}
func getResponseBody(response BuilderResponse) []byte {
if len(response.UncompressedBody) != 0 {
return response.UncompressedBody
}
return response.Body
}