From 3940ae2d691eaaead780c2c15bd10809663c77df Mon Sep 17 00:00:00 2001 From: loverustfs Date: Sun, 13 Apr 2025 19:53:29 +0800 Subject: [PATCH] fix tls configs error --- rustfs/src/config/mod.rs | 50 ++++++++++++++++++----------------- rustfs/src/console.rs | 57 ++++++++++++++++++++++------------------ 2 files changed, 58 insertions(+), 49 deletions(-) diff --git a/rustfs/src/config/mod.rs b/rustfs/src/config/mod.rs index f53bc0958..eb7a00ba2 100644 --- a/rustfs/src/config/mod.rs +++ b/rustfs/src/config/mod.rs @@ -2,6 +2,7 @@ use clap::Parser; use const_str::concat; use ecstore::global::DEFAULT_PORT; use std::string::ToString; +use std::sync::OnceLock; shadow_rs::shadow!(build); /// Default Access Key @@ -47,16 +48,16 @@ const SHORT_VERSION: &str = { }; const LONG_VERSION: &str = concat!( -concat!(SHORT_VERSION, "\n"), -concat!("build time : ", build::BUILD_TIME, "\n"), -concat!("build profile: ", build::BUILD_RUST_CHANNEL, "\n"), -concat!("build os : ", build::BUILD_OS, "\n"), -concat!("rust version : ", build::RUST_VERSION, "\n"), -concat!("rust channel : ", build::RUST_CHANNEL, "\n"), -concat!("git branch : ", build::BRANCH, "\n"), -concat!("git commit : ", build::COMMIT_HASH, "\n"), -concat!("git tag : ", build::TAG, "\n"), -concat!("git status :\n", build::GIT_STATUS_FILE), + concat!(SHORT_VERSION, "\n"), + concat!("build time : ", build::BUILD_TIME, "\n"), + concat!("build profile: ", build::BUILD_RUST_CHANNEL, "\n"), + concat!("build os : ", build::BUILD_OS, "\n"), + concat!("rust version : ", build::RUST_VERSION, "\n"), + concat!("rust channel : ", build::RUST_CHANNEL, "\n"), + concat!("git branch : ", build::BRANCH, "\n"), + concat!("git commit : ", build::COMMIT_HASH, "\n"), + concat!("git tag : ", build::TAG, "\n"), + concat!("git status :\n", build::GIT_STATUS_FILE), ); #[derive(Debug, Parser, Clone)] @@ -70,6 +71,7 @@ pub struct Opt { #[arg(long, default_value_t = format!("0.0.0.0:{}", DEFAULT_PORT), env = "RUSTFS_ADDRESS")] pub address: String, + /// Domain name used for virtual-hosted-style requests. #[arg(long, env = "RUSTFS_SERVER_DOMAINS")] pub server_domains: Vec, @@ -81,16 +83,16 @@ pub struct Opt { #[arg(long, default_value_t = DEFAULT_SECRET_KEY.to_string(), env = "RUSTFS_SECRET_KEY")] pub secret_key: String, - /// Domain name used for virtual-hosted-style requests. - #[arg(long, env = "RUSTFS_DOMAIN_NAME")] - pub domain_name: Option, - #[arg(long, default_value_t = false, env = "RUSTFS_CONSOLE_ENABLE")] pub console_enable: bool, #[arg(long, default_value_t = format!("127.0.0.1:{}", 9002), env = "RUSTFS_CONSOLE_ADDRESS")] pub console_address: String, + /// rustfs endpoint for console + #[arg(long, env = "RUSTFS_CONSOLE_FS_ENDPOINT")] + pub console_fs_endpoint: Option, + /// Observability configuration file /// Default value: config/obs.toml #[arg(long, default_value_t = DEFAULT_OBS_CONFIG.to_string(), env = "RUSTFS_OBS_CONFIG")] @@ -104,15 +106,15 @@ pub struct Opt { pub license: Option, } -// lazy_static::lazy_static! { -// pub static ref OPT: OnceLock = OnceLock::new(); -// } +lazy_static::lazy_static! { + pub(crate) static ref OPT: OnceLock = OnceLock::new(); +} -// pub fn init_config() { -// let opt = Opt::parse(); -// OPT.set(opt).expect("Failed to set global config"); -// } +pub fn init_config(opt: Opt) { + OPT.set(opt).expect("Failed to set global config"); +} + +pub fn get_config() -> &'static Opt { + OPT.get().expect("Global config not initialized") +} -// pub fn get_config() -> &'static Opt { -// OPT.get().expect("Global config not initialized") -// } diff --git a/rustfs/src/console.rs b/rustfs/src/console.rs index 8ed25f573..c21da20d6 100644 --- a/rustfs/src/console.rs +++ b/rustfs/src/console.rs @@ -1,4 +1,4 @@ -use crate::config::{RUSTFS_TLS_CERT, RUSTFS_TLS_KEY}; +use crate::config::{self, RUSTFS_TLS_CERT, RUSTFS_TLS_KEY}; use crate::license::get_license; use axum::{ body::Body, @@ -185,38 +185,44 @@ fn is_private_ip(ip: std::net::IpAddr) -> bool { #[allow(clippy::const_is_empty)] #[instrument(fields(host))] async fn config_handler(Host(host): Host) -> impl IntoResponse { - let host_with_port = if host.contains(':') { - host.clone() - } else { - format!("{}:80", host) - }; - // 将当前配置复制一份 let mut cfg = CONSOLE_CONFIG.get().unwrap().clone(); - // 尝试解析为 socket address,但不强制要求一定要是 IP 地址 - let socket_addr = host_with_port.to_socket_addrs().ok().and_then(|mut addrs| addrs.next()); - debug!("axum Using host with port: {}, Socket address: {:?}", host_with_port, socket_addr); - let url = match socket_addr { - Some(addr) if addr.ip().is_ipv4() => { - let ipv4 = addr.ip().to_string(); - // 如果是私有 IP、环回地址或未指定地址,保留原始域名 - if is_private_ip(addr.ip()) || addr.ip().is_loopback() || addr.ip().is_unspecified() { + // 如果指定入口, 直接使用 + let url = if let Some(endpoint) = &config::get_config().console_fs_endpoint { + debug!("axum Using rustfs endpoint address: {}", endpoint); + endpoint.clone() + } else { + let host_with_port = if host.contains(':') { + host.clone() + } else { + format!("{}:80", host) + }; + // 尝试解析为 socket address,但不强制要求一定要是 IP 地址 + let socket_addr = host_with_port.to_socket_addrs().ok().and_then(|mut addrs| addrs.next()); + debug!("axum Using host with port: {}, Socket address: {:?}", host_with_port, socket_addr); + match socket_addr { + Some(addr) if addr.ip().is_ipv4() => { + let ipv4 = addr.ip().to_string(); + // 如果是私有 IP、环回地址或未指定地址,保留原始域名 + if is_private_ip(addr.ip()) || addr.ip().is_loopback() || addr.ip().is_unspecified() { + let (host, _) = host_with_port.split_once(':').unwrap_or((&host, "80")); + debug!("axum Using private IPv4 address: {}", host); + format!("http://{}:{}", host, cfg.port) + } else { + debug!("axum Using public IPv4 address"); + format!("http://{}:{}", ipv4, cfg.port) + } + } + _ => { + // 如果不是有效的 IPv4 地址,保留原始域名 let (host, _) = host_with_port.split_once(':').unwrap_or((&host, "80")); - debug!("axum Using private IPv4 address: {}", host); + debug!("axum Using domain address: {}", host); format!("http://{}:{}", host, cfg.port) - } else { - debug!("axum Using public IPv4 address"); - format!("http://{}:{}", ipv4, cfg.port) } } - _ => { - // 如果不是有效的 IPv4 地址,保留原始域名 - let (host, _) = host_with_port.split_once(':').unwrap_or((&host, "80")); - debug!("axum Using domain address: {}", host); - format!("http://{}:{}", host, cfg.port) - } }; + cfg.api.base_url = format!("{}{}", url, RUSTFS_ADMIN_PREFIX); cfg.s3.endpoint = url; @@ -329,3 +335,4 @@ async fn shutdown_signal() { }, } } +