From 3b5fc367446ceaa487a29df5bdb69ca550202e60 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 17 Jun 2023 01:46:56 -0500 Subject: [PATCH] Use X-Forwarded-For for Railway IP acquisition --- src/main.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 781bb75..25809f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use std::net::SocketAddr; use axum::{http::StatusCode, response::IntoResponse, Router, routing::{get}}; use axum::body::{Full}; use axum::extract::ConnectInfo; -use axum::http::header; +use axum::http::{header, HeaderMap}; use axum::response::Response; use dotenvy::dotenv; use lazy_static::lazy_static; @@ -55,11 +55,18 @@ async fn main() { } // basic handler that responds with a static string -async fn root_handler(_connect_info: ConnectInfo) -> impl IntoResponse { +async fn root_handler(connect_info: ConnectInfo, headers: HeaderMap) -> impl IntoResponse { let renderer = svg::Renderer::new(); let mut context = Context::new(); - context.insert("text", &_connect_info.ip()); + + let mut ip_repr: Option = headers.get("X-Forwarded-For").and_then(|hv| Some(String::from(hv.to_str().unwrap()))); + if ip_repr.is_none() { + let connect_ip = connect_info.ip().to_string(); + ip_repr = Some(connect_ip.to_string()); + } + + context.insert("text", &ip_repr.unwrap()); let data = TEMPLATES.render("basic.svg", &context); if data.is_err() {