Skip to content

Commit 7b24d5a

Browse files
authored
Merge pull request #277 from Dstack-TEE/gw-compat
gw: Add 0.3.x compatibility custom domain dns prefix
2 parents c443d3b + 9d76bde commit 7b24d5a

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

gateway/gateway.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ buffer_size = 8192
6060
connect_top_n = 3
6161
localhost_enabled = false
6262
app_address_ns_prefix = "_dstack-app-address"
63+
app_address_ns_compat = true
6364
workers = 32
6465
external_port = 443
6566

gateway/rpc/proto/gateway_rpc.proto

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ message GuestAgentConfig {
4141
uint32 external_port = 1;
4242
// The in CVM port of the guest agent.
4343
uint32 internal_port = 2;
44-
// The domain of the guest agent.
44+
// The base domain of the zt-https
4545
string domain = 3;
46+
// The app address namespace prefix
47+
string app_address_ns_prefix = 4;
4648
}
4749

4850
// StatusResponse is the response for Status.
@@ -145,6 +147,8 @@ message InfoResponse {
145147
string base_domain = 1;
146148
// The external port of the ZT-HTTPS
147149
uint32 external_port = 2;
150+
// The app address namespace prefix
151+
string app_address_ns_prefix = 3;
148152
}
149153

150154
service Gateway {

gateway/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub struct ProxyConfig {
8080
pub localhost_enabled: bool,
8181
pub workers: usize,
8282
pub app_address_ns_prefix: String,
83+
pub app_address_ns_compat: bool,
8384
}
8485

8586
#[derive(Debug, Clone, Deserialize)]

gateway/src/main_service.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,10 @@ impl GatewayRpc for RpcHandler {
735735
servers,
736736
}),
737737
agent: Some(GuestAgentConfig {
738-
external_port: state.config.proxy.listen_port as u32,
738+
external_port: state.config.proxy.external_port as u32,
739739
internal_port: state.config.proxy.agent_port as u32,
740740
domain: state.config.proxy.base_domain.clone(),
741+
app_address_ns_prefix: state.config.proxy.app_address_ns_prefix.clone(),
741742
}),
742743
};
743744
self.state.notify_state_updated.notify_one();
@@ -786,6 +787,7 @@ impl GatewayRpc for RpcHandler {
786787
Ok(InfoResponse {
787788
base_domain: state.config.proxy.base_domain.clone(),
788789
external_port: state.config.proxy.external_port as u32,
790+
app_address_ns_prefix: state.config.proxy.app_address_ns_prefix.clone(),
789791
})
790792
}
791793
}

gateway/src/proxy/tls_passthough.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,42 @@ impl AppAddress {
2929
}
3030

3131
/// resolve app address by sni
32-
async fn resolve_app_address(prefix: &str, sni: &str) -> Result<AppAddress> {
32+
async fn resolve_app_address(prefix: &str, sni: &str, compat: bool) -> Result<AppAddress> {
3333
let txt_domain = format!("{prefix}.{sni}");
3434
let resolver = hickory_resolver::AsyncResolver::tokio_from_system_conf()
3535
.context("failed to create dns resolver")?;
36-
let lookup = resolver
37-
.txt_lookup(txt_domain)
38-
.await
39-
.context("failed to lookup app address")?;
40-
let txt_record = lookup.iter().next().context("no txt record found")?;
41-
let data = txt_record
42-
.txt_data()
43-
.first()
44-
.context("no data in txt record")?;
45-
AppAddress::parse(data).context("failed to parse app address")
36+
37+
if compat && prefix != "_tapp-address" {
38+
let txt_domain_legacy = format!("_tapp-address.{sni}");
39+
let (lookup, lookup_legacy) = tokio::join!(
40+
resolver.txt_lookup(txt_domain),
41+
resolver.txt_lookup(txt_domain_legacy),
42+
);
43+
for lookup in [lookup, lookup_legacy] {
44+
let Ok(lookup) = lookup else {
45+
continue;
46+
};
47+
let Some(txt_record) = lookup.iter().next() else {
48+
continue;
49+
};
50+
let Some(data) = txt_record.txt_data().first() else {
51+
continue;
52+
};
53+
return AppAddress::parse(data).context("failed to parse app address");
54+
}
55+
anyhow::bail!("failed to resolve app address");
56+
} else {
57+
let lookup = resolver
58+
.txt_lookup(txt_domain)
59+
.await
60+
.context("failed to lookup app address")?;
61+
let txt_record = lookup.iter().next().context("no txt record found")?;
62+
let data = txt_record
63+
.txt_data()
64+
.first()
65+
.context("no data in txt record")?;
66+
AppAddress::parse(data).context("failed to parse app address")
67+
}
4668
}
4769

4870
pub(crate) async fn proxy_with_sni(
@@ -51,7 +73,9 @@ pub(crate) async fn proxy_with_sni(
5173
buffer: Vec<u8>,
5274
sni: &str,
5375
) -> Result<()> {
54-
let addr = resolve_app_address(&state.config.proxy.app_address_ns_prefix, sni)
76+
let ns_prefix = &state.config.proxy.app_address_ns_prefix;
77+
let compat = state.config.proxy.app_address_ns_compat;
78+
let addr = resolve_app_address(ns_prefix, sni, compat)
5579
.await
5680
.context("failed to resolve app address")?;
5781
debug!("target address is {}:{}", addr.app_id, addr.port);
@@ -123,6 +147,7 @@ mod tests {
123147
let app_addr = resolve_app_address(
124148
"_dstack-app-address",
125149
"3327603e03f5bd1f830812ca4a789277fc31f577.app.kvin.wang",
150+
false,
126151
)
127152
.await
128153
.unwrap();

0 commit comments

Comments
 (0)