From 6709f4d05df90ddf2d8f23ed6c60c0f6933072c4 Mon Sep 17 00:00:00 2001
From: jano3 <jano@bob.co.za>
Date: Fri, 28 Feb 2025 15:16:17 +0200
Subject: [PATCH] Fix getting IP address through cloudflare proxy

---
 ip_utils/ip_utils.go | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/ip_utils/ip_utils.go b/ip_utils/ip_utils.go
index c8316f1..bf06bcd 100644
--- a/ip_utils/ip_utils.go
+++ b/ip_utils/ip_utils.go
@@ -175,9 +175,9 @@ func GetRequestSourceIP(proxyRequest *events.APIGatewayProxyRequest, websocketRe
 			VerifyCloudflareSourceIP(requestSourceIP) {
 			forwardedForHeaderIPs := strings.Split(forwardedForHeader, ",")
 
-			if len(forwardedForHeaderIPs) > 0 {
-				// Use the first IP as the source IP
-				headerSourceIP := strings.TrimSpace(forwardedForHeaderIPs[len(forwardedForHeaderIPs)-1])
+			if len(forwardedForHeaderIPs) > 1 {
+				// Use the second-to-last IP – the last IP will be the Cloudflare proxy's IP
+				headerSourceIP := strings.TrimSpace(forwardedForHeaderIPs[len(forwardedForHeaderIPs)-2])
 				return headerSourceIP
 			}
 		}
-- 
GitLab