Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"io/fs"
"log/slog"
"mime/multipart"
"net"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -252,8 +251,8 @@ func (c *Context) RealIP() string {
}
// req.RemoteAddr is the IP address of the remote end of the connection, which may be a proxy. It is populated by the
// http.conn.readRequest() method and uses net.Conn.RemoteAddr().String() which we trust.
ra, _, _ := net.SplitHostPort(c.request.RemoteAddr)
return ra
// Use extractIP so bare IPs (no host:port) still resolve, matching ExtractIPDirect.
return extractIP(c.request)
}

// Path returns the registered path for the handler.
Expand Down
12 changes: 12 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,18 @@ func TestContext_RealIP(t *testing.T) {
whenReq: &http.Request{RemoteAddr: "89.89.89.89:1654"},
expect: "89.89.89.89",
},
{
name: "ip from bare remote addr without port",
givenIPExtrator: nil,
whenReq: &http.Request{RemoteAddr: "89.89.89.89"},
expect: "89.89.89.89",
},
{
name: "ip from bare ipv6 remote addr without port",
givenIPExtrator: nil,
whenReq: &http.Request{RemoteAddr: "2001:db8::1"},
expect: "2001:db8::1",
},
{
name: "ip from ip extractor",
givenIPExtrator: ExtractIPFromRealIPHeader(TrustIPRange(ipv6ForRemoteAddrExternalRange)),
Expand Down
3 changes: 1 addition & 2 deletions ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,5 @@ func legacyIPExtractor(req *http.Request) string {
ip = strings.TrimSuffix(ip, "]")
return ip
}
ra, _, _ := net.SplitHostPort(req.RemoteAddr)
return ra
return extractIP(req)
}
Loading