ExtractRealIP is a middleware that uses headers from reverse proxies to propagate origin IP address information, when configured to do so.
(config *RealIPConfig)
| 32 | // ExtractRealIP is a middleware that uses headers from reverse proxies to |
| 33 | // propagate origin IP address information, when configured to do so. |
| 34 | func 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. |