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

question: how to fetch the remote IP address in WASM #14440

Closed
membphis opened this issue Dec 16, 2020 · 2 comments
Closed

question: how to fetch the remote IP address in WASM #14440

membphis opened this issue Dec 16, 2020 · 2 comments
Labels
area/wasm question Questions that are neither investigations, bugs, nor enhancements

Comments

@membphis
Copy link

I do not find any doc about how to fetch the remote IP address in WASM.

Many thx for your help.

@membphis membphis added the triage Issue requires triage label Dec 16, 2020
@alyssawilk alyssawilk added area/wasm question Questions that are neither investigations, bugs, nor enhancements and removed triage Issue requires triage labels Dec 16, 2020
@alandiegosantos
Copy link

alandiegosantos commented Dec 18, 2020

It is possible to get the upstream IP address by getting the property upstream.address.
I am working with WASM filters written in Rust, so the code looks like:

use log::error;
use proxy_wasm::traits::*;
use proxy_wasm::types::*;
use std::str;

#[no_mangle]
pub fn _start() {
    proxy_wasm::set_log_level(LogLevel::Info);
    proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(HttpFilter) });
}

struct HttpFilter;

impl Context for HttpFilter{}

impl HttpContext for HttpFilter {
    fn on_http_response_headers(&mut self, _: usize) -> Action {
        // Add a header on the response.
        let prop = self.get_property(["upstream", "address"].to_vec()).unwrap();
        let addr = match str::from_utf8(&prop) {
            Ok(v) => v,
            Err(_e) => "",
        };
        error!("upstream address {}",addr);
        Action::Continue
    }
}

PS: Do not use that as production code. It is only an example.

I would like to create the docs about these properties, if possible.

@membphis
Copy link
Author

ok, got it. many thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/wasm question Questions that are neither investigations, bugs, nor enhancements
Projects
None yet
Development

No branches or pull requests

3 participants