Skip to main content

Module ip_filter

Module ip_filter 

Source
Expand description

IP address allowlist and denylist middleware.

IpFilter inspects the client IP from crate::server::ConnectionInfo and either allows or blocks the request based on a list of IPv4 or IPv6 addresses and CIDR ranges. An IPv4 rule also matches the same client connecting over a dual-stack socket as an IPv4-mapped IPv6 address (::ffff:1.2.3.4).

  • Allow mode — only listed addresses/ranges pass; all others get 403 Forbidden.
  • Deny mode — listed addresses/ranges get 403 Forbidden; all others pass.

§Example

use rust_web_server::app::App;
use rust_web_server::core::New;
use rust_web_server::ip_filter::IpFilter;

// Restrict to internal networks only.
let app = App::new()
    .wrap(IpFilter::allow(["10.0.0.0/8", "192.168.0.0/16", "127.0.0.1"]));

// Block a known-bad address range.
let app = App::new()
    .wrap(IpFilter::deny(["1.2.3.4", "5.6.7.0/24", "2001:db8::/32"]));

Structs§

IpFilter
Middleware that filters requests by client IP address.

Functions§

is_valid_entry
true if entry is an address or CIDR range IpFilter understands.