How to get client IP address in a Firebase cloud f

2019-03-27 09:00发布

问题:

When saving data to Firebase database with a Firebase cloud function, I'd like to also write the IP address where the request comes from.

However, req.connection.remoteAddress always returns ::ffff:0.0.0.0. Is there a way to get the actual IP address of the client that makes the request?

回答1:

If you are looking for the client ip thru firebase hosting you should use the header fastly-client-ip there will be the real client ip.



回答2:

EDIT: This seems to be outdated. See the other answer.

The IP address seems to be available in req.headers["x-forwarded-for"]. I didn't find official information on this in the documentation, though.

Also please note that if the client itself sends e.g. X-Forwarded-For: 1.2.3.4, the values are concatenated:

"x-forwarded-for":"1.2.3.4, actual-client-ip-as-seen-by-google"