diff --git a/tts_service/src/endpoints/speak.rs b/tts_service/src/endpoints/speak.rs index 76bc5fa690..0ddacef6e0 100644 --- a/tts_service/src/endpoints/speak.rs +++ b/tts_service/src/endpoints/speak.rs @@ -52,12 +52,32 @@ pub async fn post_speak(request: HttpRequest, .body("Request has empty text.")); } - // NB: Actually, we want the X-Forwarded-For IP address, since otherwise - // we get the load balancer. - let ip_address = request.connection_info() - .remote() - .unwrap_or("") - .to_string(); + let ip_address = match request.headers().get(HeaderName::from_static("x-voder-proxy-for")) { + Some(ip_address) => { + // Unfortunately the upstream Rust proxy is replacing the `forwarded` and `x-forwarded-for` + // headers, so we populate this custom header as a workaround. + info!("Proxied IP address: {:?}", ip_address); + ip_address.to_str() + .unwrap_or("") + .to_string() + }, + None => { + // If we're running without the upstream Rust proxy, we can grab 'x-forarded-for', which is + // populated by the Digital Ocean load balancer. + let ip_address_and_port = request.connection_info() + .remote() + .unwrap_or("") + .to_string(); + let ip_address = ip_address_and_port.split(":") + .collect::>() + .get(0) + .copied() + .unwrap_or("") + .to_string(); + info!("Forwarded IP address: {}", &ip_address); + ip_address + }, + }; match request.headers().get(HeaderName::from_static("x-forwarded-for")) { Some(header_value) => { diff --git a/tts_service/src/endpoints/speak_with_spectrogram.rs b/tts_service/src/endpoints/speak_with_spectrogram.rs index 5e24c2ea8f..cdd9c370d8 100644 --- a/tts_service/src/endpoints/speak_with_spectrogram.rs +++ b/tts_service/src/endpoints/speak_with_spectrogram.rs @@ -1,4 +1,4 @@ -use actix_web::http::{StatusCode, header}; +use actix_web::http::{StatusCode, header, HeaderName, HeaderValue}; use actix_web::web::{ Data, Json, @@ -72,16 +72,32 @@ pub async fn post_speak_with_spectrogram(request: HttpRequest, .body("Request has empty text."))); } - for (k, v) in request.headers().iter() { - info!("Header: {:?} = {:?}", k, v); - } - - // NB: Actually, we want the X-Forwarded-For IP address, since otherwise - // we get the load balancer. - let ip_address = request.connection_info() - .remote() - .unwrap_or("") - .to_string(); + let ip_address = match request.headers().get(HeaderName::from_static("x-voder-proxy-for")) { + Some(ip_address) => { + // Unfortunately the upstream Rust proxy is replacing the `forwarded` and `x-forwarded-for` + // headers, so we populate this custom header as a workaround. + info!("Proxied IP address: {:?}", ip_address); + ip_address.to_str() + .unwrap_or("") + .to_string() + }, + None => { + // If we're running without the upstream Rust proxy, we can grab 'x-forarded-for', which is + // populated by the Digital Ocean load balancer. + let ip_address_and_port = request.connection_info() + .remote() + .unwrap_or("") + .to_string(); + let ip_address = ip_address_and_port.split(":") + .collect::>() + .get(0) + .copied() + .unwrap_or("") + .to_string(); + info!("Forwarded IP address: {}", &ip_address); + ip_address + }, + }; let sentence_record = NewSentence { sentence: text.clone(),