From 97e1b6285ecfe2a068c4628a5037c51e1f243b9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=80=E1=B4=8D=E1=B4=9B=E1=B4=8F=E1=B4=80=E1=B4=87?= =?UTF-8?q?=CA=80?= Date: Mon, 5 Jan 2026 12:13:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20bind=5Faddress=20=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E5=90=8E=E5=B0=9D=E8=AF=95=20fallback=20?= =?UTF-8?q?=E5=88=B0=E9=BB=98=E8=AE=A4=E5=9C=B0=E5=9D=80=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E6=97=A0=E6=B3=95=E5=90=AF=E5=8A=A8=20web=20=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=20(#590)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/bili_sync/src/config/default.rs | 2 +- crates/bili_sync/src/config/mod.rs | 1 + crates/bili_sync/src/task/http_server.rs | 29 ++++++++++++++++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/crates/bili_sync/src/config/default.rs b/crates/bili_sync/src/config/default.rs index 4a6c513..92aaf28 100644 --- a/crates/bili_sync/src/config/default.rs +++ b/crates/bili_sync/src/config/default.rs @@ -13,6 +13,6 @@ pub(super) fn default_auth_token() -> String { .collect() } -pub(super) fn default_bind_address() -> String { +pub(crate) fn default_bind_address() -> String { "0.0.0.0:12345".to_string() } diff --git a/crates/bili_sync/src/config/mod.rs b/crates/bili_sync/src/config/mod.rs index 16b372b..f0f43fc 100644 --- a/crates/bili_sync/src/config/mod.rs +++ b/crates/bili_sync/src/config/mod.rs @@ -8,6 +8,7 @@ mod versioned_config; pub use crate::config::args::{ARGS, version}; pub use crate::config::current::{CONFIG_DIR, Config}; +pub(crate) use crate::config::default::default_bind_address; pub use crate::config::handlebar::TEMPLATE; pub use crate::config::item::{ConcurrentDownloadLimit, NFOTimeType, PathSafeTemplate, RateLimit, Trigger}; pub use crate::config::versioned_cache::VersionedCache; diff --git a/crates/bili_sync/src/task/http_server.rs b/crates/bili_sync/src/task/http_server.rs index 48091be..0339123 100644 --- a/crates/bili_sync/src/task/http_server.rs +++ b/crates/bili_sync/src/task/http_server.rs @@ -13,7 +13,7 @@ use sea_orm::DatabaseConnection; use crate::api::{LogHelper, router}; use crate::bilibili::BiliClient; -use crate::config::VersionedConfig; +use crate::config::{VersionedConfig, default_bind_address}; #[derive(RustEmbed)] #[preserve_source = false] @@ -30,10 +30,29 @@ pub async fn http_server( .layer(Extension(database_connection)) .layer(Extension(bili_client)) .layer(Extension(log_writer)); - let bind_address = VersionedConfig::get().read().bind_address.to_owned(); - let listener = tokio::net::TcpListener::bind(&bind_address) - .await - .context("bind address failed")?; + let (bind_address, listener) = { + let bind_address = VersionedConfig::get().read().bind_address.to_owned(); + let listen_res = tokio::net::TcpListener::bind(&bind_address) + .await + .context("bind address failed"); + match listen_res { + Ok(listener) => (bind_address, listener), + Err(e) => { + let default_bind_address = default_bind_address(); + if default_bind_address == bind_address { + return Err(e); + } + warn!( + "绑定到地址 {} 失败:{:#},尝试绑定到默认地址 {}", + bind_address, e, default_bind_address + ); + let listener = tokio::net::TcpListener::bind(&default_bind_address) + .await + .context("bind default address failed")?; + (default_bind_address, listener) + } + } + }; info!("开始运行管理页:http://{}", bind_address); Ok(axum::serve(listener, ServiceExt::::into_make_service(app)).await?) }