-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.rs
More file actions
228 lines (200 loc) · 7.53 KB
/
Copy pathproxy.rs
File metadata and controls
228 lines (200 loc) · 7.53 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//! Proxy server entrypoint and handler wiring.
mod common;
mod http;
mod streaming;
mod websocket;
use std::net::TcpListener;
use std::path::PathBuf;
use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::{Arc, mpsc};
use std::thread;
use std::time::Duration;
use hudsucker::{Proxy, rustls::crypto::aws_lc_rs};
use crate::errors::KeyclawError;
use crate::pipeline::Processor;
use self::common::{build_ca_authority, log_warn, normalize_hosts};
pub use self::common::{set_log_file, set_unsafe_log};
/// Configured proxy server ready to start.
pub struct Server {
/// Local listen address.
pub listen_addr: String,
/// Lowercased hostnames eligible for interception.
pub allowed_hosts: Vec<String>,
/// Shared rewrite processor used by request and response handlers.
pub processor: Arc<Processor>,
/// Maximum request body size accepted for interception.
pub max_body_bytes: i64,
/// Timeout for request body collection before inspection.
pub body_timeout: Duration,
/// Optional persistent audit log path.
pub audit_log_path: Option<PathBuf>,
/// Whether to fall back to an ephemeral localhost port when the requested
/// listen address is already in use.
pub allow_addr_in_use_fallback: bool,
/// PEM-encoded local CA certificate.
pub ca_cert_pem: String,
/// PEM-encoded local CA private key.
pub ca_key_pem: String,
intercepted: Arc<AtomicI64>,
}
/// Handle to a running proxy instance.
pub struct RunningServer {
/// Effective listen address after bind or fallback selection.
pub addr: String,
intercepted: Arc<AtomicI64>,
shutdown: Option<tokio::sync::oneshot::Sender<()>>,
join: Option<thread::JoinHandle<()>>,
}
#[derive(Clone)]
struct KeyclawHttpHandler {
allowed_hosts: Vec<String>,
processor: Arc<Processor>,
max_body_bytes: i64,
body_timeout: Duration,
audit_log_path: Option<PathBuf>,
intercepted: Arc<AtomicI64>,
}
impl Drop for RunningServer {
fn drop(&mut self) {
if let Some(shutdown) = self.shutdown.take() {
let _ = shutdown.send(());
}
if let Some(join) = self.join.take() {
let _ = join.join();
}
}
}
impl RunningServer {
/// Return the number of intercepted requests handled by this process.
pub fn intercept_count(&self) -> i64 {
self.intercepted.load(Ordering::SeqCst)
}
}
impl Server {
/// Create a new proxy server with the provided listen address, allowed
/// hosts, processor, and runtime CA material.
pub fn new(
listen_addr: String,
allowed_hosts: Vec<String>,
processor: Arc<Processor>,
ca_cert_pem: String,
ca_key_pem: String,
) -> Self {
Self {
listen_addr,
allowed_hosts: normalize_hosts(&allowed_hosts),
processor,
max_body_bytes: 2 * 1024 * 1024,
body_timeout: Duration::from_secs(3),
audit_log_path: None,
allow_addr_in_use_fallback: true,
ca_cert_pem,
ca_key_pem,
intercepted: Arc::new(AtomicI64::new(0)),
}
}
/// Start the proxy and return a handle that can be dropped or shut down
/// later.
pub fn start(&self) -> Result<RunningServer, KeyclawError> {
self.processor.warm_up()?;
let bind_addr = if self.listen_addr.trim().is_empty() {
"127.0.0.1:8877".to_string()
} else {
self.listen_addr.clone()
};
let listener = match TcpListener::bind(&bind_addr) {
Ok(listener) => listener,
Err(e)
if e.kind() == std::io::ErrorKind::AddrInUse && self.allow_addr_in_use_fallback =>
{
let fallback = "127.0.0.1:0";
log_warn(format!(
"proxy listen address {bind_addr} is busy, falling back to {fallback}"
));
TcpListener::bind(fallback).map_err(|fallback_err| {
KeyclawError::uncoded(format!(
"listen on {bind_addr} failed ({e}); fallback {fallback} failed ({fallback_err})"
))
})?
}
Err(e) => {
return Err(KeyclawError::uncoded(format!(
"listen on {bind_addr} failed: {e}"
)));
}
};
let addr = listener
.local_addr()
.map_err(|e| KeyclawError::uncoded(format!("read local addr failed: {e}")))?;
listener
.set_nonblocking(true)
.map_err(|e| KeyclawError::uncoded(format!("set listener nonblocking failed: {e}")))?;
let allowed_hosts = self.allowed_hosts.clone();
let processor = Arc::clone(&self.processor);
let intercepted = Arc::clone(&self.intercepted);
let max_body_bytes = self.max_body_bytes;
let body_timeout = self.body_timeout;
let audit_log_path = self.audit_log_path.clone();
let ca_cert_pem = self.ca_cert_pem.clone();
let ca_key_pem = self.ca_key_pem.clone();
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel::<()>();
let (ready_tx, ready_rx) = mpsc::channel::<Result<(), String>>();
let ready_tx_err = ready_tx.clone();
let join = thread::spawn(move || {
let runtime = match tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
{
Ok(rt) => rt,
Err(e) => {
let _ = ready_tx_err.send(Err(format!("create tokio runtime failed: {e}")));
return;
}
};
let result = runtime.block_on(async move {
let listener = tokio::net::TcpListener::from_std(listener)
.map_err(|e| format!("adopt listener failed: {e}"))?;
let ca =
build_ca_authority(&ca_cert_pem, &ca_key_pem).map_err(|e| e.to_string())?;
let handler = KeyclawHttpHandler {
allowed_hosts,
processor,
max_body_bytes,
body_timeout,
audit_log_path,
intercepted,
};
let shutdown = async move {
let _ = shutdown_rx.await;
};
let proxy = Proxy::builder()
.with_listener(listener)
.with_ca(ca)
.with_rustls_connector(aws_lc_rs::default_provider())
.with_http_handler(handler.clone())
.with_websocket_handler(handler)
.with_graceful_shutdown(shutdown)
.build()
.map_err(|e| format!("build proxy failed: {e}"))?;
let _ = ready_tx.send(Ok(()));
proxy
.start()
.await
.map_err(|e| format!("proxy exited with error: {e}"))
});
if let Err(err) = result {
let _ = ready_tx_err.send(Err(err));
}
});
match ready_rx.recv_timeout(Duration::from_secs(5)) {
Ok(Ok(())) => Ok(RunningServer {
addr: addr.to_string(),
intercepted: Arc::clone(&self.intercepted),
shutdown: Some(shutdown_tx),
join: Some(join),
}),
Ok(Err(msg)) => Err(KeyclawError::uncoded(msg)),
Err(_) => Err(KeyclawError::uncoded("proxy startup timeout")),
}
}
}