MCPcopy Index your code
hub / github.com/coder/coder / ExtractRealIP

Function ExtractRealIP

coderd/httpmw/realip.go:34–53  ·  view source on GitHub ↗

ExtractRealIP is a middleware that uses headers from reverse proxies to propagate origin IP address information, when configured to do so.

(config *RealIPConfig)

Source from the content-addressed store, hash-verified

32// ExtractRealIP is a middleware that uses headers from reverse proxies to
33// propagate origin IP address information, when configured to do so.
34func ExtractRealIP(config *RealIPConfig) func(next http.Handler) http.Handler {
35 return func(next http.Handler) http.Handler {
36 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
37 // Preserve the original TLS connection state and RemoteAddr
38 req = req.WithContext(context.WithValue(req.Context(), ctxKey{}, &RealIPState{
39 Config: config,
40 OriginalRemoteAddr: req.RemoteAddr,
41 }))
42
43 info, err := ExtractRealIPAddress(config, req)
44 if err != nil {
45 httpapi.InternalServerError(w, err)
46 return
47 }
48 req.RemoteAddr = info.String()
49
50 next.ServeHTTP(w, req)
51 })
52 }
53}
54
55// ExtractRealIPAddress returns the original client address according to the
56// configuration and headers. It does not mutate the original request.

Callers 5

TestTrustedOriginsFunction · 0.92
TestCorruptedHeadersFunction · 0.92
TestAddressFamiliesFunction · 0.92
TestEffectiveHostFunction · 0.92
TestApplicationProxyFunction · 0.92

Calls 6

InternalServerErrorFunction · 0.92
ExtractRealIPAddressFunction · 0.85
WithContextMethod · 0.80
ContextMethod · 0.65
StringMethod · 0.45
ServeHTTPMethod · 0.45

Tested by 5

TestTrustedOriginsFunction · 0.74
TestCorruptedHeadersFunction · 0.74
TestAddressFamiliesFunction · 0.74
TestEffectiveHostFunction · 0.74
TestApplicationProxyFunction · 0.74