58 lines
1.4 KiB
Rust
58 lines
1.4 KiB
Rust
use std::net::IpAddr;
|
|
|
|
use clap::Parser;
|
|
use rocket::http::Status;
|
|
use rocket::serde::{json::Json, Serialize};
|
|
use rocket::{get, launch, routes};
|
|
|
|
mod guards;
|
|
use guards::{ClientIP, TrustedProxies};
|
|
|
|
#[derive(Parser, Debug)]
|
|
#[clap(version, about)]
|
|
struct Args {
|
|
/// Trusted proxy IP addresses, enabling X-FORWARDED-FOR and X-REAL-IP headers inspection
|
|
#[clap(short, long, multiple_values = true)]
|
|
trusted_proxies: Vec<String>,
|
|
}
|
|
|
|
#[get("/")]
|
|
fn check_connectivity() -> Status {
|
|
Status::NoContent
|
|
}
|
|
|
|
#[get("/")]
|
|
fn what_is_my_ip(client_ip: ClientIP) -> String {
|
|
client_ip.0
|
|
}
|
|
|
|
#[derive(Serialize, Debug)]
|
|
#[serde(crate = "rocket::serde")]
|
|
struct ResponseIP {
|
|
ip: String,
|
|
}
|
|
|
|
#[get("/json", format = "json")]
|
|
fn what_is_my_ip_json(client_ip: ClientIP) -> Json<ResponseIP> {
|
|
Json(ResponseIP { ip: client_ip.0 })
|
|
}
|
|
|
|
#[launch]
|
|
fn rocket() -> _ {
|
|
let args = Args::parse();
|
|
|
|
// parse provided trusted proxy IP addresses
|
|
let mut trusted_proxies: Vec<IpAddr> = Vec::new();
|
|
for ip_address in &args.trusted_proxies {
|
|
match ip_address.parse() {
|
|
Ok(ip_address) => trusted_proxies.push(ip_address),
|
|
Err(e) => eprintln!("Could not parse trusted proxy IP address {ip_address} : {e}"),
|
|
}
|
|
}
|
|
|
|
rocket::build()
|
|
.manage(TrustedProxies(trusted_proxies))
|
|
.mount("/", routes![check_connectivity])
|
|
.mount("/ip", routes![what_is_my_ip, what_is_my_ip_json])
|
|
}
|