-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathtls_passthough.rs
More file actions
296 lines (274 loc) · 9.9 KB
/
Copy pathtls_passthough.rs
File metadata and controls
296 lines (274 loc) · 9.9 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// SPDX-FileCopyrightText: © 2024-2025 Phala Network <dstack@phala.network>
//
// SPDX-License-Identifier: Apache-2.0
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Debug;
use std::sync::atomic::Ordering;
use anyhow::{bail, Context, Result};
use proxy_protocol::ProxyHeader;
use tokio::{io::AsyncWriteExt, net::TcpStream, task::JoinSet, time::timeout};
use tracing::{debug, info, warn};
use crate::{
main_service::Proxy,
models::{Counting, EnteredCounter},
};
use super::{
io_bridge::bridge,
port_policy::{filter_allowed_addresses, should_send_pp},
AddressGroup,
};
#[derive(Debug)]
struct AppAddress {
app_id: String,
port: u16,
}
impl AppAddress {
fn parse(data: &[u8]) -> Result<Self> {
// format: "3327603e03f5bd1f830812ca4a789277fc31f577:555"
let data = String::from_utf8(data.to_vec()).context("invalid app address")?;
let (app_id, port) = data.split_once(':').context("invalid app address")?;
Ok(Self {
app_id: app_id.to_string(),
port: port.parse().context("invalid port")?,
})
}
}
/// find app by app id in current memory box
fn select_app_address(items: &[Box<[u8]>], known_apps: &BTreeMap<String, BTreeSet<String>>) -> Result<AppAddress> {
let mut fallback = None;
for data in items {
if let Ok(addr) = AppAddress::parse(data) {
if known_apps.contains_key(&addr.app_id) {
return Ok(addr);
}
if fallback.is_none() {
fallback = Some(addr);
}
}
}
fallback.context("no app address found in txt record")
}
/// resolve app address by sni
async fn resolve_app_address(prefix: &str, sni: &str, compat: bool, state: &Proxy) -> Result<AppAddress> {
let txt_domain = format!("{prefix}.{sni}");
let resolver = hickory_resolver::AsyncResolver::tokio_from_system_conf()
.context("failed to create dns resolver")?;
if compat && prefix != "_tapp-address" {
let txt_domain_legacy = format!("_tapp-address.{sni}");
let (lookup, lookup_legacy) = tokio::join!(
resolver.txt_lookup(txt_domain),
resolver.txt_lookup(txt_domain_legacy),
);
for lookup in [lookup, lookup_legacy] {
let Ok(lookup) = lookup else {
continue;
};
let Some(txt_record) = lookup.iter().next() else {
continue;
};
let locked = state.lock();
if let Ok(addr) = select_app_address(txt_record.txt_data(), &locked.state.apps) {
return Ok(addr);
}
}
} else if let Ok(lookup) = resolver.txt_lookup(txt_domain).await {
if let Some(txt_record) = lookup.iter().next() {
let locked = state.lock();
if let Ok(addr) = select_app_address(txt_record.txt_data(), &locked.state.apps) {
return Ok(addr);
}
}
}
// wildcard fallback: try {prefix}-wildcard.{parent_domain}
if let Some((_, parent)) = sni.split_once('.') {
let wildcard_domain = format!("{prefix}-wildcard.{parent}");
let lookup = resolver
.txt_lookup(&wildcard_domain)
.await
.with_context(|| {
format!("failed to lookup wildcard app address for {sni} via {wildcard_domain}")
})?;
let txt_record = lookup
.iter()
.next()
.with_context(|| format!("no txt record found for {sni} via {wildcard_domain}"))?;
let locked = state.lock();
return select_app_address(txt_record.txt_data(), &locked.state.apps)
.with_context(|| {
format!("failed to parse app address for {sni} via {wildcard_domain}")
});
}
anyhow::bail!("failed to resolve app address for {sni}");
}
pub(crate) async fn proxy_with_sni(
state: Proxy,
inbound: TcpStream,
pp_header: ProxyHeader,
buffer: Vec<u8>,
sni: &str,
) -> Result<()> {
let ns_prefix = &state.config.proxy.app_address_ns_prefix;
let compat = state.config.proxy.app_address_ns_compat;
let dns_timeout = state.config.proxy.timeouts.dns_resolve;
let addr = timeout(dns_timeout, resolve_app_address(ns_prefix, sni, compat, &state))
.await
.with_context(|| format!("DNS TXT resolve timeout for {sni}"))?
.with_context(|| format!("failed to resolve app address for {sni}"))?;
debug!("target address is {}:{}", addr.app_id, addr.port);
proxy_to_app(state, inbound, pp_header, buffer, &addr.app_id, addr.port).await
}
/// Check if app has reached max connections limit
fn check_connection_limit(
addresses: &AddressGroup,
max_connections: u64,
app_id: &str,
) -> Result<()> {
if max_connections == 0 {
return Ok(());
}
let total: u64 = addresses
.iter()
.map(|a| a.counter.load(Ordering::Relaxed))
.sum();
if total >= max_connections {
warn!(
app_id,
total, max_connections, "app connection limit exceeded"
);
bail!("app connection limit exceeded: {total}/{max_connections}");
}
Ok(())
}
/// connect to multiple hosts simultaneously and return the first successful connection
/// along with the instance_id of the winning address.
pub(crate) async fn connect_multiple_hosts(
addresses: AddressGroup,
port: u16,
max_connections: u64,
app_id: &str,
) -> Result<(TcpStream, EnteredCounter, String)> {
check_connection_limit(&addresses, max_connections, app_id)?;
let mut join_set = JoinSet::new();
for addr in addresses {
let counter = addr.counter.enter();
let ip = addr.ip;
let instance_id = addr.instance_id;
debug!("connecting to {ip}:{port}");
let future = TcpStream::connect((ip, port));
join_set.spawn(async move {
(
future.await.map_err(|e| (e, ip, port)),
counter,
instance_id,
)
});
}
// select the first successful connection
let (connection, counter, instance_id) = loop {
let (result, counter, instance_id) = join_set
.join_next()
.await
.context("No connection success")?
.context("Failed to join the connect task")?;
match result {
Ok(connection) => break (connection, counter, instance_id),
Err((e, addr, port)) => {
info!("failed to connect to app@{addr}:{port}: {e}");
}
}
};
debug!("connected to {:?}", connection.peer_addr());
Ok((connection, counter, instance_id))
}
pub(crate) async fn proxy_to_app(
state: Proxy,
inbound: TcpStream,
pp_header: ProxyHeader,
buffer: Vec<u8>,
app_id: &str,
port: u16,
) -> Result<()> {
let addresses = state.lock().select_top_n_hosts(app_id)?;
let addresses = filter_allowed_addresses(&state, addresses, app_id, port)?;
let max_connections = state.config.proxy.max_connections_per_app;
let (mut outbound, _counter, instance_id) = timeout(
state.config.proxy.timeouts.connect,
connect_multiple_hosts(addresses.clone(), port, max_connections, app_id),
)
.await
.with_context(|| format!("connecting timeout to app {app_id}: {addresses:?}:{port}"))?
.with_context(|| format!("failed to connect to app {app_id}: {addresses:?}:{port}"))?;
if should_send_pp(&state, &instance_id, port) {
let pp_header_bin =
proxy_protocol::encode(pp_header).context("failed to encode pp header")?;
outbound.write_all(&pp_header_bin).await?;
}
outbound
.write_all(&buffer)
.await
.context("failed to write to app")?;
bridge(inbound, outbound, &state.config.proxy)
.await
.context("failed to copy between inbound and outbound")?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
config::{load_config_figment, Config, MutualConfig, TlsConfig},
main_service::ProxyOptions,
};
use tempfile::TempDir;
fn boxed(s: &[u8]) -> Box<[u8]> {
s.to_vec().into_boxed_slice()
}
async fn create_test_proxy() -> (Proxy, TempDir) {
let figment = load_config_figment(None);
let mut config = figment.focus("core").extract::<Config>().unwrap();
let temp_dir = TempDir::new().expect("failed to create temp dir");
config.sync.data_dir = temp_dir.path().to_string_lossy().to_string();
let proxy = Proxy::new(ProxyOptions {
config,
my_app_id: None,
tls_config: TlsConfig {
certs: "".to_string(),
key: "".to_string(),
mutual: MutualConfig { ca_certs: "".to_string() },
},
})
.await
.expect("failed to create proxy");
(proxy, temp_dir)
}
#[tokio::test]
async fn test_resolve_app_address() {
let (state, _dir) = create_test_proxy().await;
let app_addr = resolve_app_address(
"_dstack-app-address",
"3327603e03f5bd1f830812ca4a789277fc31f577.app.dstack.org",
false,
&state,
)
.await
.unwrap();
assert_eq!(app_addr.app_id, "3327603e03f5bd1f830812ca4a789277fc31f577");
assert_eq!(app_addr.port, 8090);
}
#[test]
fn test_select_app_address_prefers_local() {
let items = vec![boxed(b"aaaaaa:443"), boxed(b"bbbbbb:8080")];
let mut apps = BTreeMap::new();
apps.insert("bbbbbb".to_string(), BTreeSet::new());
let addr = select_app_address(&items, &apps).unwrap();
assert_eq!(addr.app_id, "bbbbbb");
assert_eq!(addr.port, 8080);
}
#[test]
fn test_select_app_address_fallback_when_none_local() {
let items = vec![boxed(b"aaaaaa:443"), boxed(b"bbbbbb:8080")];
let addr = select_app_address(&items, &BTreeMap::new()).unwrap();
assert_eq!(addr.app_id, "aaaaaa");
assert_eq!(addr.port, 443);
}
}