- With issues: Can't get ClientIP run in AWS Lambda with gin
Description
Hi there, I run a gin web application in AWS Lambda, but I can't get the client IP with gin function: context.ClientIP() or context.RemoteIP(). Very appreciate for anyone's help.
Code
func getIP(c *gin.Context) string {
return c.ClientIP()
//return c.RemoteIP()
}
call getIP() and get empty.
Environment
- go version: 1.18
- gin version (or commit ref): 1.18.2
- operating system: aws-lambda-go-api-proxy v0.14.0
Comment From: radurobot
Do you have any trusted proxies set up?
Comment From: rts-gordon
Hi @radurobot Thanks for your reply.
No, there are no any proxies set up. The gin application is lunched in AWS lambda and access the API via AWS API Gateway.
If the gin application run in VM or PC, the Client IP can be obtained.
Comment From: Trygun
Perhaps accepting this will solve the problem: https://github.com/gin-gonic/gin/pull/3520 we have a similar problem
Comment From: rts-gordon
Thanks for your message. Waiting for testing new function after merge into main branch.
Comment From: jiqing112
Perhaps accepting this will solve the problem: #3520
the problem have be solved ?
Comment From: BranislavLazic
@Trygun I've seen your PR. Is there any way to bypass this issue with the current version of Gin?
Comment From: duaneking
This is working for me in AWS just fine.
You just need to configure it correctly.
Comment From: BranislavLazic
@duaneking If so, can you please elaborate, how? I've set X-Forwarded-For on API Gateway, but I obtain it through ctx.GetHeader call.
Comment From: duaneking
that's because the getheader call is the wrong place to do it; its not working the way you want because that's the wrong place to configure it. Read the docs and the other bugs people have also filed after not reading the docs first.
To get you started: https://github.com/gin-gonic/gin/issues/3336
Close this bug once that helps you, please.
Comment From: rts-gordon
Hi @duaneking Thanks for your response and docs. I read the issues carefully.
I add TrustedPlatform to my code:
router := gin.New()
router.SetTrustedProxies(nil)
router.TrustedPlatform = "X-Forwarded-For"
r1 := router.Group("/users")
r1.POST("/login", ctl.login)
......another function......
func (ctl *UserController) login(c *gin.Context) {
log.Printf("client real IP:%s", c.ClientIP());
......
}
}
This code run in AWS Lambda. Unfortunately, it can't get the real client IP, did I do something wrong?
Thanks for your help.
Comment From: duaneking
Its working for me correctly in production, and in the gin unit tests that pass before every release.
As this is also something that's a part of the unit tests that pass for every release, the burden of proof is on you.
If its not working for you then you need to check your entire config; if you configured the system you have deployed wrong, then you may need to use a different header or your sytem may be injecting the wrong ip. Thats a config issue, not a bug in gin.
Comment From: duaneking
The code I have working in prod is effectively everything you have been given already:
gin.SetMode(gin.DebugMode)
router = gin.New()
router.SetTrustedProxies(nil)
router.TrustedPlatform = "X-Forwarded-For"
But remember, you need to read the AWS docs and configure the api gateway correctly as well. It honestly sounds to me like you have a misconfigured API gateway, so check that.