mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
Store client IP address in request context
This commit is contained in:
parent
c1e1506720
commit
c9f9dd3262
9 changed files with 46 additions and 19 deletions
|
@ -24,6 +24,7 @@ const (
|
|||
FlashMessageContextKey
|
||||
FlashErrorMessageContextKey
|
||||
PocketRequestTokenContextKey
|
||||
ClientIPContextKey
|
||||
)
|
||||
|
||||
// IsAdminUser checks if the logged user is administrator.
|
||||
|
@ -103,6 +104,11 @@ func PocketRequestToken(r *http.Request) string {
|
|||
return getContextStringValue(r, PocketRequestTokenContextKey)
|
||||
}
|
||||
|
||||
// ClientIP returns the client IP address stored in the context.
|
||||
func ClientIP(r *http.Request) string {
|
||||
return getContextStringValue(r, ClientIPContextKey)
|
||||
}
|
||||
|
||||
func getContextStringValue(r *http.Request, key ContextKey) string {
|
||||
if v := r.Context().Value(key); v != nil {
|
||||
return v.(string)
|
||||
|
|
|
@ -100,8 +100,8 @@ func HasQueryParam(r *http.Request, param string) bool {
|
|||
return ok
|
||||
}
|
||||
|
||||
// RealIP returns client's real IP address.
|
||||
func RealIP(r *http.Request) string {
|
||||
// FindClientIP returns client's real IP address.
|
||||
func FindClientIP(r *http.Request) string {
|
||||
headers := []string{"X-Forwarded-For", "X-Real-Ip"}
|
||||
for _, header := range headers {
|
||||
value := r.Header.Get(header)
|
||||
|
|
|
@ -11,12 +11,12 @@ import (
|
|||
|
||||
func TestRealIPWithoutHeaders(t *testing.T) {
|
||||
r := &http.Request{RemoteAddr: "192.168.0.1:4242"}
|
||||
if ip := RealIP(r); ip != "192.168.0.1" {
|
||||
if ip := FindClientIP(r); ip != "192.168.0.1" {
|
||||
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||
}
|
||||
|
||||
r = &http.Request{RemoteAddr: "192.168.0.1"}
|
||||
if ip := RealIP(r); ip != "192.168.0.1" {
|
||||
if ip := FindClientIP(r); ip != "192.168.0.1" {
|
||||
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ func TestRealIPWithXFFHeader(t *testing.T) {
|
|||
headers.Set("X-Forwarded-For", "203.0.113.195, 70.41.3.18, 150.172.238.178")
|
||||
r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
||||
|
||||
if ip := RealIP(r); ip != "203.0.113.195" {
|
||||
if ip := FindClientIP(r); ip != "203.0.113.195" {
|
||||
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||
}
|
||||
|
||||
|
@ -36,7 +36,7 @@ func TestRealIPWithXFFHeader(t *testing.T) {
|
|||
headers.Set("X-Forwarded-For", "2001:db8:85a3:8d3:1319:8a2e:370:7348")
|
||||
r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
||||
|
||||
if ip := RealIP(r); ip != "2001:db8:85a3:8d3:1319:8a2e:370:7348" {
|
||||
if ip := FindClientIP(r); ip != "2001:db8:85a3:8d3:1319:8a2e:370:7348" {
|
||||
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ func TestRealIPWithXFFHeader(t *testing.T) {
|
|||
headers.Set("X-Forwarded-For", "70.41.3.18")
|
||||
r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
||||
|
||||
if ip := RealIP(r); ip != "70.41.3.18" {
|
||||
if ip := FindClientIP(r); ip != "70.41.3.18" {
|
||||
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,7 @@ func TestRealIPWithXFFHeader(t *testing.T) {
|
|||
headers.Set("X-Forwarded-For", "fake IP")
|
||||
r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
||||
|
||||
if ip := RealIP(r); ip != "192.168.0.1" {
|
||||
if ip := FindClientIP(r); ip != "192.168.0.1" {
|
||||
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||
}
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ func TestRealIPWithXRealIPHeader(t *testing.T) {
|
|||
headers.Set("X-Real-Ip", "192.168.122.1")
|
||||
r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
||||
|
||||
if ip := RealIP(r); ip != "192.168.122.1" {
|
||||
if ip := FindClientIP(r); ip != "192.168.122.1" {
|
||||
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ func TestRealIPWithBothHeaders(t *testing.T) {
|
|||
|
||||
r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
||||
|
||||
if ip := RealIP(r); ip != "203.0.113.195" {
|
||||
if ip := FindClientIP(r); ip != "203.0.113.195" {
|
||||
t.Fatalf(`Unexpected result, got: %q`, ip)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue