Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get ClientIP run in AWS Lambda with gin #3501

Open
rts-gordon opened this issue Feb 16, 2023 · 12 comments
Open

Can't get ClientIP run in AWS Lambda with gin #3501

rts-gordon opened this issue Feb 16, 2023 · 12 comments

Comments

@rts-gordon
Copy link

rts-gordon commented Feb 16, 2023

  • 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
@radurobot
Copy link

Do you have any trusted proxies set up?

@rts-gordon rts-gordon changed the title Can get ClientIP run in AWS Lambda with gin Can't get ClientIP run in AWS Lambda with gin Feb 16, 2023
@rts-gordon
Copy link
Author

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.

@Trygun
Copy link

Trygun commented Mar 3, 2023

Perhaps accepting this will solve the problem: #3520
we have a similar problem

@rts-gordon
Copy link
Author

Thanks for your message.
Waiting for testing new function after merge into main branch.

@jiqing112
Copy link

Perhaps accepting this will solve the problem: #3520

the problem have be solved ?

@BranislavLazic
Copy link

@Trygun I've seen your PR. Is there any way to bypass this issue with the current version of Gin?

@duaneking
Copy link

This is working for me in AWS just fine.

You just need to configure it correctly.

@BranislavLazic
Copy link

BranislavLazic commented Apr 10, 2023

@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.

@duaneking
Copy link

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: #3336

Close this bug once that helps you, please.

@rts-gordon
Copy link
Author

rts-gordon commented Apr 12, 2023

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.

@duaneking
Copy link

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.

@duaneking
Copy link

duaneking commented Apr 12, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants