proxy_protocol_rs/validator.rs
1// Copyright (C) 2025-2026 Michael S. Klishin and Contributors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::net::SocketAddr;
16
17use crate::types::ProxyInfo;
18
19/// Validate a Proxy Protocol header before the connection is passed
20/// to the application
21pub trait HeaderValidator: Send + Sync + 'static {
22 fn validate(
23 &self,
24 header: &ProxyInfo,
25 peer_addr: SocketAddr,
26 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
27}
28
29/// Blanket implementation for closures
30impl<F> HeaderValidator for F
31where
32 F: Fn(&ProxyInfo, SocketAddr) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
33 + Send
34 + Sync
35 + 'static,
36{
37 fn validate(
38 &self,
39 header: &ProxyInfo,
40 peer_addr: SocketAddr,
41 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
42 self(header, peer_addr)
43 }
44}