Skip to main content

TrustedProxies

Struct TrustedProxies 

Source
pub struct TrustedProxies { /* private fields */ }
Available on crate feature iputil only.
Expand description

“可信代理网段 + 该边缘设置的客户端 IP 头“组合, 供鉴权、限流、封禁等安全决策取客户端 IP。

构造时必须显式声明部署边缘会强制覆盖的头 (Cloudflare 的 CF-Connecting-IP、 EdgeOne 的 EO-Client-IP、自建 nginx 明确 set 的 X-Real-IP, 或 X-Forwarded-For)。 不提供“来源可信就信任所有厂商头“的模式: 代理 / CDN 会透传客户端伪造的其它厂商头。

启动时构建一次, 之后可在多线程间共享 (Send + Sync)。

Implementations§

Source§

impl TrustedProxies

Source

pub fn new<I, S>(trusted_header: &str, cidrs: I) -> Result<Self, Error>
where I: IntoIterator<Item = S>, S: AsRef<str>,

构建可信代理集合。

trusted_header 为部署边缘强制覆盖的客户端 IP 头, 不能为空; cidrs 支持 CIDR (如 10.0.0.0/8) 或单个 IP (按 /32、/128 处理), 空白条目忽略, 任一条目非法即返回错误, 不静默跳过; 至少要有一个网段。

Source

pub fn contains(&self, ip: &str) -> bool

判断 IP 字符串是否落在可信网段内 (去首尾空白); IP 非法时返回 false

Source

pub fn contains_ip(&self, ip: IpAddr) -> bool

判断 IP 是否落在可信网段内 (IPv4 映射地址按 IPv4 判断)

Source

pub fn client_ip(&self, headers: &HeaderMap, remote: IpAddr) -> IpAddr

带信任边界校验地获取客户端 IP; 结果已规范化 (IPv4 映射地址转为 IPv4), 可直接作为限流 / 封禁的键:

  • 直连来源不在可信网段: 忽略一切转发头, 返回直连 IP
  • 可信头为 X-Forwarded-For: 从右往左跳过可信网段条目, 返回第一个不可信的合法 IP; 链上全部可信时返回最左侧合法 IP (内部设施互调), 无合法条目时返回直连 IP
  • 其它可信头: 返回该头的值 (自动去端口), 缺失或不是合法 IP 时返回直连 IP
Source

pub fn client_ip_from_request<B>(&self, req: &Request<B>) -> Option<IpAddr>

TrustedProxies::client_ip 的请求版: 直连地址从请求扩展读取 (见 remote_ip); 读不到直连地址时返回 None

Trait Implementations§

Source§

impl Clone for TrustedProxies

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TrustedProxies

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for TrustedProxies

§

impl Forget for TrustedProxies

§

impl Move for TrustedProxies

§

impl RefUnwindSafe for TrustedProxies

§

impl Send for TrustedProxies
where RawVecInner: Send, Bytes: Send,

§

impl Sync for TrustedProxies
where RawVecInner: Sync, Bytes: Sync,

§

impl Unpin for TrustedProxies
where *const u8 is !null: Unpin, *const u8: Unpin, Align8<*mut ()>: Unpin, &'static Vtable: Unpin,

§

impl UnsafeUnpin for TrustedProxies
where *const u8 is !null: UnsafeUnpin, PhantomData<IpNet>: UnsafeUnpin, *const u8: UnsafeUnpin, Align8<*mut ()>: UnsafeUnpin, &'static Vtable: UnsafeUnpin,

§

impl UnwindSafe for TrustedProxies
where RawVecInner: UnwindSafe, *const u8: UnwindSafe, Align8<*mut ()>: UnwindSafe, &'static Vtable: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.