How to get client IP address in a Firebase cloud f

2019-03-27 08:48发布

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?

2条回答
叼着烟拽天下
2楼-- · 2019-03-27 08:53

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.

查看更多
迷人小祖宗
3楼-- · 2019-03-27 09:18

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"
查看更多
登录 后发表回答