This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / knot2 / crates / knot-server / src / main.rs
30 kB 790 lines
1mod allocator; 2 3#[global_allocator] 4static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; 5 6#[allow(non_upper_case_globals)] 7#[unsafe(export_name = "_rjem_malloc_conf")] 8pub static malloc_conf: &[u8] = 9 b"background_thread:true,retain:false,dirty_decay_ms:0,muzzy_decay_ms:0\0"; 10 11use std::collections::BTreeSet; 12use std::num::{NonZeroU32, NonZeroU64}; 13use std::path::{Path, PathBuf}; 14use std::sync::Arc; 15use std::time::Duration; 16 17use tokio_util::sync::CancellationToken; 18 19use anyhow::Context; 20use axum::Json; 21use axum::response::Html; 22use axum::routing::get; 23use base64::Engine; 24use knot_atproto::Atproto; 25use knot_config::HomepageSource; 26use knot_index::Index; 27use knot_runtime::{Clock, HttpTransport, OsEntropy, ReqwestHttp, SystemClock}; 28use knot_secrets::{MasterKey, SealedStore}; 29use knot_types::{ActorId, AuthorName, BranchName, CiLogsAddr, Email, KnotHostname, ObjectCount}; 30use knot_xrpc::XrpcState; 31use tower_http::services::ServeFile; 32 33const MAINTENANCE_SHUTDOWN_DRAIN: Duration = Duration::from_secs(30); 34const EDGE_SHUTDOWN_DRAIN: Duration = Duration::from_secs(40); 35 36const DEFAULT_HOMEPAGE: &str = include_str!("homepage.html"); 37 38struct IndexRepos(Arc<Index>); 39 40impl knot_maintenance::RepoSource for IndexRepos { 41 fn repos(&self) -> Vec<knot_types::RepoDid> { 42 self.0.hosted_repos() 43 } 44 45 fn ready_repos(&self) -> Option<Vec<knot_types::RepoDid>> { 46 match self.0.coverage().registry { 47 knot_index::Coverage::Ready => Some(self.0.hosted_repos()), 48 knot_index::Coverage::Warming => None, 49 } 50 } 51} 52 53struct AtprotoHandleResolver<H, C> { 54 atproto: Arc<Atproto<H, C>>, 55} 56 57impl<H: HttpTransport, C: Clock> knot_pack::HandleResolver for AtprotoHandleResolver<H, C> { 58 fn resolve( 59 &self, 60 handle: knot_types::Handle, 61 ) -> std::pin::Pin< 62 Box<dyn std::future::Future<Output = Option<knot_types::AccountDid>> + Send + '_>, 63 > { 64 Box::pin(async move { self.atproto.resolve_handle_to_did(&handle).await.ok() }) 65 } 66} 67 68fn init_tracing() { 69 let filter = tracing_subscriber::EnvFilter::try_from_default_env() 70 .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")); 71 tracing_subscriber::fmt() 72 .with_env_filter(filter) 73 .with_writer(std::io::stderr) 74 .init(); 75} 76 77const VALIDATE_CONFIG_ONLY: &str = "--config-only"; 78 79enum ValidateScope { 80 ConfigOnly, 81 Environment, 82} 83 84impl ValidateScope { 85 fn verify(self, config: &knot_config::Validated) -> anyhow::Result<()> { 86 match self { 87 Self::ConfigOnly => Ok(()), 88 Self::Environment => config 89 .verify_environment() 90 .context("verify runtime environment"), 91 } 92 } 93} 94 95fn validate(args: impl Iterator<Item = String>) -> anyhow::Result<()> { 96 let (flags, paths): (Vec<String>, Vec<String>) = args.partition(|arg| arg.starts_with("--")); 97 flags 98 .iter() 99 .find(|flag| flag.as_str() != VALIDATE_CONFIG_ONLY) 100 .map_or(Ok(()), |unknown| { 101 Err(anyhow::anyhow!( 102 "unrecognized flag {unknown}, expected {VALIDATE_CONFIG_ONLY}" 103 )) 104 })?; 105 let scope = match flags.is_empty() { 106 true => ValidateScope::Environment, 107 false => ValidateScope::ConfigOnly, 108 }; 109 let path = match paths.as_slice() { 110 [] => None, 111 [path] => Some(Path::new(path)), 112 extra => anyhow::bail!( 113 "expected at most one configuration path, got {}", 114 extra.len() 115 ), 116 }; 117 let config = knot_config::load(path).context("load configuration")?; 118 scope.verify(&config)?; 119 println!("configuration is valid"); 120 Ok(()) 121} 122 123fn subcommand(name: &str) -> Option<anyhow::Result<()>> { 124 match name { 125 "config-template" => { 126 print!("{}", knot_config::template()); 127 Some(Ok(())) 128 } 129 "validate" => Some(validate(std::env::args().skip(2))), 130 _ => None, 131 } 132} 133 134#[tokio::main] 135async fn main() -> anyhow::Result<()> { 136 #[cfg(target_os = "linux")] 137 rustix::process::set_dumpable_behavior(rustix::process::DumpableBehavior::NotDumpable) 138 .context("disable core dumps and ptrace attachment")?; 139 140 if let Some(result) = std::env::args().nth(1).as_deref().and_then(subcommand) { 141 return result; 142 } 143 144 init_tracing(); 145 146 tracing::info!("!"); 147 tracing::info!("!"); 148 tracing::info!("!"); 149 tracing::info!("> If knot1 was so good then why isn't there a... ( ˶°ㅁ°)"); 150 tracing::info!("..."); 151 tracing::info!("Welcome to knot2!"); 152 tracing::info!("This code was made with love."); 153 tracing::info!("Hachapuri is sho tasty, definitely worth a try. Better than pizza tbh."); 154 tracing::info!("!"); 155 tracing::info!("!"); 156 tracing::info!("!"); 157 158 let config_path = std::env::args().nth(1).map(PathBuf::from); 159 let config = knot_config::load(config_path.as_deref()).context("load configuration")?; 160 config 161 .verify_environment() 162 .context("verify runtime environment")?; 163 164 let resources = knot_resource::init(knot_resource::Ceilings { 165 max_threads: match config.resources.max_threads { 166 0 => None, 167 n => Some(knot_resource::ThreadCount::new(n as usize)), 168 }, 169 max_memory: match config.resources.max_memory_bytes { 170 0 => None, 171 n => Some(knot_resource::MemoryBudget::new(n)), 172 }, 173 }); 174 tracing::info!( 175 threads = resources.threads.get(), 176 memory_bytes = resources.memory.map(knot_resource::MemoryBudget::get), 177 memory_source = ?resources.memory_source, 178 memory_high_bytes = resources 179 .memory_high_bytes 180 .map(knot_resource::MemoryHighBytes::get), 181 "resource governor initialized" 182 ); 183 allocator::install(); 184 185 // I made the hostname stay aa string in the config, 186 // so that confique can layer env-over-file. 187 // Here's where it becomes a real type, 188 // and the DID + the service url stack on this. 189 let hostname = KnotHostname::new(config.server.hostname.clone()) 190 .context("server.hostname isn't a valid knot hostname")?; 191 let knot_did = hostname.knot_did(); 192 let object_format = config.object_format().context("parse git.object_format")?; 193 let default_branch = 194 BranchName::new(config.repo.default_branch.as_str()).context("parse default branch")?; 195 let layout = knot_git::Layout::new(&config.repo.scan_path) 196 .with_default_branch(default_branch) 197 .with_object_format(object_format) 198 .reserving_meta(&knot_did) 199 .context("reserve meta-repo path")?; 200 201 let swept = knot_pack::sweep_incoming(&config.repo.scan_path); 202 if swept > 0 { 203 tracing::info!(swept, "swept abandoned receive staging directories"); 204 } 205 206 layout 207 .bootstrap_meta(&knot_did) 208 .context("bootstrap meta-repo")?; 209 let meta_path = layout 210 .meta_path(&knot_did) 211 .context("resolve meta-repo path")?; 212 213 let index = Arc::new(Index::new(meta_path.clone(), layout.clone())); 214 index.rebuild().context("rebuild index from meta-repo")?; 215 tracing::info!(coverage = ?index.coverage(), "index ready"); 216 217 let warm = Arc::clone(&index); 218 tokio::task::spawn_blocking(move || warm.warm_collaborators()); 219 220 let http = ReqwestHttp::new(config.http_limits()).context("build outbound HTTP client")?; 221 let git_http: Arc<dyn knot_runtime::HttpTransport> = Arc::new( 222 ReqwestHttp::new(config.fork_http_limits()).context("build outbound git fetch client")?, 223 ); 224 let atproto = Arc::new(Atproto::new( 225 http, 226 SystemClock, 227 knot_did.clone(), 228 knot_atproto::PlcDirectory::new(config.atproto.plc_directory.clone()) 229 .context("atproto.plc_directory isn't a valid PLC base URL")?, 230 )); 231 let admins: BTreeSet<_> = config.server.admins.iter().cloned().collect(); 232 let admission = config.acl.admission; 233 let service_owner = config 234 .server 235 .admins 236 .first() 237 .cloned() 238 .context("at least one admin is configured")?; 239 240 let master_key = MasterKey::new( 241 base64::engine::general_purpose::STANDARD 242 .decode( 243 std::env::var(&config.secrets.master_key_env) 244 .context("read master key from environment")? 245 .trim(), 246 ) 247 .context("decode master key as base64")?, 248 ) 249 .context("master key from environment")?; 250 let secrets = Arc::new( 251 SealedStore::open( 252 &config.secrets.sealed_key_file, 253 &master_key, 254 Box::new(OsEntropy), 255 ) 256 .context("open sealed key store")?, 257 ); 258 let knot_signing_key = secrets 259 .ensure(&knot_did) 260 .context("seal knot's own signing key")?; 261 let knot_actor = ActorId::from_secp256k1(knot_signing_key.as_bytes()); 262 let appview_endpoint = config.server.appview_endpoint.clone(); 263 let knot_service_url = knot_types::KnotServiceUrl::new(format!("https://{hostname}")) 264 .context("server.hostname doesn't form a valid knot service URL")?; 265 let did_document = 266 knot_atproto::knot_did_document(&knot_did, &knot_signing_key, &knot_service_url); 267 268 let http_addr = config.server.listen_addr; 269 let listen_limits = knot_edge::ListenLimits::new( 270 knot_edge::HeaderTimeout::from_millis( 271 NonZeroU64::new(config.server.listen_header_timeout_ms) 272 .context("server.listen_header_timeout_ms must be greater than zero")?, 273 ), 274 knot_edge::IdleTimeout::from_millis( 275 NonZeroU64::new(config.server.listen_idle_timeout_ms) 276 .context("server.listen_idle_timeout_ms must be greater than zero")?, 277 ), 278 NonZeroU32::new(config.server.listen_max_connections) 279 .context("server.listen_max_connections must be greater than zero")?, 280 ); 281 // A header name that doesn't parse will never match, 282 // `effective_peer` falls back to socket, 283 // and every request in the world shares 284 // the proxy's address + its one ratelimit bucket. 285 // So... better to refuse to start. 286 let trusted_proxy_header = config 287 .xrpc 288 .trusted_proxy_header 289 .as_deref() 290 .map(|header| axum::http::HeaderName::from_bytes(header.as_bytes())) 291 .transpose() 292 .context("xrpc.trusted_proxy_header isn't a valid HTTP header name")?; 293 let edge_guards = knot_edge::EdgeGuards::new( 294 knot_edge::RequestsPerSecond::new( 295 NonZeroU32::new(config.server.listen_rate_limit_per_second) 296 .context("server.listen_rate_limit_per_second must be greater than zero")?, 297 ), 298 knot_edge::BurstSize::new( 299 NonZeroU32::new(config.server.listen_rate_limit_burst) 300 .context("server.listen_rate_limit_burst must be greater than zero")?, 301 ), 302 knot_edge::MaxInflightRequests::new( 303 NonZeroU32::new(config.server.listen_max_inflight_requests) 304 .context("server.listen_max_inflight_requests must be greater than zero")?, 305 ), 306 knot_edge::RequestTimeout::from_millis( 307 NonZeroU64::new(config.server.listen_request_timeout_ms) 308 .context("server.listen_request_timeout_ms must be greater than zero")?, 309 ), 310 knot_edge::BodyInactivityTimeout::from_millis( 311 NonZeroU64::new(config.server.listen_body_timeout_ms) 312 .context("server.listen_body_timeout_ms must be greater than zero")?, 313 ), 314 knot_edge::WriteRequestTimeout::from_millis( 315 NonZeroU64::new(config.server.listen_write_request_timeout_ms) 316 .context("server.listen_write_request_timeout_ms must be greater than zero")?, 317 ), 318 trusted_proxy_header.clone(), 319 ); 320 let tls_setup = build_tls_setup(&config, &hostname).context("assemble TLS configuration")?; 321 if config.tls.http3 && tls_setup.is_none() { 322 tracing::warn!( 323 "tls.http3 is set without any TLS certificate, so HTTP/3 won't start. Configure a static cert or ACME to serve h3." 324 ); 325 } 326 if tls_setup.is_none() && config.xrpc.trusted_proxy_header.is_none() { 327 tracing::warn!( 328 "running plaintext behind a reverse proxy without xrpc.trusted_proxy_header. Per-IP rate limiting will key on the proxy socket address, throttling all clients as one. Set xrpc.trusted_proxy_header to the header your proxy appends." 329 ); 330 } 331 if config.tls.acme_enabled && http_addr.port() != 443 { 332 tracing::warn!( 333 listen_port = http_addr.port(), 334 "ACME validation over TLS-ALPN-01 needs the certificate authority to reach this host on TCP 443. Map 443 to the listen port if it differs." 335 ); 336 } 337 if config.tls.acme_enabled && config.tls.acme_staging { 338 tracing::warn!( 339 "ACME is using the Let's Encrypt staging directory. Its certificates aren't browser-trusted. Unset tls.acme_staging for real certificates." 340 ); 341 } 342 let ssh_addr = config.server.ssh_listen_addr; 343 let ssh_max_pack_bytes = config.server.ssh_max_pack_bytes as usize; 344 let pack_limits = knot_pack::PackLimits { 345 max_objects: ObjectCount::from(config.pack.max_objects), 346 max_total_bytes: knot_pack::MaxTotalBytes::new(config.pack.max_total_bytes), 347 ..knot_pack::PackLimits::default() 348 }; 349 knot_pack::init_selection_limits(knot_pack::SelectionLimits { 350 max_objects: ObjectCount::from(config.pack.selection_max_objects), 351 time_budget: Duration::from_secs(config.pack.selection_time_budget_secs), 352 }); 353 let host_key = knot_ssh::load_or_create_host_key(&config.server.ssh_host_key_file) 354 .context("load or create SSH host key")?; 355 356 let xrpc_limits = knot_xrpc::LimitConfig { 357 rate: Some(knot_xrpc::RateLimit { 358 burst: knot_xrpc::Burst::new(config.xrpc.preauth_burst), 359 refill: knot_xrpc::RefillMicros::new( 360 config.xrpc.preauth_refill_ms.saturating_mul(1_000), 361 ), 362 }), 363 per_peer_inflight: Some(knot_xrpc::PerPeerInflight::new( 364 config.xrpc.per_peer_inflight as usize, 365 )), 366 global_inflight: Some(knot_xrpc::GlobalInflight::new( 367 config.xrpc.global_inflight as usize, 368 )), 369 }; 370 let byte_limits = knot_xrpc::ByteLimits { 371 body: knot_xrpc::BodyLimit::new(config.xrpc.max_body_bytes as usize), 372 patch: knot_xrpc::PatchLimit::new(config.xrpc.max_patch_bytes as usize), 373 patch_decompressed: knot_xrpc::PatchDecompressedLimit::new( 374 config.xrpc.max_patch_decompressed_bytes, 375 ), 376 response: knot_xrpc::ResponseLimit::new(config.xrpc.max_response_bytes as usize), 377 archive: knot_xrpc::ArchiveLimit::new(config.xrpc.max_archive_bytes), 378 fork_pack: knot_xrpc::ForkPackLimit::new(config.xrpc.fork_max_pack_bytes), 379 pack: knot_xrpc::MaxWireBytes::new(ssh_max_pack_bytes), 380 }; 381 let budgets = knot_xrpc::Budgets { 382 tree_last_commit: knot_xrpc::TreeReadBudget::new(knot_xrpc::ReadBudget::Within( 383 Duration::from_millis(config.xrpc.tree_last_commit_budget_ms), 384 )), 385 blob_last_commit: knot_xrpc::BlobReadBudget::new(knot_xrpc::ReadBudget::Within( 386 Duration::from_millis(config.xrpc.blob_last_commit_budget_ms), 387 )), 388 languages: knot_xrpc::LanguagesReadBudget::new(knot_xrpc::ReadBudget::Within( 389 Duration::from_millis(config.xrpc.languages_budget_ms), 390 )), 391 languages_push: knot_xrpc::LanguagesPushBudget::new(Duration::from_millis( 392 config.xrpc.languages_push_budget_ms, 393 )), 394 }; 395 let committer = knot_xrpc::Committer { 396 name: AuthorName::new(config.git.user_name.clone()), 397 email: Email::new(config.git.user_email.clone()), 398 }; 399 let reservations = Arc::new(knot_xrpc::Reservations::new( 400 knot_xrpc::ReservationTtl::new(config.xrpc.reservation_ttl_secs as i64), 401 knot_xrpc::PerActorQuota::new(config.xrpc.per_actor_reservations as usize), 402 knot_xrpc::GlobalQuota::new(config.xrpc.max_pending_reservations as usize), 403 )); 404 let replay_bounds = knot_events::ReplayBounds::new( 405 knot_events::ReplayEvents::new(config.xrpc.events_replay_buffer as usize) 406 .context("xrpc.events_replay_buffer must be greater than zero")?, 407 knot_events::ReplayBytes::new(config.xrpc.events_replay_bytes as usize) 408 .context("xrpc.events_replay_bytes must be greater than zero")?, 409 ); 410 let events = Arc::new(knot_events::EventLog::new(SystemClock, replay_bounds)); 411 let subscriber_gate = Arc::new(knot_events::SubscriberGate::new( 412 knot_events::GlobalSubscriberLimit::new(config.xrpc.events_max_subscribers as usize), 413 knot_events::PerPeerSubscriberLimit::new(config.xrpc.events_max_per_peer as usize), 414 )); 415 416 let maintenance_enabled = config.maintenance.enabled; 417 let maintenance_options = knot_maintenance::Options::from_config(&config.maintenance); 418 let maintenance_interval = Duration::from_secs(config.maintenance.interval_secs); 419 let maintenance_large_push = 420 knot_maintenance::PushBytes::new(config.maintenance.large_push_bytes); 421 422 let lfs_handle = config 423 .lfs 424 .store_path 425 .as_ref() 426 .map(|path| { 427 knot_lfs::LfsHandle::open( 428 knot_lfs::LfsStorePath::new(path), 429 knot_lfs::LfsSize::new(config.lfs.max_object_bytes), 430 knot_lfs::FreeSpaceFloor::new(config.lfs.free_space_floor_bytes), 431 ) 432 .inspect(|_| { 433 tracing::info!(store = %path.display(), "git-lfs capability enabled"); 434 }) 435 }) 436 .transpose() 437 .context("open LFS object store")?; 438 let lfs_max_ssh_transfers = config.lfs.max_ssh_transfers as usize; 439 let lfs_max_http_downloads = config.lfs.max_http_downloads as usize; 440 let lfs_gc_grace = knot_maintenance::lfs_grace( 441 knot_maintenance::GcGrace::from_secs(config.lfs.gc_grace_secs), 442 knot_maintenance::ReflogRetention::from_secs(config.maintenance.reflog_expire_secs), 443 ); 444 let lfs_gc_interval = 445 knot_maintenance::SweepInterval::new(Duration::from_secs(config.lfs.gc_interval_secs)); 446 if lfs_handle.is_some() && !maintenance_enabled { 447 tracing::warn!( 448 "LFS store configured but maintenance is disabled. Unreferenced LFS objects will accumulate with no garbage collection or orphan sweep." 449 ); 450 } 451 452 let pack_cache_config = knot_pack::CacheConfig { 453 enabled: config.pack_cache.enabled, 454 ttl: Duration::from_secs(config.pack_cache.ttl_secs), 455 max_entry_bytes: knot_pack::MaxEntryBytes::new(config.pack_cache.max_entry_bytes as usize), 456 max_total_bytes: knot_pack::MaxCacheBytes::new(knot_resource::pack_cache_bytes( 457 config.pack_cache.max_total_bytes, 458 ) as usize), 459 }; 460 461 let ci_logs = config 462 .ci 463 .logs_addr 464 .as_deref() 465 .map(CiLogsAddr::new) 466 .transpose() 467 .context("ci.logs_addr must be host:port")?; 468 469 let homepage = config.homepage.source(); 470 let catalog = Arc::new( 471 knot_messages::Catalog::parse(&config.messages).context("parse message templates")?, 472 ); 473 let legacy_admin = legacy_admin_secret(&config); 474 475 knot_config::init(config); 476 477 let (maintenance_handle, maintenance_shutdown, maintenance_task) = if maintenance_enabled { 478 let (scheduler, handle) = knot_maintenance::Scheduler::new( 479 layout.clone(), 480 Arc::new(IndexRepos(Arc::clone(&index))), 481 SystemClock, 482 maintenance_options, 483 maintenance_interval, 484 maintenance_large_push, 485 ); 486 let scheduler = match &lfs_handle { 487 Some(lfs) => { 488 scheduler.with_lfs_gc(Arc::clone(&lfs.store), lfs_gc_grace, lfs_gc_interval) 489 } 490 None => scheduler, 491 }; 492 let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false); 493 let task = tokio::spawn(scheduler.run(shutdown_rx)); 494 tracing::info!( 495 interval_secs = maintenance_interval.as_secs(), 496 "maintenance scheduler running" 497 ); 498 (handle, Some(shutdown_tx), Some(task)) 499 } else { 500 (knot_maintenance::MaintenanceHandle::disabled(), None, None) 501 }; 502 503 let slots = knot_resource::Slots::for_machine(); 504 505 let ssh_base = knot_ssh::SshState::new( 506 layout.clone(), 507 Arc::clone(&index), 508 Arc::clone(&atproto), 509 knot_actor, 510 Arc::clone(&events), 511 hostname.clone(), 512 appview_endpoint.clone(), 513 admins.clone(), 514 admission, 515 byte_limits.pack, 516 budgets.languages_push, 517 ci_logs.clone(), 518 ) 519 .with_maintenance(maintenance_handle.clone()) 520 .with_limits(pack_limits) 521 .with_slots(slots.clone()) 522 .with_catalog(Arc::clone(&catalog)); 523 let ssh_state = Arc::new(match &lfs_handle { 524 Some(handle) => ssh_base.with_lfs(handle.clone(), lfs_max_ssh_transfers), 525 None => ssh_base, 526 }); 527 528 let xrpc_state = Arc::new(XrpcState { 529 layout: layout.clone(), 530 index: Arc::clone(&index), 531 atproto: Arc::clone(&atproto), 532 secrets, 533 entropy: Arc::new(OsEntropy), 534 ci_logs, 535 admins, 536 admission, 537 knot_did, 538 knot_hostname: hostname, 539 meta_path, 540 knot_service_url, 541 limiter: Arc::new(knot_xrpc::PreAuthLimiter::with_config(xrpc_limits)), 542 cob_locks: Arc::new(knot_xrpc::CobLocks::default()), 543 reservations, 544 trusted_proxy_header, 545 committer, 546 byte_limits, 547 budgets, 548 git_http, 549 pack_limits, 550 service_owner, 551 subscriber_gate, 552 maintenance: maintenance_handle, 553 appview: appview_endpoint, 554 slots: slots.clone(), 555 events, 556 lfs: lfs_handle.map(|handle| knot_xrpc::LfsWeb::new(handle, lfs_max_http_downloads)), 557 catalog: Arc::clone(&catalog), 558 }); 559 560 let resolver: Arc<dyn knot_pack::RepoResolver> = { 561 let index = Arc::clone(&index); 562 Arc::new(move |target: &knot_pack::RepoTarget| match target { 563 knot_pack::RepoTarget::Did(did) => { 564 knot_pack::RepoLookup::from_resolved(index.owner_of(did), |_| did.clone()) 565 } 566 knot_pack::RepoTarget::OwnerPath(owner, path) => knot_pack::RepoLookup::from_resolved( 567 index.resolve_clone_path(owner, path), 568 |found| found, 569 ), 570 }) 571 }; 572 let receive_advertiser = knot_xrpc::receive_advertiser(Arc::clone(&xrpc_state)); 573 let handle_resolver: Arc<dyn knot_pack::HandleResolver> = Arc::new(AtprotoHandleResolver { 574 atproto: Arc::clone(&atproto), 575 }); 576 let (write_routes, early_data_safe) = knot_pack::edge_routes( 577 layout, 578 resolver, 579 Some(receive_advertiser), 580 Some(handle_resolver), 581 slots.pack.clone(), 582 pack_cache_config, 583 Arc::clone(&catalog), 584 xrpc_state.knot_hostname.clone(), 585 Arc::new(SystemClock), 586 ); 587 let legacy_admin_routes = legacy_admin.map(|secret| { 588 tracing::warn!( 589 route = knot_xrpc::legacy_admin::ADD_MEMBER_ROUTE, 590 "serving the legacy basic-auth admin route" 591 ); 592 knot_xrpc::legacy_admin::router(Arc::clone(&xrpc_state), secret) 593 }); 594 let base_router = write_routes.merge(knot_xrpc::router(xrpc_state)).route( 595 "/.well-known/did.json", 596 get(move || { 597 let document = did_document.clone(); 598 async move { Json(document) } 599 }), 600 ); 601 let base_router = match legacy_admin_routes { 602 Some(routes) => base_router.merge(routes), 603 None => base_router, 604 }; 605 let base_router = match homepage { 606 HomepageSource::Disabled => base_router, 607 HomepageSource::Default => base_router.route("/", get(|| async { Html(DEFAULT_HOMEPAGE) })), 608 HomepageSource::File(path) => base_router.route_service("/", ServeFile::new(path)), 609 }; 610 let app = knot_edge::RequiresFullHandshake::new(base_router); 611 let scheme = if tls_setup.is_some() { "https" } else { "http" }; 612 let edge_config = knot_edge::EdgeConfig { 613 http_addr: knot_edge::PublicBind::new(http_addr), 614 limits: listen_limits, 615 guards: edge_guards, 616 tls: tls_setup, 617 }; 618 tracing::info!("listening on {scheme}://{http_addr} and ssh://{ssh_addr}"); 619 620 let shutdown = CancellationToken::new(); 621 tokio::spawn(allocator::govern_decay(shutdown.clone())); 622 let mut edge_task = tokio::spawn(knot_edge::serve( 623 edge_config, 624 app, 625 early_data_safe, 626 shutdown.clone(), 627 )); 628 let mut ssh_task = { 629 let shutdown = shutdown.clone(); 630 tokio::spawn(async move { knot_ssh::serve(ssh_addr, host_key, ssh_state, shutdown).await }) 631 }; 632 633 let exit = tokio::select! { 634 result = &mut edge_task => FirstExit::Edge(result), 635 result = &mut ssh_task => FirstExit::Ssh(result), 636 () = shutdown_signal() => { 637 tracing::info!("shutdown signal received"); 638 FirstExit::Signal 639 } 640 }; 641 shutdown.cancel(); 642 let drain = async { 643 match &exit { 644 FirstExit::Edge(_) => { 645 let _ = (&mut ssh_task).await; 646 } 647 FirstExit::Ssh(_) => { 648 let _ = (&mut edge_task).await; 649 } 650 FirstExit::Signal => { 651 let _ = (&mut edge_task).await; 652 let _ = (&mut ssh_task).await; 653 } 654 } 655 }; 656 if tokio::time::timeout(EDGE_SHUTDOWN_DRAIN, drain) 657 .await 658 .is_err() 659 { 660 tracing::warn!( 661 timeout_secs = EDGE_SHUTDOWN_DRAIN.as_secs(), 662 "aborting edge drain after timeout" 663 ); 664 } 665 if let Some(shutdown) = maintenance_shutdown { 666 let _ = shutdown.send(true); 667 } 668 if let Some(task) = maintenance_task 669 && tokio::time::timeout(MAINTENANCE_SHUTDOWN_DRAIN, task) 670 .await 671 .is_err() 672 { 673 tracing::warn!( 674 timeout_secs = MAINTENANCE_SHUTDOWN_DRAIN.as_secs(), 675 "aborting maintenance drain after timeout :3" 676 ); 677 } 678 match exit { 679 FirstExit::Edge(result) => result 680 .context("edge server task panicked")? 681 .context("serve edge")?, 682 FirstExit::Ssh(result) => result 683 .context("ssh server task panicked")? 684 .context("serve ssh")?, 685 FirstExit::Signal => {} 686 } 687 Ok(()) 688} 689 690enum FirstExit { 691 Edge(Result<Result<(), knot_edge::EdgeError>, tokio::task::JoinError>), 692 Ssh(Result<Result<(), knot_ssh::SshError>, tokio::task::JoinError>), 693 Signal, 694} 695 696fn legacy_admin_secret( 697 config: &knot_config::Validated, 698) -> Option<knot_xrpc::legacy_admin::LegacyAdminSecret> { 699 let name = config.acl.legacy_admin_secret_env.as_deref()?; 700 let value = zeroize::Zeroizing::new(std::env::var(name).unwrap_or_default()); 701 match knot_xrpc::legacy_admin::LegacyAdminSecret::new(&value) { 702 Ok(secret) => Some(secret), 703 Err(_) => { 704 tracing::warn!( 705 secret_env = name, 706 "the environment variable in acl.legacy_admin_secret_env is unset or empty, so we won't serve the admin route" 707 ); 708 None 709 } 710 } 711} 712 713fn build_tls_setup( 714 config: &knot_config::KnotConfig, 715 hostname: &knot_types::KnotHostname, 716) -> anyhow::Result<Option<knot_edge::TlsSetup>> { 717 let tls = &config.tls; 718 let source = if tls.acme_enabled { 719 knot_edge::CertSource::Acme(knot_edge::AcmeParams { 720 domains: vec![hostname.clone()], 721 contact: knot_edge::AcmeContact::new( 722 tls.acme_contact 723 .clone() 724 .context("tls.acme_contact is required when ACME is enabled")?, 725 )?, 726 cache_dir: knot_edge::AcmeCacheDir::new( 727 tls.acme_cache_dir 728 .clone() 729 .context("tls.acme_cache_dir is required when ACME is enabled")?, 730 ), 731 production: !tls.acme_staging, 732 }) 733 } else { 734 match (&tls.cert_path, &tls.key_path) { 735 (Some(cert_path), Some(key_path)) => { 736 knot_edge::CertSource::Static(knot_edge::StaticCertPaths { 737 cert_path: knot_edge::CertChainPath::new(cert_path.clone()), 738 key_path: knot_edge::PrivateKeyPath::new(key_path.clone()), 739 }) 740 } 741 _ => return Ok(None), 742 } 743 }; 744 745 let internal = match tls.mtls_enabled { 746 true => Some(knot_edge::InternalTls { 747 addr: knot_edge::InternalBind::new(config.server.internal_listen_addr), 748 client_ca_path: knot_edge::ClientCaPath::new( 749 tls.mtls_client_ca_path 750 .clone() 751 .context("tls.mtls_client_ca_path is required when mTLS is enabled")?, 752 ), 753 spki_pin: knot_edge::SpkiPin::from_base64( 754 tls.mtls_admin_spki_pin 755 .as_deref() 756 .context("tls.mtls_admin_spki_pin is required when mTLS is enabled")?, 757 ) 758 .context("parse tls.mtls_admin_spki_pin")?, 759 }), 760 false => None, 761 }; 762 763 Ok(Some(knot_edge::TlsSetup { 764 source, 765 http3: tls.http3, 766 internal, 767 })) 768} 769 770async fn shutdown_signal() { 771 let interrupt = async { 772 let _ = tokio::signal::ctrl_c().await; 773 }; 774 #[cfg(unix)] 775 let terminate = async { 776 match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) { 777 Ok(mut stream) => { 778 stream.recv().await; 779 } 780 Err(_) => std::future::pending::<()>().await, 781 } 782 }; 783 #[cfg(not(unix))] 784 let terminate = std::future::pending::<()>(); 785 786 tokio::select! { 787 () = interrupt => {} 788 () = terminate => {} 789 } 790}