From 97507634df83ea2cd5eab2ebf4c35b55242207ff Mon Sep 17 00:00:00 2001 From: Slatian Date: Sat, 25 Feb 2023 13:30:46 +0100 Subject: [PATCH] Added the possibility to serve static files --- Cargo.lock | 60 ++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 +- echoip_config.toml | 4 +++ src/config.rs | 2 ++ src/main.rs | 27 +++++++++++++---- templates/static/robots.txt | 2 ++ 6 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 templates/static/robots.txt diff --git a/Cargo.lock b/Cargo.lock index 8564032..7916e28 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,7 +67,7 @@ dependencies = [ "sync_wrapper", "tokio", "tower", - "tower-http", + "tower-http 0.3.5", "tower-layer", "tower-service", ] @@ -378,6 +378,7 @@ dependencies = [ "tokio", "toml", "tower", + "tower-http 0.4.0", "trust-dns-proto", "trust-dns-resolver", ] @@ -978,6 +979,16 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "mio" version = "0.8.5" @@ -1664,6 +1675,19 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-util" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5427d89453009325de0d8f342c9490009f76e999cb7672d77e46267448f7e6b2" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + [[package]] name = "toml" version = "0.7.2" @@ -1733,6 +1757,31 @@ dependencies = [ "tower-service", ] +[[package]] +name = "tower-http" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d1d42a9b3f3ec46ba828e8d376aec14592ea199f70a06a548587ecd1c4ab658" +dependencies = [ + "bitflags", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-range-header", + "httpdate", + "mime", + "mime_guess", + "percent-encoding", + "pin-project-lite", + "tokio", + "tokio-util", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "tower-layer" version = "0.3.2" @@ -1900,6 +1949,15 @@ dependencies = [ "unic-common", ] +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.10" diff --git a/Cargo.toml b/Cargo.toml index 47fe3ff..73d0296 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,8 @@ serde = { version = "1", features = ["derive"] } tokio = { version = "1", features = ["full"] } tera = "1" toml = "0.7" -tower = "*" +tower = "0.4" +tower-http = { version = "0.4", features = ["fs"] } trust-dns-proto = "0.22" trust-dns-resolver = "0.22" maxminddb = "0.23" diff --git a/echoip_config.toml b/echoip_config.toml index 6917454..212a5b0 100644 --- a/echoip_config.toml +++ b/echoip_config.toml @@ -11,6 +11,10 @@ ip_header = "RightmostXForwardedFor" # When you don't want to use a proxy server: #ip_header = "ConnectInfo" +# You can specify a custom location for static files here +# Defaults to the static directory in the templete direcotry +# Acts as an "underlay" +#static_location = "" # Allow querying of private range ips # enable if you want to use this service diff --git a/src/config.rs b/src/config.rs index 4af9c25..0d55893 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,6 +17,7 @@ pub struct ServerConfig { pub ip_header: SecureClientIpSource, pub allow_private_ip_lookup: bool, + pub static_location: Option, } #[derive(serde::Deserialize, Clone)] @@ -52,6 +53,7 @@ impl Default for ServerConfig { listen_on: "127.0.0.1:3000".parse().unwrap(), ip_header: SecureClientIpSource::ConnectInfo, allow_private_ip_lookup: false, + static_location: None, } } } diff --git a/src/main.rs b/src/main.rs index 21f6b45..ba3266b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use axum::{ }, headers, http::Request, + handler::Handler, middleware::{self, Next}, response::Response, Router, @@ -19,6 +20,7 @@ use lazy_static::lazy_static; use regex::Regex; use tera::Tera; use tower::ServiceBuilder; +use tower_http::services::ServeDir; use trust_dns_resolver::{ TokioAsyncResolver, // config::ResolverOpts, @@ -103,9 +105,11 @@ struct CliArgs { #[arg(short, long)] listen_on: Option, #[arg(short, long)] - templates: Option, + template_location: Option, #[arg(short,long)] extra_config: Option, + #[arg(short,long)] + static_location: Option, } fn match_domain_hidden_list(domain: &String, hidden_list: &Vec) -> bool { @@ -155,7 +159,10 @@ async fn main() { }; // Initalize Tera templates - let mut template_base_dir = (&config.template.template_location).to_owned(); + let mut template_base_dir = match cli_args.template_location { + Some(template_base_dir) => template_base_dir, + None => (&config.template.template_location).to_owned(), + }; if !template_base_dir.ends_with("/") { template_base_dir = template_base_dir + "/"; } @@ -170,7 +177,7 @@ async fn main() { }, }, }; - let template_glob = template_base_dir+"*.html"; + let template_glob = template_base_dir.clone()+"*.html"; println!("Parsing Templates from '{}' ...", &template_glob); let res = Tera::new((template_glob).as_str()); let tera = match res { @@ -186,8 +193,13 @@ async fn main() { template_config: template_extra_config, }; - // Initalize Rate Limiter + // Static file directory + let static_file_directory = cli_args.static_location.unwrap_or( + config.server.static_location.clone().unwrap_or( + template_base_dir+"/static" + ) + ); // Initalize GeoIP Database @@ -243,8 +255,11 @@ async fn main() { .route("/ip/:address", get(handle_ip_route_with_path)) .route("/ua", get(user_agent_handler)) .route("/hi", get(hello_world_handler)) - .fallback(not_found_handler) - .with_state(shared_state) + .fallback_service( + ServeDir::new(static_file_directory) + .fallback(not_found_handler.with_state(shared_state.clone())) + ) + .with_state(shared_state) .layer( ServiceBuilder::new() .layer(ip_header.into_extension()) diff --git a/templates/static/robots.txt b/templates/static/robots.txt new file mode 100644 index 0000000..4c02f8d --- /dev/null +++ b/templates/static/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: /*?