-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathtls_passthough.rs
More file actions
282 lines (257 loc) · 9.42 KB
/
Copy pathtls_passthough.rs
File metadata and controls
282 lines (257 loc) · 9.42 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
// SPDX-FileCopyrightText: © 2024-2025 Phala Network <dstack@phala.network>
//
// SPDX-License-Identifier: Apache-2.0
use std::fmt::Debug;
use std::sync::atomic::Ordering;
use std::time::Duration;
use anyhow::{bail, Context, Result};
use hickory_resolver::lookup::Lookup;
use hickory_resolver::proto::rr::RData;
use hickory_resolver::TokioResolver;
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,
};
const APP_ADDRESS_DNS_CACHE_SIZE: usize = 256;
const APP_ADDRESS_NEGATIVE_CACHE_TTL: Duration = Duration::from_secs(10);
#[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")?,
})
}
}
/// Shared resolver for SNI -> app address TXT lookups.
///
/// Hickory's resolver already has an internal TTL-aware DNS cache. The old
/// code created a new resolver per proxy connection, which defeated that cache.
/// Keeping a resolver in `ProxyInner` makes TXT caching effective across
/// connections without introducing a separate cache invalidation policy here.
pub(crate) struct AppAddressResolver {
prefix: String,
compat: bool,
resolver: TokioResolver,
}
impl AppAddressResolver {
pub(crate) fn new(prefix: String, compat: bool) -> Result<Self> {
Ok(Self {
prefix,
compat,
resolver: app_address_tokio_resolver_from_system_conf()?,
})
}
async fn resolve(&self, sni: &str) -> Result<AppAddress> {
resolve_app_address(&self.resolver, &self.prefix, sni, self.compat).await
}
}
fn app_address_tokio_resolver_from_system_conf() -> Result<TokioResolver> {
let mut builder = TokioResolver::builder_tokio().context("failed to read system dns config")?;
// App-address records may appear shortly after a CVM/app is registered.
// Reusing one resolver enables positive TXT caching, but we do not want a
// transient NXDOMAIN/NODATA response to hide a newly-added app for too
// long. Keep positive caching TTL-aware and cap negative caching.
let options = builder.options_mut();
options.cache_size = APP_ADDRESS_DNS_CACHE_SIZE as u64;
options.negative_min_ttl = Some(Duration::ZERO);
options.negative_max_ttl = Some(APP_ADDRESS_NEGATIVE_CACHE_TTL);
builder.build().context("failed to build dns resolver")
}
fn parse_lookup(lookup: &Lookup, sni: &str, txt_domain: &str) -> Result<Option<AppAddress>> {
for answer in lookup.answers() {
let RData::TXT(txt) = &answer.data else {
continue;
};
let Some(data) = txt.txt_data.first() else {
continue;
};
return AppAddress::parse(data)
.with_context(|| format!("failed to parse app address for {sni} via {txt_domain}"))
.map(Some);
}
Ok(None)
}
/// Resolve app address by SNI. `resolver` is shared so its DNS cache is reused.
async fn resolve_app_address(
resolver: &TokioResolver,
prefix: &str,
sni: &str,
compat: bool,
) -> Result<AppAddress> {
let txt_domain = format!("{prefix}.{sni}");
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, domain) in [
(lookup, txt_domain.as_str()),
(lookup_legacy, txt_domain_legacy.as_str()),
] {
let Ok(lookup) = lookup else {
continue;
};
if let Some(app_address) = parse_lookup(&lookup, sni, domain)? {
return Ok(app_address);
}
}
} else if let Ok(lookup) = resolver.txt_lookup(&txt_domain).await {
if let Some(app_address) = parse_lookup(&lookup, sni, &txt_domain)? {
return Ok(app_address);
}
}
// 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}")
})?;
return parse_lookup(&lookup, sni, &wildcard_domain)?
.with_context(|| format!("no txt record found 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 dns_timeout = state.config.proxy.timeouts.dns_resolve;
let addr = timeout(dns_timeout, state.app_address_resolver.resolve(sni))
.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::*;
#[tokio::test]
async fn test_resolve_app_address() -> Result<()> {
let resolver = AppAddressResolver::new("_dstack-app-address".to_string(), false)?;
let app_addr = resolver
.resolve("3327603e03f5bd1f830812ca4a789277fc31f577.app.dstack.org")
.await?;
assert_eq!(app_addr.app_id, "3327603e03f5bd1f830812ca4a789277fc31f577");
assert_eq!(app_addr.port, 8090);
Ok(())
}
}