proxy_protocol_rs/config.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::sync::Arc;
16use std::time::Duration;
17
18use crate::policy::{AcceptAll, ConnPolicy};
19use crate::validator::HeaderValidator;
20
21/// Configuration for the Proxy Protocol listener
22pub struct ProxyProtocolConfig {
23 /// Maximum time to wait for the complete PP header after accept;
24 /// default: 5 seconds
25 pub header_timeout: Duration,
26
27 /// Maximum buffer size for reading the PP header;
28 /// default: 4096 bytes
29 pub max_header_size: usize,
30
31 /// Maximum number of connections simultaneously reading PP headers;
32 /// default: 1024
33 pub max_pending_handshakes: usize,
34
35 /// Pre-read connection policy;
36 /// default: `AcceptAll`
37 pub policy: Arc<dyn ConnPolicy>,
38
39 /// Post-parse header validator;
40 /// default: `None`
41 pub validator: Option<Arc<dyn HeaderValidator>>,
42
43 /// Which protocol versions to accept;
44 /// default: `Both`
45 pub version: VersionPreference,
46}
47
48impl Default for ProxyProtocolConfig {
49 fn default() -> Self {
50 Self {
51 header_timeout: Duration::from_secs(5),
52 max_header_size: 4096,
53 max_pending_handshakes: 1024,
54 policy: Arc::new(AcceptAll),
55 validator: None,
56 version: VersionPreference::Both,
57 }
58 }
59}
60
61#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62#[non_exhaustive]
63pub enum VersionPreference {
64 Both,
65 V1Only,
66 V2Only,
67}