node.js 如何通过 Amazon API Gateway + Lambda(节点)检索用户的公共 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33062097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:22:46  来源:igfitidea点击:

How can I retrieve a user's public IP address via Amazon API Gateway + Lambda (node)

node.jsamazon-web-servicesaws-lambdaaws-api-gateway

提问by rdegges

I'm currently writing a Node.js lambda function, in which I want to log the incoming requester's public IP address. I've been looking through both the API Gateway and Lambda docs all day, but haven't found a solution.

我目前正在编写一个 Node.js lambda 函数,我想在其中记录传入请求者的公共 IP 地址。我一整天都在浏览 API Gateway 和 Lambda 文档,但没有找到解决方案。

Does the lambda eventobject include request metadata I can use to extract the user's IP?

lambdaevent对象是否包含我可以用来提取用户 IP 的请求元数据?

采纳答案by Kenneth Rory Dunn

Here is a simple demonstration of using API Gateway's $context.identity.sourceIpin a Lambda function.

这是$context.identity.sourceIp在 Lambda 函数中使用 API 网关的简单演示。

API Mapping template:

API 映射模板:

{
??? "sourceIP" : "$context.identity.sourceIp"
}

Lambda function:

拉姆达函数:

'use strict';
console.log('Loading function');
exports.handler = (event, context, callback) => {
    console.log('SourceIP =', event.identity.sourceIP);
    callback(null, event.identity.sourceIP);
};

回答by Jonathan

Update for HTTP APIs

HTTP API 更新

Adding @Elijah's comment. The format for HTTP APIs will be

添加@Elijah 的评论。HTTP API 的格式为

event['requestContext']['http']['sourceIp']

Edit

编辑

A better way is actually to check

更好的方法实际上是检查

event['requestContext']['identity']['sourceIp']

You can also get the User-Agent from the same object

您还可以从同一个对象中获取 User-Agent

event['requestContext']['identity']['userAgent']

See Cesar's comment below. Headers are easily spoofed and the user can set X-Forwarded-Forto anything. AFAIK the sourceIpabove is retrieved from the TCP connection.

请参阅下面的塞萨尔评论。标题很容易被欺骗,用户可以设置X-Forwarded-For任何内容。AFAIKsourceIp以上是从 TCP 连接中检索到的。

Original answer

原答案

As of September 2017, you can create a method in API Gateway with Lambda Proxy integration, this will give you access to

截至 2017 年 9 月,您可以在 API Gateway 中创建一个与 Lambda 代理集成的方法,这将使您能够访问

events['headers']['X-Forwarded-For']

Which will look something like 1.1.1.1,214.25.52.1

哪个看起来像 1.1.1.1,214.25.52.1

The first ip 1.1.1.1is your user's public ip address.

第一个 ip1.1.1.1是您用户的公共 ip 地址。

回答by Mike76

In the API Gateway, it's the value

在 API 网关中,它是价值

$context.identity.sourceIp

http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference

http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference

You can pass that through to your Lambda via a mapping template.

您可以通过映射模板将其传递给您的 Lambda。