1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (C) 2026 Mullvad VPN AB
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
//! Domain fronting for API connections.
//!
//! This module provides both client and server components for domain fronting,
//! allowing API connections to be tunneled through HTTP POST requests.
//!
//! # Client
//!
//! [`ProxyConnection`] implements [`tokio::io::AsyncRead`] + [`tokio::io::AsyncWrite`], tunneling data via HTTP POST requests.
//! The client establishes an HTTP/1.1 connection and uses POST requests with a session ID header
//! to maintain a bidirectional stream over HTTP.
//!
//! ## Usage
//!
//! With the `tls` feature enabled, provide your own certificate configuration:
//!
//! ```no_run
//! # #[cfg(feature = "tls")]
//! # async fn example_impl() -> Result<(), Box<dyn std::error::Error>> {
//! use domain_fronting::{DomainFronting, ProxyConfig};
//! use tokio::io::{AsyncReadExt, AsyncWriteExt};
//! use std::sync::Arc;
//!
//! let df = DomainFronting::new(
//! "cdn.example.com".to_string(),
//! "api.example.com".to_string(),
//! "X-Session-Id".to_string(),
//! );
//!
//! let proxy_config = df.proxy_config().await?;
//!
//! // Create your TLS config with desired certificate store
//! let mut root_store = tokio_rustls::rustls::RootCertStore::empty();
//! // Add your certificates to root_store...
//!
//! let tls_config = Arc::new(
//! tokio_rustls::rustls::ClientConfig::builder()
//! .with_root_certificates(root_store)
//! .with_no_client_auth()
//! );
//!
//! let mut client = proxy_config.connect_with_tls(tls_config).await?;
//!
//! // Use like a regular AsyncRead + AsyncWrite stream
//! client.write_all(b"Hello").await?;
//! let mut buf = vec![0u8; 1024];
//! let n = client.read(&mut buf).await?;
//! # Ok(())
//! # }
//! # fn main() {}
//! ```
//!
//! # Server
//!
//! [`server::Sessions`] manages HTTP sessions, forwarding data to upstream servers.
//! Each unique session ID (sent via a configurable session header) gets its own
//! upstream TCP connection that persists across multiple HTTP requests.
//!
//! ## Usage
//!
//! ```no_run
//! use domain_fronting::domain_fronting::server::Sessions;
//! use std::sync::Arc;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let upstream_addr = "127.0.0.1:8080".parse()?;
//! let sessions = Sessions::new(upstream_addr, "X-Session-Id".to_string());
//!
//! // Use with hyper to handle HTTP requests
//! // sessions.handle_request(req).await
//! # Ok(())
//! # }
//! ```
//!
//! # Testing
//!
//! Both client and server support generic [`tokio::io::AsyncRead`] + [`tokio::io::AsyncWrite`] streams for testing.
//! Use [`ProxyConnection::from_stream()`] and [`server::Sessions::with_connector()`] to inject
//! custom transports like [`tokio::io::duplex`] for unit tests.
//!
//! # Protocol
//!
//! - Each HTTP POST request contains data to send upstream
//! - Response body contains data received from upstream
//! - Empty POST requests are used for polling when no data needs to be sent
//! - Session cleanup happens when the client disconnects or the upstream closes
use ;
use crate::;
pub use ;
/// Errors that can occur when establishing a domain fronting connection.
/// Configuration for creating a [`ProxyConfig`].
///
/// Contains the fronting domain, session header key and target host.