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 addresses or CIDR ranges. IPv6 addresses are not matched by any rule; their handling depends on the filter mode — they are blocked in allow mode and passed in deny mode.

  • 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"]));

Structs§

IpFilter
Middleware that filters requests by client IPv4 address.