fix tls configs error

This commit is contained in:
loverustfs
2025-04-13 19:53:29 +08:00
parent c651bea903
commit 3940ae2d69
2 changed files with 58 additions and 49 deletions
+26 -24
View File
@@ -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<String>,
@@ -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<String>,
#[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<String>,
/// 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<String>,
}
// lazy_static::lazy_static! {
// pub static ref OPT: OnceLock<Opt> = OnceLock::new();
// }
lazy_static::lazy_static! {
pub(crate) static ref OPT: OnceLock<Opt> = 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")
// }
+32 -25
View File
@@ -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() {
},
}
}