mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-16 19:14:41 -07:00
Merge pull request #2769 from nats-io/ws_x_forwarded_for_changes
Add X-Forwarded-For IP to the client's remote address
This commit is contained in:
@@ -610,12 +610,15 @@ func (c *client) initClient() {
|
||||
if conn = addr.String(); conn != _EMPTY_ {
|
||||
host, port, _ := net.SplitHostPort(conn)
|
||||
iPort, _ := strconv.Atoi(port)
|
||||
c.port = uint16(iPort)
|
||||
c.host, c.port = host, uint16(iPort)
|
||||
if c.isWebsocket() && c.ws.clientIP != _EMPTY_ {
|
||||
c.host = c.ws.clientIP
|
||||
conn = net.JoinHostPort(c.host, port)
|
||||
} else {
|
||||
c.host = host
|
||||
cip := c.ws.clientIP
|
||||
// Surround IPv6 addresses with square brackets, as
|
||||
// net.JoinHostPort would do...
|
||||
if strings.Contains(cip, ":") {
|
||||
cip = "[" + cip + "]"
|
||||
}
|
||||
conn = fmt.Sprintf("%s/%s", cip, conn)
|
||||
}
|
||||
// Now that we have extracted host and port, escape
|
||||
// the string because it is going to be used in Sprintf
|
||||
|
||||
@@ -3989,12 +3989,12 @@ func TestWSXForwardedFor(t *testing.T) {
|
||||
}
|
||||
select {
|
||||
case d := <-l.ch:
|
||||
ipAndColumn := fmt.Sprintf("%s:", test.expectedValue)
|
||||
ipAndSlash := fmt.Sprintf("%s/", test.expectedValue)
|
||||
if test.useHdrValue {
|
||||
if !strings.HasPrefix(d, ipAndColumn) {
|
||||
t.Fatalf("Expected debug statement to start with: %q, got %q", ipAndColumn, d)
|
||||
if !strings.HasPrefix(d, ipAndSlash) {
|
||||
t.Fatalf("Expected debug statement to start with: %q, got %q", ipAndSlash, d)
|
||||
}
|
||||
} else if strings.HasPrefix(d, ipAndColumn) {
|
||||
} else if strings.HasPrefix(d, ipAndSlash) {
|
||||
t.Fatalf("Unexpected debug statement: %q", d)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
|
||||
Reference in New Issue
Block a user