1#[cfg(feature = "direct")]
2use std::collections::HashMap;
3use std::sync::Arc;
4#[cfg(any(feature = "managed", feature = "direct"))]
5use std::time::Duration;
6
7#[cfg(any(feature = "managed", feature = "direct"))]
8use anyhow::Context;
9use arc_swap::ArcSwap;
10use iroh::Endpoint;
11#[cfg(any(feature = "managed", feature = "direct"))]
12use iroh::{SecretKey, endpoint::presets};
13#[cfg(any(feature = "managed", feature = "direct"))]
14use tunnet_common::TUNNEL_ALPN;
15#[cfg(feature = "direct")]
16use uuid::Uuid;
17
18use crate::acl::AclEngine;
19#[cfg(any(feature = "managed", feature = "direct"))]
20use crate::acl::SelfIdentity;
21#[cfg(feature = "managed")]
22use crate::acl_hook::AclHook;
23#[cfg(feature = "managed")]
24use crate::control::{SignedClient, basic_metadata};
25#[cfg(feature = "direct")]
26use crate::direct::{
27 AUTH_ALPN, AuthCache, DirectAuthHook, DocsBootstrap, DocsMembership, MembershipEntry,
28 derive_ipv4, firewall_to_policy, spawn_discovery,
29};
30use crate::identity::AgentIdentity;
31use crate::iroh_pool::ConnPool;
32use crate::routing::RoutingTable;
33#[cfg(feature = "send")]
34use crate::send::SendManager;
35#[cfg(feature = "serve")]
36use crate::serve::ServeManager;
37#[cfg(feature = "direct")]
38use crate::state::DirectState;
39#[cfg(feature = "managed")]
40use crate::state::{ManagedState, load_snapshot_cache, save_snapshot_cache};
41use crate::state::{PersistedState, StatePaths};
42#[cfg(any(feature = "managed", feature = "direct"))]
43use crate::stream::TUNNEL_STREAM_ALPN;
44#[cfg(feature = "managed")]
45use crate::sync::{
46 apply_membership, membership_for_network, spawn_poll_fallback, spawn_ws_processor,
47};
48#[cfg(feature = "tunnel")]
49use crate::tunnel::TunnelManager;
50
51pub type KillSshHook = Arc<dyn Fn(&str) + Send + Sync>;
53
54#[cfg(feature = "direct")]
56#[derive(Clone)]
57pub struct DirectNetworkRuntime {
58 pub docs: DocsMembership,
59 pub firewall: crate::direct::FirewallEngine,
60 pub spoof_tracker: crate::direct::SpoofTracker,
61 pub state: DirectState,
62}
63
64#[derive(Clone)]
65pub struct CoreNodeConfig {
66 pub hostname: String,
67 pub agent_version: &'static str,
68 pub poll_secs: u64,
69 pub advertise_datagram_alpn: bool,
70 pub advertise_recording_alpn: bool,
72 pub kind: &'static str, pub on_kill_ssh: Option<KillSshHook>,
75 pub enable_mdns: bool,
77 pub enable_gossip: bool,
79 pub keep_alive: bool,
81}
82
83impl Default for CoreNodeConfig {
84 fn default() -> Self {
85 Self {
86 hostname: "tunnet-node".into(),
87 agent_version: env!("CARGO_PKG_VERSION"),
88 poll_secs: 30,
89 advertise_datagram_alpn: false,
90 advertise_recording_alpn: false,
91 kind: "sdk",
92 on_kill_ssh: None,
93 enable_mdns: true,
94 enable_gossip: true,
95 keep_alive: true,
96 }
97 }
98}
99
100#[derive(Clone)]
101pub struct CoreNode {
102 pub identity: AgentIdentity,
103 pub persisted: PersistedState,
104 pub endpoint: Endpoint,
105 pub pool: ConnPool,
106 pub routes: RoutingTable,
107 pub acl: AclEngine,
108 pub version: Arc<ArcSwap<u64>>,
109 pub self_ipv4: std::net::Ipv4Addr,
110 pub paths: StatePaths,
111 #[cfg(feature = "serve")]
112 pub serves: ServeManager,
113 #[cfg(feature = "tunnel")]
114 pub tunnels: TunnelManager,
115 #[cfg(feature = "send")]
116 pub send: SendManager,
117 #[cfg(feature = "managed")]
119 pub signed: Option<SignedClient>,
120 #[cfg(feature = "direct")]
122 pub direct_auth: Option<AuthCache>,
123 #[cfg(feature = "direct")]
125 pub direct: HashMap<Uuid, DirectNetworkRuntime>,
126 pub gossip: Option<iroh_gossip::net::Gossip>,
128}
129
130impl CoreNode {
131 #[cfg(feature = "direct")]
133 pub fn docs(&self) -> Option<&DocsMembership> {
134 self.direct.values().next().map(|r| &r.docs)
135 }
136
137 #[cfg(feature = "direct")]
139 pub fn firewall(&self) -> Option<&crate::direct::FirewallEngine> {
140 self.direct.values().next().map(|r| &r.firewall)
141 }
142
143 #[cfg(feature = "direct")]
144 pub fn firewall_for(&self, network_id: Uuid) -> Option<&crate::direct::FirewallEngine> {
145 self.direct.get(&network_id).map(|r| &r.firewall)
146 }
147
148 #[cfg(feature = "direct")]
149 pub fn docs_for(&self, network_id: Uuid) -> Option<&DocsMembership> {
150 self.direct.get(&network_id).map(|r| &r.docs)
151 }
152
153 #[cfg(feature = "direct")]
154 pub fn spoof_tracker(&self) -> Option<&crate::direct::SpoofTracker> {
155 self.direct.values().next().map(|r| &r.spoof_tracker)
156 }
157
158 #[cfg(feature = "direct")]
159 pub fn spoof_for(&self, network_id: Uuid) -> Option<&crate::direct::SpoofTracker> {
160 self.direct.get(&network_id).map(|r| &r.spoof_tracker)
161 }
162
163 pub async fn bootstrap(
165 identity: AgentIdentity,
166 persisted: PersistedState,
167 paths: StatePaths,
168 cfg: CoreNodeConfig,
169 ) -> anyhow::Result<Self> {
170 match &persisted {
171 PersistedState::Managed(m) => {
172 #[cfg(feature = "managed")]
173 {
174 Self::bootstrap_managed(identity, persisted.clone(), m.clone(), paths, cfg)
175 .await
176 }
177 #[cfg(not(feature = "managed"))]
178 {
179 let _ = (&identity, &paths, &cfg, m);
180 anyhow::bail!("managed mode requires the `managed` feature");
181 }
182 }
183 PersistedState::Direct { networks } => {
184 if networks.is_empty() {
185 anyhow::bail!("no Direct networks joined");
186 }
187 #[cfg(feature = "direct")]
188 {
189 Self::bootstrap_direct(identity, persisted.clone(), paths, cfg).await
190 }
191 #[cfg(not(feature = "direct"))]
192 {
193 let _ = (identity, paths, cfg);
194 let _ = networks.len();
195 anyhow::bail!("direct mode requires the `direct` feature");
196 }
197 }
198 }
199 }
200
201 #[cfg(feature = "managed")]
202 async fn bootstrap_managed(
203 identity: AgentIdentity,
204 persisted: PersistedState,
205 managed: ManagedState,
206 paths: StatePaths,
207 cfg: CoreNodeConfig,
208 ) -> anyhow::Result<Self> {
209 let alpns = build_alpns(&cfg, false, cfg.enable_gossip);
210
211 let my_id_hex = identity.endpoint_id_hex();
212 let signed = SignedClient::new(
213 managed.control_url.clone(),
214 my_id_hex.clone(),
215 identity.signing_key.clone(),
216 )?;
217
218 let meta = basic_metadata(&cfg.hostname, cfg.agent_version, cfg.kind);
219 let snapshot = match signed
220 .register(&cfg.hostname, cfg.agent_version, Some(meta))
221 .await
222 {
223 Ok(s) => {
224 save_snapshot_cache(&paths, &s).ok();
225 s
226 }
227 Err(e) => {
228 tracing::warn!(?e, "register failed; falling back to cache");
229 load_snapshot_cache(&paths).context("no cache")?
230 }
231 };
232
233 let membership = membership_for_network(&snapshot, managed.network_id)?.clone();
234 let routes = RoutingTable::new();
235 let version = Arc::new(ArcSwap::from_pointee(snapshot.version));
236 let acl = AclEngine::new(
237 SelfIdentity {
238 endpoint_hex: my_id_hex.clone(),
239 ip: membership.assigned_ipv4,
240 tags: membership.self_tags.clone(),
241 network: managed.network_name.clone(),
242 },
243 routes.clone(),
244 membership.policy.clone(),
245 );
246 apply_membership(
247 &membership,
248 &routes,
249 &acl,
250 &version,
251 snapshot.version,
252 &my_id_hex,
253 &cfg.hostname,
254 Some(paths.dir.as_path()),
255 );
256
257 let secret = SecretKey::from_bytes(&identity.secret_bytes);
258 let endpoint = Endpoint::builder(presets::N0)
259 .secret_key(secret)
260 .alpns(alpns)
261 .hooks(AclHook::new(acl.clone()))
262 .bind()
263 .await
264 .context("bind iroh endpoint")?;
265
266 debug_assert_eq!(format!("{}", endpoint.id()), my_id_hex);
267
268 match tokio::time::timeout(Duration::from_secs(10), endpoint.online()).await {
269 Ok(()) => tracing::info!("endpoint online"),
270 Err(_) => tracing::warn!("timed out waiting for relay; continuing"),
271 }
272
273 #[cfg(feature = "serve")]
274 let serves = ServeManager::new(membership.assigned_ipv4, routes.clone());
275 let pool = ConnPool::new(endpoint.clone(), TUNNEL_STREAM_ALPN);
276 #[cfg(feature = "tunnel")]
277 let tunnels = TunnelManager::new(pool.clone());
278 #[cfg(feature = "send")]
279 let send = SendManager::open(
280 paths.dir.join("blobs"),
281 pool.clone(),
282 routes.clone(),
283 acl.clone(),
284 my_id_hex.clone(),
285 )
286 .await
287 .context("open send manager")?;
288
289 let ws = crate::ws_client::spawn(
290 managed.control_url.clone(),
291 my_id_hex.clone(),
292 identity.signing_key.clone(),
293 );
294 #[cfg(feature = "serve")]
295 serves.set_client_tx(ws.tx.clone());
296 #[cfg(feature = "send")]
297 send.set_client_tx(ws.tx.clone());
298 spawn_ws_processor(
299 ws,
300 routes.clone(),
301 acl.clone(),
302 version.clone(),
303 paths.clone_paths(),
304 managed.network_id,
305 my_id_hex.clone(),
306 cfg.hostname.clone(),
307 cfg.agent_version,
308 Some(signed.clone()),
309 #[cfg(feature = "serve")]
310 Some(serves.clone()),
311 #[cfg(feature = "tunnel")]
312 Some(tunnels.clone()),
313 #[cfg(feature = "send")]
314 Some(send.clone()),
315 cfg.on_kill_ssh.clone(),
316 );
317 spawn_poll_fallback(
318 signed.clone(),
319 version.clone(),
320 cfg.poll_secs,
321 routes.clone(),
322 acl.clone(),
323 managed.network_id,
324 my_id_hex.clone(),
325 cfg.hostname.clone(),
326 Some(paths.dir.clone()),
327 );
328
329 let _ = persisted;
330 pool.set_keep_alive(cfg.keep_alive);
331
332 let gossip = if cfg.enable_gossip {
333 tracing::info!("Managed shared Gossip enabled");
334 Some(iroh_gossip::net::Gossip::builder().spawn(endpoint.clone()))
335 } else {
336 None
337 };
338
339 Ok(Self {
340 identity,
341 persisted: PersistedState::Managed(managed),
342 endpoint,
343 pool,
344 routes,
345 acl,
346 version,
347 self_ipv4: membership.assigned_ipv4,
348 paths,
349 #[cfg(feature = "serve")]
350 serves,
351 #[cfg(feature = "tunnel")]
352 tunnels,
353 #[cfg(feature = "send")]
354 send,
355 signed: Some(signed),
356 #[cfg(feature = "direct")]
357 direct_auth: None,
358 #[cfg(feature = "direct")]
359 direct: HashMap::new(),
360 gossip,
361 })
362 }
363
364 #[cfg(feature = "direct")]
365 async fn bootstrap_direct(
366 identity: AgentIdentity,
367 persisted: PersistedState,
368 paths: StatePaths,
369 cfg: CoreNodeConfig,
370 ) -> anyhow::Result<Self> {
371 let networks = persisted.direct_networks().to_vec();
372 if networks.is_empty() {
373 anyhow::bail!("no Direct networks joined");
374 }
375
376 let alpns = build_alpns(&cfg, true, true);
377 let my_id_hex = identity.endpoint_id_hex();
378 let primary = &networks[0];
379 let self_ipv4 = if primary.assigned_ipv4.is_unspecified() {
380 derive_ipv4(&my_id_hex, primary.collision_index)
381 } else {
382 primary.assigned_ipv4
383 };
384
385 let routes = RoutingTable::new();
386 let version = Arc::new(ArcSwap::from_pointee(1u64));
387 let fw0 = crate::agent_config::load_firewall_for(&paths, &primary.network_name);
389 let policy0 = firewall_to_policy(&fw0, &my_id_hex, self_ipv4);
390 let acl = AclEngine::new(
391 SelfIdentity {
392 endpoint_hex: my_id_hex.clone(),
393 ip: self_ipv4,
394 tags: vec![],
395 network: primary.network_name.clone(),
396 },
397 routes.clone(),
398 policy0,
399 );
400
401 let auth = AuthCache::new();
402 for d in &networks {
403 auth.insert(my_id_hex.clone(), d.network_id);
404 }
405
406 let secret = SecretKey::from_bytes(&identity.secret_bytes);
407 let builder = Endpoint::builder(presets::N0)
408 .secret_key(secret)
409 .alpns(alpns)
410 .hooks(DirectAuthHook::new(acl.clone(), auth.clone()));
411 let builder = crate::direct::apply_mdns(builder, cfg.enable_mdns);
412 let endpoint = builder
413 .bind()
414 .await
415 .context("bind iroh endpoint (direct)")?;
416
417 match tokio::time::timeout(Duration::from_secs(10), endpoint.online()).await {
418 Ok(()) => tracing::info!("direct endpoint online"),
419 Err(_) => tracing::warn!("timed out waiting for relay; continuing"),
420 }
421
422 #[cfg(feature = "serve")]
423 let serves = ServeManager::new(self_ipv4, routes.clone());
424 let pool = ConnPool::new(endpoint.clone(), TUNNEL_STREAM_ALPN);
425 pool.set_keep_alive(cfg.keep_alive);
426 #[cfg(feature = "tunnel")]
427 let tunnels = TunnelManager::new(pool.clone());
428
429 let blobs_dir = paths.dir.join("blobs");
430 std::fs::create_dir_all(&blobs_dir)?;
431 let blobs = iroh_blobs::store::fs::FsStore::load(&blobs_dir)
432 .await
433 .map_err(|e| anyhow::anyhow!("open shared FsStore: {e}"))?;
434
435 #[cfg(feature = "send")]
436 let send = SendManager::from_store(
437 blobs.clone(),
438 pool.clone(),
439 routes.clone(),
440 acl.clone(),
441 my_id_hex.clone(),
442 )
443 .await
444 .context("open send manager")?;
445
446 let mut direct_runtimes = HashMap::new();
447 let mut updated_networks = Vec::new();
448 let mut any_secret_update = false;
449
450 for (join_index, mut direct) in networks.into_iter().enumerate() {
451 let net_ipv4 = if direct.assigned_ipv4.is_unspecified() {
452 derive_ipv4(&my_id_hex, direct.collision_index)
453 } else {
454 direct.assigned_ipv4
455 };
456
457 let fw_cfg = crate::agent_config::load_firewall_for(&paths, &direct.network_name);
458 let policy = firewall_to_policy(&fw_cfg, &my_id_hex, net_ipv4);
459 let firewall =
460 crate::direct::FirewallEngine::from_config(&fw_cfg, net_ipv4, my_id_hex.clone());
461 let spoof_tracker = crate::direct::SpoofTracker::new();
462
463 let self_entry = MembershipEntry {
464 endpoint_id: my_id_hex.clone(),
465 hostname: direct.hostname.clone(),
466 ipv4: net_ipv4,
467 collision_index: direct.collision_index,
468 tags: vec![],
469 joined_at: chrono::Utc::now(),
470 coordinator: direct.coordinator,
471 status: "online".into(),
472 ssh_host_key: None,
473 };
474
475 let (docs, new_ticket, new_ns) = DocsMembership::bootstrap(DocsBootstrap {
476 endpoint: endpoint.clone(),
477 paths: &paths,
478 direct: &direct,
479 self_endpoint_id: &my_id_hex,
480 self_entry,
481 blobs: blobs.clone(),
482 routes: routes.clone(),
483 acl: acl.clone(),
484 auth: auth.clone(),
485 policy,
486 firewall: Some(firewall.clone()),
487 dns: crate::load_dns(&paths),
488 join_index: join_index as u64,
489 })
490 .await
491 .with_context(|| {
492 format!(
493 "bootstrap iroh-docs membership for '{}'",
494 direct.network_name
495 )
496 })?;
497
498 if new_ticket.is_some() || new_ns.is_some() {
499 any_secret_update = true;
500 if let Some(t) = new_ticket {
501 direct.doc_ticket = Some(t);
502 }
503 if let Some(ns) = new_ns {
504 direct.namespace_id = Some(ns);
505 }
506 }
507 direct.assigned_ipv4 = net_ipv4;
508
509 let mut seeds = Vec::new();
510 if let Some(coord) = &direct.coordinator_endpoint_id {
511 seeds.push(coord.clone());
512 }
513 for m in docs.snapshot_members() {
514 if m.endpoint_id != my_id_hex {
515 seeds.push(m.endpoint_id);
516 }
517 }
518 let _discovery = spawn_discovery(direct.topic_hash.clone(), my_id_hex.clone(), seeds);
519
520 direct_runtimes.insert(
521 direct.network_id,
522 DirectNetworkRuntime {
523 docs,
524 firewall,
525 spoof_tracker,
526 state: direct.clone(),
527 },
528 );
529 updated_networks.push(direct);
530 }
531
532 if any_secret_update {
533 crate::secret_store::persist_agent(
534 &paths,
535 &identity,
536 PersistedState::Direct {
537 networks: updated_networks.clone(),
538 },
539 crate::secret_store::SealPolicy::from_env_and_flag(false),
540 )?;
541 }
542
543 let contact = crate::direct::contact_id_from_endpoint(&endpoint.id());
544 tracing::info!(%contact, networks = direct_runtimes.len(), "direct contact id");
545
546 let _ = cfg;
547 Ok(Self {
548 identity,
549 persisted: PersistedState::Direct {
550 networks: updated_networks,
551 },
552 endpoint,
553 pool,
554 routes,
555 acl,
556 version,
557 self_ipv4,
558 paths,
559 #[cfg(feature = "serve")]
560 serves,
561 #[cfg(feature = "tunnel")]
562 tunnels,
563 #[cfg(feature = "send")]
564 send,
565 #[cfg(feature = "managed")]
566 signed: None,
567 direct_auth: Some(auth),
568 direct: direct_runtimes,
569 gossip: None,
571 })
572 }
573
574 pub fn shared_gossip(&self) -> Option<iroh_gossip::net::Gossip> {
577 if let Some(g) = &self.gossip {
578 return Some(g.clone());
579 }
580 #[cfg(feature = "direct")]
581 {
582 self.docs().map(|d| d.gossip())
583 }
584 #[cfg(not(feature = "direct"))]
585 {
586 None
587 }
588 }
589
590 pub fn endpoint_id_hex(&self) -> String {
591 self.identity.endpoint_id_hex()
592 }
593
594 #[cfg(feature = "managed")]
595 pub fn require_signed(&self) -> anyhow::Result<&SignedClient> {
596 self.signed.as_ref().context(
597 "this operation requires Managed mode (control plane client unavailable in Direct)",
598 )
599 }
600
601 pub async fn shutdown(&self) {
602 self.endpoint.close().await;
603 }
604}
605
606#[cfg(any(feature = "managed", feature = "direct"))]
607fn build_alpns(cfg: &CoreNodeConfig, direct: bool, enable_gossip: bool) -> Vec<Vec<u8>> {
608 let mut alpns: Vec<Vec<u8>> = vec![TUNNEL_STREAM_ALPN.to_vec()];
609 if cfg.advertise_datagram_alpn {
610 alpns.push(TUNNEL_ALPN.to_vec());
611 }
612 if cfg.advertise_recording_alpn {
613 #[cfg(feature = "recording")]
614 alpns.push(tunnet_common::RECORDING_ALPN.to_vec());
615 #[cfg(not(feature = "recording"))]
616 tracing::warn!("advertise_recording_alpn set but `recording` feature disabled");
617 }
618 #[cfg(feature = "send")]
619 {
620 alpns.push(tunnet_common::SEND_ALPN.to_vec());
621 alpns.push(iroh_blobs::ALPN.to_vec());
622 }
623 if direct {
624 #[cfg(feature = "direct")]
625 {
626 alpns.push(AUTH_ALPN.to_vec());
627 alpns.push(iroh_gossip::ALPN.to_vec());
628 alpns.push(iroh_docs::ALPN.to_vec());
629 }
630 } else if enable_gossip {
631 alpns.push(iroh_gossip::ALPN.to_vec());
632 }
633 alpns
634}