Skip to main content

Crate vpndetection_axum

Crate vpndetection_axum 

Source
Expand description

The official axum middleware for the VPNDetection API.

It classifies the visitor behind each request - VPN, residential proxy, Tor, hosting, CDN, relay - and puts the answer on the request. Blocking is opt-in.

It is a tower_layer::Layer, so it works in any tower stack, not only axum.

use axum::{Router, routing::get};
use vpndetection_axum::{Found, VPNDetection};

let guard = VPNDetection::new(
    vpndetection::middleware::Options::new().api_key(std::env::var("VPNDETECTION_API_KEY")?),
)?;

let app: Router = Router::new().route("/", get(hello)).layer(guard);

async fn hello(Found(found): Found) -> &'static str {
    match found.result.as_ref().map(|r| r.is_vpn) {
        Some(true) => "Hello, VPN user",
        _ => "Hello",
    }
}

By default nothing is blocked. Every request carries a vpndetection::middleware::Lookup and your own handlers decide what that means - which is usually what you want, because whether a VPN visitor is a problem depends entirely on what they are doing.

§Where the client address comes from

By default the middleware reads axum’s axum::extract::ConnectInfo, which is the socket peer - and which is absent unless you served the app with axum::Router::into_make_service_with_connect_info. Behind nginx, a load balancer or a CDN, that peer is your proxy: a datacenter address, so a hosting rule would block every visitor you have. Name your edge’s header instead:

Options::new().ip_selector(vpndetection::middleware::header("CF-Connecting-IP"));

Modules§

middleware
The framework-agnostic half of a web middleware: resolve a client address, classify it, and decide whether the condition matched.

Structs§

Found
What the middleware found out about this visitor.
MaybeFound
Found, for a route where the middleware may not have run.
VPNDetection
The middleware, as a tower_layer::Layer.
VPNDetectionService
What VPNDetection wraps a service in. You never name this type.