Skip to main content

shared/
tunnel.rs

1use crate::{
2    database::Database,
3    models::egg_configuration::{EggConfiguration, EggConfigurationTarget},
4    prelude::*,
5};
6use ed25519_dalek::pkcs8::EncodePrivateKey;
7use serde::{Deserialize, Serialize};
8use sqlx::{Row, Type};
9use std::{
10    collections::HashSet,
11    sync::{Arc, OnceLock},
12};
13use tundra_common::{
14    hash::Hash32,
15    jwt::JwtIssuer,
16    state::{AclEntry, NodeEntry, PortSpec, Proto, ServerEntry, Snapshot},
17};
18use utoipa::ToSchema;
19
20pub use tundra_common::state::MAX_SERVER_IDX;
21
22const JWT_KEY_SETTING: &str = "::tunnel_jwt_key";
23
24/// Every write that changes what [`snapshot`] would return has to bump the epoch, or the nodes
25/// keep serving the state they already hold. Models bump; the route that owns the commit then
26/// calls [`poke_nodes`].
27pub async fn bump_epoch<'a>(
28    executor: impl sqlx::PgExecutor<'a>,
29) -> Result<i64, crate::database::DatabaseError> {
30    Ok(sqlx::query_scalar("SELECT nextval('tunnel_epoch')")
31        .fetch_one(executor)
32        .await?)
33}
34
35pub async fn bump_epoch_if_node_on_mesh(
36    transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
37    node_uuid: uuid::Uuid,
38) -> Result<bool, crate::database::DatabaseError> {
39    let member: bool = sqlx::query_scalar(
40        "SELECT EXISTS (SELECT 1 FROM node_tunnels WHERE node_tunnels.node_uuid = $1)",
41    )
42    .bind(node_uuid)
43    .fetch_one(&mut **transaction)
44    .await?;
45
46    if member {
47        bump_epoch(&mut **transaction).await?;
48    }
49
50    Ok(member)
51}
52
53pub async fn bump_epoch_if_server_on_mesh(
54    transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
55    server_uuid: uuid::Uuid,
56) -> Result<bool, crate::database::DatabaseError> {
57    let member: bool = sqlx::query_scalar(
58        "SELECT EXISTS (SELECT 1 FROM server_tunnels WHERE server_tunnels.server_uuid = $1)",
59    )
60    .bind(server_uuid)
61    .fetch_one(&mut **transaction)
62    .await?;
63
64    if member {
65        bump_epoch(&mut **transaction).await?;
66    }
67
68    Ok(member)
69}
70
71pub async fn epoch<'a>(
72    executor: impl sqlx::PgExecutor<'a>,
73) -> Result<i64, crate::database::DatabaseError> {
74    Ok(sqlx::query_scalar("SELECT last_value FROM tunnel_epoch")
75        .fetch_one(executor)
76        .await?)
77}
78
79pub fn frontend_address(idx: u16) -> Option<compact_str::CompactString> {
80    tundra_common::state::frontend_ip(idx).map(|ip| compact_str::format_compact!("{ip}"))
81}
82
83#[inline]
84pub fn alias_of(uuid_short: i32) -> compact_str::CompactString {
85    compact_str::format_compact!("{uuid_short:08x}")
86}
87
88#[inline]
89pub fn is_alias_shaped(name: &str) -> bool {
90    name.len() == 8
91        && name
92            .bytes()
93            .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase())
94}
95
96pub struct TunnelSigner {
97    pub issuer: JwtIssuer,
98    pub public: Hash32,
99}
100
101static SIGNER: OnceLock<Arc<TunnelSigner>> = OnceLock::new();
102
103pub async fn signer(
104    database: &Database,
105) -> Result<Arc<TunnelSigner>, crate::database::DatabaseError> {
106    if let Some(signer) = SIGNER.get() {
107        return Ok(Arc::clone(signer));
108    }
109
110    let stored: Option<String> = sqlx::query_scalar("SELECT value FROM settings WHERE key = $1")
111        .bind(JWT_KEY_SETTING)
112        .fetch_optional(database.read())
113        .await?;
114
115    let stored = match stored {
116        Some(stored) => stored,
117        None => {
118            let candidate = database
119                .encrypt_base64(hex::encode(rand::random::<[u8; 32]>()))
120                .await?;
121
122            sqlx::query_scalar(
123                r#"
124                INSERT INTO settings (key, value)
125                VALUES ($1, $2)
126                ON CONFLICT (key) DO UPDATE SET value = settings.value
127                RETURNING value
128                "#,
129            )
130            .bind(JWT_KEY_SETTING)
131            .bind(candidate.as_str())
132            .fetch_one(database.write())
133            .await?
134        }
135    };
136
137    let mut seed = [0; 32];
138    hex::decode_to_slice(database.decrypt_base64(stored).await?.as_str(), &mut seed)
139        .map_err(|err| anyhow::anyhow!("the stored tunnel signing key is malformed: {err}"))?;
140
141    let key = ed25519_dalek::SigningKey::from_bytes(&seed);
142    let public = Hash32(key.verifying_key().to_bytes());
143    let issuer = JwtIssuer::from_pkcs8_der(
144        key.to_pkcs8_der()
145            .map_err(|err| anyhow::anyhow!("failed to encode the tunnel signing key: {err}"))?
146            .as_bytes(),
147    );
148
149    Ok(Arc::clone(
150        SIGNER.get_or_init(|| Arc::new(TunnelSigner { issuer, public })),
151    ))
152}
153
154fn proto_of(protocols: &[TunnelProtocol]) -> Proto {
155    match (
156        protocols.contains(&TunnelProtocol::Tcp),
157        protocols.contains(&TunnelProtocol::Udp),
158    ) {
159        (true, true) => Proto::Both,
160        (false, true) => Proto::Udp,
161        _ => Proto::Tcp,
162    }
163}
164
165#[derive(ToSchema, Serialize, Deserialize, Type, PartialEq, Eq, Hash, Clone, Copy)]
166#[serde(rename_all = "snake_case")]
167#[sqlx(type_name = "tunnel_protocol", rename_all = "SCREAMING_SNAKE_CASE")]
168pub enum TunnelProtocol {
169    Tcp,
170    Udp,
171}
172
173pub async fn snapshot(database: &Database) -> Result<Snapshot, crate::database::DatabaseError> {
174    let jwt_pubkey = signer(database).await?.public;
175
176    let mut transaction = database.write().begin().await?;
177    sqlx::query("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ")
178        .execute(&mut *transaction)
179        .await?;
180
181    let epoch = epoch(&mut *transaction).await?;
182    let configurations = EggConfiguration::all(&mut *transaction).await?;
183
184    let nodes = sqlx::query(
185        r#"
186        SELECT nodes.uuid, nodes.name, node_tunnels.host, node_tunnels.port, node_tunnels.cert_sha256
187        FROM node_tunnels
188        JOIN nodes ON nodes.uuid = node_tunnels.node_uuid
189        "#,
190    )
191    .fetch_all(&mut *transaction)
192    .await?
193    .into_iter()
194    .map(|row| {
195        Ok::<_, crate::database::DatabaseError>(NodeEntry {
196            uuid: row.try_get("uuid")?,
197            name: row.try_get::<String, _>("name")?,
198            host: row.try_get::<String, _>("host")?,
199            tunnel_port: row.try_get::<i32, _>("port")? as u16,
200            cert_sha256: row
201                .try_get::<Option<Vec<u8>>, _>("cert_sha256")?
202                .and_then(|bytes| <[u8; 32]>::try_from(bytes.as_slice()).ok())
203                .map(Hash32),
204        })
205    })
206    .try_collect_vec()?;
207
208    let mut ports: std::collections::HashMap<uuid::Uuid, Vec<PortSpec>> =
209        std::collections::HashMap::new();
210    for row in sqlx::query(
211        r#"
212        SELECT server_tunnel_ports.server_uuid, server_tunnel_ports.port, server_tunnel_ports.protocols
213        FROM server_tunnel_ports
214        ORDER BY server_tunnel_ports.port
215        "#,
216    )
217    .fetch_all(&mut *transaction)
218    .await?
219    {
220        ports
221            .entry(row.try_get("server_uuid")?)
222            .or_default()
223            .push(PortSpec {
224                port: row.try_get::<i32, _>("port")? as u16,
225                proto: proto_of(&row.try_get::<Vec<TunnelProtocol>, _>("protocols")?),
226            });
227    }
228
229    let mut servers = Vec::new();
230    for row in sqlx::query(
231        r#"
232        SELECT
233            server_tunnels.server_uuid,
234            server_tunnels.idx,
235            server_tunnels.name,
236            servers.node_uuid,
237            servers.uuid_short,
238            servers.egg_uuid,
239            nest_eggs.nest_uuid,
240            nodes.location_uuid
241        FROM server_tunnels
242        JOIN servers ON servers.uuid = server_tunnels.server_uuid
243        JOIN node_tunnels ON node_tunnels.node_uuid = servers.node_uuid
244        JOIN nest_eggs ON nest_eggs.uuid = servers.egg_uuid
245        JOIN nodes ON nodes.uuid = servers.node_uuid
246        "#,
247    )
248    .fetch_all(&mut *transaction)
249    .await?
250    {
251        let target = EggConfigurationTarget::map(&row)?;
252        if !EggConfiguration::merge(&configurations, &target).private_network_enabled() {
253            continue;
254        }
255
256        let uuid: uuid::Uuid = row.try_get("server_uuid")?;
257        servers.push(ServerEntry {
258            uuid,
259            idx: row.try_get::<i32, _>("idx")? as u16,
260            node_uuid: row.try_get("node_uuid")?,
261            name: row.try_get::<String, _>("name")?,
262            aliases: vec![alias_of(row.try_get::<i32, _>("uuid_short")?).into()],
263            container_ref: String::new(),
264            dial_addr: None,
265            ports: ports.remove(&uuid).unwrap_or_default(),
266        });
267    }
268
269    let on_mesh = servers
270        .iter()
271        .map(|server| server.uuid)
272        .collect::<HashSet<_>>();
273    let mut acls = Vec::new();
274    for row in sqlx::query(
275        r#"
276        SELECT server_tunnel_connections.src_server_uuid, server_tunnel_connections.dst_server_uuid
277        FROM server_tunnel_connections
278        ORDER BY server_tunnel_connections.src_server_uuid, server_tunnel_connections.dst_server_uuid
279        "#,
280    )
281    .fetch_all(&mut *transaction)
282    .await?
283    {
284        let acl = AclEntry {
285            src_server: row.try_get("src_server_uuid")?,
286            dst_server: row.try_get("dst_server_uuid")?,
287        };
288
289        if on_mesh.contains(&acl.src_server) && on_mesh.contains(&acl.dst_server) {
290            acls.push(acl);
291        }
292    }
293
294    transaction.commit().await?;
295
296    Ok(Snapshot {
297        epoch: epoch as u64,
298        jwt_pubkey,
299        nodes,
300        servers,
301        acls,
302    })
303}
304
305const POKE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
306
307/// Tells every node on the mesh to pull the snapshot now rather than on its next poll. Must be
308/// called *after* the commit that bumped the epoch, or a node can win the race and re-read the
309/// state it already has. Batched under one key, so several bumps in a request collapse to one
310/// round of pokes, and spawned so the batch loop is not held for the length of the requests.
311pub async fn poke_nodes(database: &Arc<Database>) {
312    database
313        .batch_action("poke_tunnel_nodes", uuid::Uuid::nil(), {
314            let database = Arc::clone(database);
315
316            async move {
317                tokio::spawn(async move {
318                    if let Err(err) = poke_nodes_now(&database).await {
319                        tracing::warn!("failed to poke the tunnel nodes: {:?}", err);
320                    }
321                });
322
323                Ok(())
324            }
325        })
326        .await;
327}
328
329async fn poke_nodes_now(database: &Database) -> Result<(), crate::database::DatabaseError> {
330    let rows = sqlx::query(
331        r#"
332        SELECT nodes.uuid, nodes.url, nodes.token
333        FROM node_tunnels
334        JOIN nodes ON nodes.uuid = node_tunnels.node_uuid
335        "#,
336    )
337    .fetch_all(database.read())
338    .await?;
339
340    let mut pokes = Vec::with_capacity(rows.len());
341    for row in rows {
342        let uuid: uuid::Uuid = row.try_get("uuid")?;
343        let client = wings_api::client::WingsClient::new(
344            row.try_get::<String, _>("url")?,
345            database
346                .decrypt(row.try_get::<Vec<u8>, _>("token")?)
347                .await?
348                .into(),
349        );
350
351        pokes.push(tokio::spawn(async move {
352            match tokio::time::timeout(POKE_TIMEOUT, client.post_tundra_sync()).await {
353                Ok(Ok(())) => {}
354                Ok(Err(err)) => tracing::debug!(node = %uuid, "failed to poke node: {:?}", err),
355                Err(_) => tracing::debug!(node = %uuid, "poking node timed out"),
356            }
357        }));
358    }
359
360    for poke in pokes {
361        let _ = poke.await;
362    }
363
364    Ok(())
365}