From dd4fc52c989d13b672188ba1c5ed6451752ad21d Mon Sep 17 00:00:00 2001 From: blek Date: Thu, 26 Oct 2023 18:30:35 +1000 Subject: [PATCH] get client ip on upload action --- filed/Cargo.lock | 29 +++++++++++++++++++++++++++++ filed/Cargo.toml | 1 + filed/src/web/forms.rs | 6 ++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/filed/Cargo.lock b/filed/Cargo.lock index 03f39c7..3653e5b 100644 --- a/filed/Cargo.lock +++ b/filed/Cargo.lock @@ -383,6 +383,7 @@ dependencies = [ "toml", "urlencoding 2.1.3", "warp", + "warp-real-ip", ] [[package]] @@ -1077,6 +1078,15 @@ dependencies = [ "url", ] +[[package]] +name = "rfc7239" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "087317b3cf7eb481f13bd9025d729324b7cd068d6f470e2d76d049e191f5ba47" +dependencies = [ + "uncased", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -1548,6 +1558,15 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "uncased" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b9bc53168a4be7402ab86c3aad243a84dd7381d09be0eddc81280c1da95ca68" +dependencies = [ + "version_check", +] + [[package]] name = "unicase" version = "2.7.0" @@ -1689,6 +1708,16 @@ dependencies = [ "tracing", ] +[[package]] +name = "warp-real-ip" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22f23ff9f07b19eab4161057a2378cede84d618d3553cb01fe2111bee684003b" +dependencies = [ + "rfc7239", + "warp", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/filed/Cargo.toml b/filed/Cargo.toml index 8b883e3..eb2837b 100644 --- a/filed/Cargo.toml +++ b/filed/Cargo.toml @@ -27,6 +27,7 @@ tokio = { version = "1.32.0", features = ["rt", "macros", "rt-multi-thread"] } toml = "0.8.2" urlencoding = "2.1.3" warp = "0.3.6" +warp-real-ip = "0.2.0" [profile.release] opt-level = 'z' diff --git a/filed/src/web/forms.rs b/filed/src/web/forms.rs index e3e22ed..f64bff0 100644 --- a/filed/src/web/forms.rs +++ b/filed/src/web/forms.rs @@ -3,13 +3,14 @@ forms.rs - All the forms */ -use std::collections::HashMap; +use std::{collections::HashMap, net::IpAddr}; use askama::Template; use warp::{Filter, reply::{Reply, json, with_status, html}, reject::Rejection, filters::multipart::FormData, http::StatusCode}; use futures_util::TryStreamExt; use bytes::BufMut; use serde::Serialize; +use warp_real_ip::real_ip; use crate::files::{File, lookup::LookupKind, DeleteMode}; @@ -145,7 +146,7 @@ fn bad_req_html(data: String) -> Box { ) } -pub async fn upload(form: FormData, state: SharedState) -> Result, Rejection> { +pub async fn upload(form: FormData, _ip: Option, state: SharedState) -> Result, Rejection> { if ! state.config.files.allow_uploads { return Ok( @@ -251,6 +252,7 @@ pub async fn upload(form: FormData, state: SharedState) -> Result pub fn get_routes(state: SharedState) -> impl Filter + Clone { warp::post() .and(warp::multipart::form()) + .and(real_ip(vec![state.env.proxy_addr])) .and( warp::any().map(move || state.clone()) )