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

Looking for a way to get the source ip from requests #768

Closed
l1x opened this issue Jan 7, 2024 · 2 comments
Closed

Looking for a way to get the source ip from requests #768

l1x opened this issue Jan 7, 2024 · 2 comments

Comments

@l1x
Copy link

l1x commented Jan 7, 2024

I am using http_lambda with Tower / Axum in Rust and I was wondering if there is a way to get the source IP where the request is coming from.

I used to be able to do that in Python with source_ip = request.client.host. I am not sure what are the modes where this is possible.

Is it possible when CloudFront directly sending traffic to the Lambda function? Is this possible at all?

This does not work:

use lambda_http::ext::extensions::RequestExt;

async fn get_request_details(request: Request) -> RequestDetails {
    let ctx = request.lambda_context_ref();

    match ctx {
        Some(cc) => info!("{:?}", cc),
        None => info!("No client context"),
    }

    RequestDetails {
        client_ip: "127.0.0.1".parse(),
    }
}
@l1x
Copy link
Author

l1x commented Jan 7, 2024

As it turns out it is really simple:

let ctx: lambda_http::request::RequestContext = request.request_context();
info!("{:?}", ctx);
ApiGatewayV2(ApiGatewayV2httpRequestContext { route_key: Some("$default"), account_id: Some("anonymous"), stage: Some("$default"), request_id: Some("a2cc41b3-25ec-4271-b920-42436b132b87"), authorizer: None, apiid: Some("rnwi3vgflhmpqsfimm2gv6bq6m0slffd"), domain_name: Some("rnwi3vgflhmpqsfimm2gv6bq6m0slffd.lambda-url.eu-west-1.on.aws"), domain_prefix: Some("rnwi3vgflhmpqsfimm2gv6bq6m0slffd"), time: Some("07/Jan/2024:19:34:11 +0000"), time_epoch: 1704656051745, http: ApiGatewayV2httpRequestContextHttpDescription { method: POST, path: Some("/view/auth/magic-link"), protocol: Some("HTTP/1.1"), source_ip: Some("11.23.33.55"), user_agent: Some("curl/8.1.2") }, authentication: None })

@l1x l1x closed this as completed Jan 7, 2024
Copy link

github-actions bot commented Jan 7, 2024

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for the maintainers of this repository to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

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

1 participant