如何在Firebase云功能中获取客户端IP地址?

时间:2022-12-07 21:11:51

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.

使用Firebase云功能将数据保存到Firebase数据库时,我还想写出请求来源的IP地址。

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?

但是,req.connection.remoteAddress始终返回:: ffff:0.0.0.0。有没有办法获取发出请求的客户端的实际IP地址?

2 个解决方案

#1


13  

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.

IP地址似乎在req.headers [“x-forwarded-for”]中可用。但是,我没有在文档中找到关于此的官方信息。

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,值连接在一起:

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

#2


0  

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.

如果你正在寻找客户端ip通过firebase托管你应该使用标头fastly-client-ip将有真正的客户端IP。

#1


13  

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.

IP地址似乎在req.headers [“x-forwarded-for”]中可用。但是,我没有在文档中找到关于此的官方信息。

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,值连接在一起:

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

#2


0  

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.

如果你正在寻找客户端ip通过firebase托管你应该使用标头fastly-client-ip将有真正的客户端IP。