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
31 kB 797 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 // `ProxyTrust::client_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 proxy_trust = knot_types::ProxyTrust::new(trusted_proxy_header, config.trusted_proxies()?); 294 if proxy_trust.trusts_any_peer() && !http_addr.ip().is_loopback() { 295 tracing::warn!( 296 bind = %http_addr, 297 "xrpc.trusted_proxy_header is set without xrpc.trusted_proxies while the HTTP surface takes connections from off-host, so a client that reaches this knot without passing the proxy can forge the header and pick its own rate-limit bucket. List the proxy's address in xrpc.trusted_proxies." 298 ); 299 } 300 let edge_guards = knot_edge::EdgeGuards::new( 301 knot_edge::RequestsPerSecond::new( 302 NonZeroU32::new(config.server.listen_rate_limit_per_second) 303 .context("server.listen_rate_limit_per_second must be greater than zero")?, 304 ), 305 knot_edge::BurstSize::new( 306 NonZeroU32::new(config.server.listen_rate_limit_burst) 307 .context("server.listen_rate_limit_burst must be greater than zero")?, 308 ), 309 knot_edge::MaxInflightRequests::new( 310 NonZeroU32::new(config.server.listen_max_inflight_requests) 311 .context("server.listen_max_inflight_requests must be greater than zero")?, 312 ), 313 knot_edge::RequestTimeout::from_millis( 314 NonZeroU64::new(config.server.listen_request_timeout_ms) 315 .context("server.listen_request_timeout_ms must be greater than zero")?, 316 ), 317 knot_edge::BodyInactivityTimeout::from_millis( 318 NonZeroU64::new(config.server.listen_body_timeout_ms) 319 .context("server.listen_body_timeout_ms must be greater than zero")?, 320 ), 321 knot_edge::WriteRequestTimeout::from_millis( 322 NonZeroU64::new(config.server.listen_write_request_timeout_ms) 323 .context("server.listen_write_request_timeout_ms must be greater than zero")?, 324 ), 325 proxy_trust.clone(), 326 ); 327 let tls_setup = build_tls_setup(&config, &hostname).context("assemble TLS configuration")?; 328 if config.tls.http3 && tls_setup.is_none() { 329 tracing::warn!( 330 "tls.http3 is set without any TLS certificate, so HTTP/3 won't start. Configure a static cert or ACME to serve h3." 331 ); 332 } 333 if tls_setup.is_none() && config.xrpc.trusted_proxy_header.is_none() { 334 tracing::warn!( 335 "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, and xrpc.trusted_proxies to the address it connects from." 336 ); 337 } 338 if config.tls.acme_enabled && http_addr.port() != 443 { 339 tracing::warn!( 340 listen_port = http_addr.port(), 341 "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." 342 ); 343 } 344 if config.tls.acme_enabled && config.tls.acme_staging { 345 tracing::warn!( 346 "ACME is using the Let's Encrypt staging directory. Its certificates aren't browser-trusted. Unset tls.acme_staging for real certificates." 347 ); 348 } 349 let ssh_addr = config.server.ssh_listen_addr; 350 let ssh_max_pack_bytes = config.server.ssh_max_pack_bytes as usize; 351 let pack_limits = knot_pack::PackLimits { 352 max_objects: ObjectCount::from(config.pack.max_objects), 353 max_total_bytes: knot_pack::MaxTotalBytes::new(config.pack.max_total_bytes), 354 ..knot_pack::PackLimits::default() 355 }; 356 knot_pack::init_selection_limits(knot_pack::SelectionLimits { 357 max_objects: ObjectCount::from(config.pack.selection_max_objects), 358 time_budget: Duration::from_secs(config.pack.selection_time_budget_secs), 359 }); 360 let host_key = knot_ssh::load_or_create_host_key(&config.server.ssh_host_key_file) 361 .context("load or create SSH host key")?; 362 363 let xrpc_limits = knot_xrpc::LimitConfig { 364 rate: Some(knot_xrpc::RateLimit { 365 burst: knot_xrpc::Burst::new(config.xrpc.preauth_burst), 366 refill: knot_xrpc::RefillMicros::new( 367 config.xrpc.preauth_refill_ms.saturating_mul(1_000), 368 ), 369 }), 370 per_peer_inflight: Some(knot_xrpc::PerPeerInflight::new( 371 config.xrpc.per_peer_inflight as usize, 372 )), 373 global_inflight: Some(knot_xrpc::GlobalInflight::new( 374 config.xrpc.global_inflight as usize, 375 )), 376 }; 377 let byte_limits = knot_xrpc::ByteLimits { 378 body: knot_xrpc::BodyLimit::new(config.xrpc.max_body_bytes as usize), 379 patch: knot_xrpc::PatchLimit::new(config.xrpc.max_patch_bytes as usize), 380 patch_decompressed: knot_xrpc::PatchDecompressedLimit::new( 381 config.xrpc.max_patch_decompressed_bytes, 382 ), 383 response: knot_xrpc::ResponseLimit::new(config.xrpc.max_response_bytes as usize), 384 archive: knot_xrpc::ArchiveLimit::new(config.xrpc.max_archive_bytes), 385 fork_pack: knot_xrpc::ForkPackLimit::new(config.xrpc.fork_max_pack_bytes), 386 pack: knot_xrpc::MaxWireBytes::new(ssh_max_pack_bytes), 387 }; 388 let budgets = knot_xrpc::Budgets { 389 tree_last_commit: knot_xrpc::TreeReadBudget::new(knot_xrpc::ReadBudget::Within( 390 Duration::from_millis(config.xrpc.tree_last_commit_budget_ms), 391 )), 392 blob_last_commit: knot_xrpc::BlobReadBudget::new(knot_xrpc::ReadBudget::Within( 393 Duration::from_millis(config.xrpc.blob_last_commit_budget_ms), 394 )), 395 languages: knot_xrpc::LanguagesReadBudget::new(knot_xrpc::ReadBudget::Within( 396 Duration::from_millis(config.xrpc.languages_budget_ms), 397 )), 398 languages_push: knot_xrpc::LanguagesPushBudget::new(Duration::from_millis( 399 config.xrpc.languages_push_budget_ms, 400 )), 401 }; 402 let committer = knot_xrpc::Committer { 403 name: AuthorName::new(config.git.user_name.clone()), 404 email: Email::new(config.git.user_email.clone()), 405 }; 406 let reservations = Arc::new(knot_xrpc::Reservations::new( 407 knot_xrpc::ReservationTtl::new(config.xrpc.reservation_ttl_secs as i64), 408 knot_xrpc::PerActorQuota::new(config.xrpc.per_actor_reservations as usize), 409 knot_xrpc::GlobalQuota::new(config.xrpc.max_pending_reservations as usize), 410 )); 411 let replay_bounds = knot_events::ReplayBounds::new( 412 knot_events::ReplayEvents::new(config.xrpc.events_replay_buffer as usize) 413 .context("xrpc.events_replay_buffer must be greater than zero")?, 414 knot_events::ReplayBytes::new(config.xrpc.events_replay_bytes as usize) 415 .context("xrpc.events_replay_bytes must be greater than zero")?, 416 ); 417 let events = Arc::new(knot_events::EventLog::new(SystemClock, replay_bounds)); 418 let subscriber_gate = Arc::new(knot_events::SubscriberGate::new( 419 knot_events::GlobalSubscriberLimit::new(config.xrpc.events_max_subscribers as usize), 420 knot_events::PerPeerSubscriberLimit::new(config.xrpc.events_max_per_peer as usize), 421 )); 422 423 let maintenance_enabled = config.maintenance.enabled; 424 let maintenance_options = knot_maintenance::Options::from_config(&config.maintenance); 425 let maintenance_interval = Duration::from_secs(config.maintenance.interval_secs); 426 let maintenance_large_push = 427 knot_maintenance::PushBytes::new(config.maintenance.large_push_bytes); 428 429 let lfs_handle = config 430 .lfs 431 .store_path 432 .as_ref() 433 .map(|path| { 434 knot_lfs::LfsHandle::open( 435 knot_lfs::LfsStorePath::new(path), 436 knot_lfs::LfsSize::new(config.lfs.max_object_bytes), 437 knot_lfs::FreeSpaceFloor::new(config.lfs.free_space_floor_bytes), 438 ) 439 .inspect(|_| { 440 tracing::info!(store = %path.display(), "git-lfs capability enabled"); 441 }) 442 }) 443 .transpose() 444 .context("open LFS object store")?; 445 let lfs_max_ssh_transfers = config.lfs.max_ssh_transfers as usize; 446 let lfs_max_http_downloads = config.lfs.max_http_downloads as usize; 447 let lfs_gc_grace = knot_maintenance::lfs_grace( 448 knot_maintenance::GcGrace::from_secs(config.lfs.gc_grace_secs), 449 knot_maintenance::ReflogRetention::from_secs(config.maintenance.reflog_expire_secs), 450 ); 451 let lfs_gc_interval = 452 knot_maintenance::SweepInterval::new(Duration::from_secs(config.lfs.gc_interval_secs)); 453 if lfs_handle.is_some() && !maintenance_enabled { 454 tracing::warn!( 455 "LFS store configured but maintenance is disabled. Unreferenced LFS objects will accumulate with no garbage collection or orphan sweep." 456 ); 457 } 458 459 let pack_cache_config = knot_pack::CacheConfig { 460 enabled: config.pack_cache.enabled, 461 ttl: Duration::from_secs(config.pack_cache.ttl_secs), 462 max_entry_bytes: knot_pack::MaxEntryBytes::new(config.pack_cache.max_entry_bytes as usize), 463 max_total_bytes: knot_pack::MaxCacheBytes::new(knot_resource::pack_cache_bytes( 464 config.pack_cache.max_total_bytes, 465 ) as usize), 466 }; 467 468 let ci_logs = config 469 .ci 470 .logs_addr 471 .as_deref() 472 .map(CiLogsAddr::new) 473 .transpose() 474 .context("ci.logs_addr must be host:port")?; 475 476 let homepage = config.homepage.source(); 477 let catalog = Arc::new( 478 knot_messages::Catalog::parse(&config.messages).context("parse message templates")?, 479 ); 480 let legacy_admin = legacy_admin_secret(&config); 481 482 knot_config::init(config); 483 484 let (maintenance_handle, maintenance_shutdown, maintenance_task) = if maintenance_enabled { 485 let (scheduler, handle) = knot_maintenance::Scheduler::new( 486 layout.clone(), 487 Arc::new(IndexRepos(Arc::clone(&index))), 488 SystemClock, 489 maintenance_options, 490 maintenance_interval, 491 maintenance_large_push, 492 ); 493 let scheduler = match &lfs_handle { 494 Some(lfs) => { 495 scheduler.with_lfs_gc(Arc::clone(&lfs.store), lfs_gc_grace, lfs_gc_interval) 496 } 497 None => scheduler, 498 }; 499 let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false); 500 let task = tokio::spawn(scheduler.run(shutdown_rx)); 501 tracing::info!( 502 interval_secs = maintenance_interval.as_secs(), 503 "maintenance scheduler running" 504 ); 505 (handle, Some(shutdown_tx), Some(task)) 506 } else { 507 (knot_maintenance::MaintenanceHandle::disabled(), None, None) 508 }; 509 510 let slots = knot_resource::Slots::for_machine(); 511 512 let ssh_base = knot_ssh::SshState::new(knot_ssh::SshConfig { 513 layout: layout.clone(), 514 index: Arc::clone(&index), 515 atproto: Arc::clone(&atproto), 516 knot_actor, 517 events: Arc::clone(&events), 518 hostname: hostname.clone(), 519 appview: appview_endpoint.clone(), 520 admins: admins.clone(), 521 admission, 522 max_pack_bytes: byte_limits.pack, 523 archive_limit: byte_limits.archive, 524 languages_push_budget: budgets.languages_push, 525 ci_logs: ci_logs.clone(), 526 }) 527 .with_maintenance(maintenance_handle.clone()) 528 .with_limits(pack_limits) 529 .with_slots(slots.clone()) 530 .with_catalog(Arc::clone(&catalog)); 531 let ssh_state = Arc::new(match &lfs_handle { 532 Some(handle) => ssh_base.with_lfs(handle.clone(), lfs_max_ssh_transfers), 533 None => ssh_base, 534 }); 535 536 let xrpc_state = Arc::new(XrpcState { 537 layout: layout.clone(), 538 index: Arc::clone(&index), 539 atproto: Arc::clone(&atproto), 540 secrets, 541 entropy: Arc::new(OsEntropy), 542 ci_logs, 543 admins, 544 admission, 545 knot_did, 546 knot_hostname: hostname, 547 meta_path, 548 knot_service_url, 549 limiter: Arc::new(knot_xrpc::PreAuthLimiter::with_config(xrpc_limits)), 550 cob_locks: Arc::new(knot_xrpc::CobLocks::default()), 551 reservations, 552 proxy_trust, 553 committer, 554 byte_limits, 555 budgets, 556 git_http, 557 pack_limits, 558 service_owner, 559 subscriber_gate, 560 maintenance: maintenance_handle, 561 appview: appview_endpoint, 562 slots: slots.clone(), 563 events, 564 lfs: lfs_handle.map(|handle| knot_xrpc::LfsWeb::new(handle, lfs_max_http_downloads)), 565 catalog: Arc::clone(&catalog), 566 }); 567 568 let resolver: Arc<dyn knot_pack::RepoResolver> = { 569 let index = Arc::clone(&index); 570 Arc::new(move |target: &knot_pack::RepoTarget| match target { 571 knot_pack::RepoTarget::Did(did) => { 572 knot_pack::RepoLookup::from_resolved(index.owner_of(did), |_| did.clone()) 573 } 574 knot_pack::RepoTarget::OwnerPath(owner, path) => knot_pack::RepoLookup::from_resolved( 575 index.resolve_clone_path(owner, path), 576 |found| found, 577 ), 578 }) 579 }; 580 let receive_advertiser = knot_xrpc::receive_advertiser(Arc::clone(&xrpc_state)); 581 let handle_resolver: Arc<dyn knot_pack::HandleResolver> = Arc::new(AtprotoHandleResolver { 582 atproto: Arc::clone(&atproto), 583 }); 584 let (write_routes, early_data_safe) = knot_pack::edge_routes(knot_pack::EdgeConfig { 585 receive: Some(receive_advertiser), 586 handle_resolver: Some(handle_resolver), 587 pack_slots: slots.pack.clone(), 588 cache: pack_cache_config, 589 catalog: Arc::clone(&catalog), 590 hostname: xrpc_state.knot_hostname.clone(), 591 archive_limit: byte_limits.archive, 592 ..knot_pack::EdgeConfig::serving(layout, resolver, Arc::new(SystemClock)) 593 }); 594 let legacy_admin_routes = legacy_admin.map(|secret| { 595 tracing::warn!( 596 route = knot_xrpc::legacy_admin::ADD_MEMBER_ROUTE, 597 "serving the legacy basic-auth admin route" 598 ); 599 knot_xrpc::legacy_admin::router(Arc::clone(&xrpc_state), secret) 600 }); 601 let base_router = write_routes.merge(knot_xrpc::router(xrpc_state)).route( 602 "/.well-known/did.json", 603 get(move || { 604 let document = did_document.clone(); 605 async move { Json(document) } 606 }), 607 ); 608 let base_router = match legacy_admin_routes { 609 Some(routes) => base_router.merge(routes), 610 None => base_router, 611 }; 612 let base_router = match homepage { 613 HomepageSource::Disabled => base_router, 614 HomepageSource::Default => base_router.route("/", get(|| async { Html(DEFAULT_HOMEPAGE) })), 615 HomepageSource::File(path) => base_router.route_service("/", ServeFile::new(path)), 616 }; 617 let app = knot_edge::RequiresFullHandshake::new(base_router); 618 let scheme = if tls_setup.is_some() { "https" } else { "http" }; 619 let edge_config = knot_edge::EdgeConfig { 620 http_addr: knot_edge::PublicBind::new(http_addr), 621 limits: listen_limits, 622 guards: edge_guards, 623 tls: tls_setup, 624 }; 625 tracing::info!("listening on {scheme}://{http_addr} and ssh://{ssh_addr}"); 626 627 let shutdown = CancellationToken::new(); 628 tokio::spawn(allocator::govern_decay(shutdown.clone())); 629 let mut edge_task = tokio::spawn(knot_edge::serve( 630 edge_config, 631 app, 632 early_data_safe, 633 shutdown.clone(), 634 )); 635 let mut ssh_task = { 636 let shutdown = shutdown.clone(); 637 tokio::spawn(async move { knot_ssh::serve(ssh_addr, host_key, ssh_state, shutdown).await }) 638 }; 639 640 let exit = tokio::select! { 641 result = &mut edge_task => FirstExit::Edge(result), 642 result = &mut ssh_task => FirstExit::Ssh(result), 643 () = shutdown_signal() => { 644 tracing::info!("shutdown signal received"); 645 FirstExit::Signal 646 } 647 }; 648 shutdown.cancel(); 649 let drain = async { 650 match &exit { 651 FirstExit::Edge(_) => { 652 let _ = (&mut ssh_task).await; 653 } 654 FirstExit::Ssh(_) => { 655 let _ = (&mut edge_task).await; 656 } 657 FirstExit::Signal => { 658 let _ = (&mut edge_task).await; 659 let _ = (&mut ssh_task).await; 660 } 661 } 662 }; 663 if tokio::time::timeout(EDGE_SHUTDOWN_DRAIN, drain) 664 .await 665 .is_err() 666 { 667 tracing::warn!( 668 timeout_secs = EDGE_SHUTDOWN_DRAIN.as_secs(), 669 "aborting edge drain after timeout" 670 ); 671 } 672 if let Some(shutdown) = maintenance_shutdown { 673 let _ = shutdown.send(true); 674 } 675 if let Some(task) = maintenance_task 676 && tokio::time::timeout(MAINTENANCE_SHUTDOWN_DRAIN, task) 677 .await 678 .is_err() 679 { 680 tracing::warn!( 681 timeout_secs = MAINTENANCE_SHUTDOWN_DRAIN.as_secs(), 682 "aborting maintenance drain after timeout :3" 683 ); 684 } 685 match exit { 686 FirstExit::Edge(result) => result 687 .context("edge server task panicked")? 688 .context("serve edge")?, 689 FirstExit::Ssh(result) => result 690 .context("ssh server task panicked")? 691 .context("serve ssh")?, 692 FirstExit::Signal => {} 693 } 694 Ok(()) 695} 696 697enum FirstExit { 698 Edge(Result<Result<(), knot_edge::EdgeError>, tokio::task::JoinError>), 699 Ssh(Result<Result<(), knot_ssh::SshError>, tokio::task::JoinError>), 700 Signal, 701} 702 703fn legacy_admin_secret( 704 config: &knot_config::Validated, 705) -> Option<knot_xrpc::legacy_admin::LegacyAdminSecret> { 706 let name = config.acl.legacy_admin_secret_env.as_deref()?; 707 let value = zeroize::Zeroizing::new(std::env::var(name).unwrap_or_default()); 708 match knot_xrpc::legacy_admin::LegacyAdminSecret::new(&value) { 709 Ok(secret) => Some(secret), 710 Err(_) => { 711 tracing::warn!( 712 secret_env = name, 713 "the environment variable in acl.legacy_admin_secret_env is unset or empty, so we won't serve the admin route" 714 ); 715 None 716 } 717 } 718} 719 720fn build_tls_setup( 721 config: &knot_config::KnotConfig, 722 hostname: &knot_types::KnotHostname, 723) -> anyhow::Result<Option<knot_edge::TlsSetup>> { 724 let tls = &config.tls; 725 let source = if tls.acme_enabled { 726 knot_edge::CertSource::Acme(knot_edge::AcmeParams { 727 domains: vec![hostname.clone()], 728 contact: knot_edge::AcmeContact::new( 729 tls.acme_contact 730 .clone() 731 .context("tls.acme_contact is required when ACME is enabled")?, 732 )?, 733 cache_dir: knot_edge::AcmeCacheDir::new( 734 tls.acme_cache_dir 735 .clone() 736 .context("tls.acme_cache_dir is required when ACME is enabled")?, 737 ), 738 production: !tls.acme_staging, 739 }) 740 } else { 741 match (&tls.cert_path, &tls.key_path) { 742 (Some(cert_path), Some(key_path)) => { 743 knot_edge::CertSource::Static(knot_edge::StaticCertPaths { 744 cert_path: knot_edge::CertChainPath::new(cert_path.clone()), 745 key_path: knot_edge::PrivateKeyPath::new(key_path.clone()), 746 }) 747 } 748 _ => return Ok(None), 749 } 750 }; 751 752 let internal = match tls.mtls_enabled { 753 true => Some(knot_edge::InternalTls { 754 addr: knot_edge::InternalBind::new(config.server.internal_listen_addr), 755 client_ca_path: knot_edge::ClientCaPath::new( 756 tls.mtls_client_ca_path 757 .clone() 758 .context("tls.mtls_client_ca_path is required when mTLS is enabled")?, 759 ), 760 spki_pin: knot_edge::SpkiPin::from_base64( 761 tls.mtls_admin_spki_pin 762 .as_deref() 763 .context("tls.mtls_admin_spki_pin is required when mTLS is enabled")?, 764 ) 765 .context("parse tls.mtls_admin_spki_pin")?, 766 }), 767 false => None, 768 }; 769 770 Ok(Some(knot_edge::TlsSetup { 771 source, 772 http3: tls.http3, 773 internal, 774 })) 775} 776 777async fn shutdown_signal() { 778 let interrupt = async { 779 let _ = tokio::signal::ctrl_c().await; 780 }; 781 #[cfg(unix)] 782 let terminate = async { 783 match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) { 784 Ok(mut stream) => { 785 stream.recv().await; 786 } 787 Err(_) => std::future::pending::<()>().await, 788 } 789 }; 790 #[cfg(not(unix))] 791 let terminate = std::future::pending::<()>(); 792 793 tokio::select! { 794 () = interrupt => {} 795 () = terminate => {} 796 } 797}