Skip to content
Snippets Groups Projects
Commit 5f39e022 authored by Jano Hendriks's avatar Jano Hendriks
Browse files

Add GetRequestSourceIP function

parent a0b2a769
No related branches found
Tags v1.280.0
No related merge requests found
......@@ -2,7 +2,9 @@ package ip_utils
import (
"fmt"
"github.com/aws/aws-lambda-go/events"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/handler_utils"
"net"
"os"
"strings"
......@@ -163,4 +165,27 @@ func ValidateIPAddress(ipAddress string) (cleanedIPAddress string, err error) {
return ipAddress, nil
}
func GetRequestSourceIP(proxyRequest *events.APIGatewayProxyRequest, websocketReqeuest *events.APIGatewayWebsocketProxyRequest) string {
var requestSourceIP string
if proxyRequest != nil {
requestSourceIP = proxyRequest.RequestContext.Identity.SourceIP
// Cloudflare uses this header to pass the real IP
forwardedForHeader := handler_utils.FindHeaderValue(proxyRequest.Headers, "x-forwarded-for")
if forwardedForHeader != "" &&
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])
return headerSourceIP
}
}
} else if websocketReqeuest != nil {
requestSourceIP = websocketReqeuest.RequestContext.Identity.SourceIP
}
return requestSourceIP
}
// endregion Helpers
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment