From 0eec37af999af0ab1b4a5943d852b512cfa88f3d Mon Sep 17 00:00:00 2001 From: Ben Hardill Date: Tue, 16 Aug 2022 13:49:48 +0100 Subject: [PATCH] Add support for X-Forwarded-For LWS I know you've added this for the next release, but here is a fix for LWS. But just incase there are any more 2.0.x releases. It does leave the incoming port as 0 but as this is pretty meaningless for a proxied connection I think it's probably ok. Signed-off-by: Ben Hardill --- src/websockets.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/websockets.c b/src/websockets.c index 74e36d31..c85760da 100644 --- a/src/websockets.c +++ b/src/websockets.c @@ -133,6 +133,7 @@ static int callback_mqtt( uint8_t *buf; int rc; uint8_t byte; + char ip_addr_buff[1024]; switch (reason) { case LWS_CALLBACK_ESTABLISHED: @@ -157,7 +158,12 @@ static int callback_mqtt( }else{ return -1; } - easy_address(lws_get_socket_fd(wsi), mosq); + + if (lws_hdr_copy(wsi, ip_addr_buff, sizeof(ip_addr_buff), WSI_TOKEN_X_FORWARDED_FOR) > 0) { + mosq->address = mosquitto__strdup(ip_addr_buff); + } else { + easy_address(lws_get_socket_fd(wsi), mosq); + } if(!mosq->address){ /* getpeername and inet_ntop failed and not a bridge */ mosquitto__free(mosq);