mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-25 13:36:50 +00:00
fix tls configs error
This commit is contained in:
+26
-24
@@ -2,6 +2,7 @@ use clap::Parser;
|
|||||||
use const_str::concat;
|
use const_str::concat;
|
||||||
use ecstore::global::DEFAULT_PORT;
|
use ecstore::global::DEFAULT_PORT;
|
||||||
use std::string::ToString;
|
use std::string::ToString;
|
||||||
|
use std::sync::OnceLock;
|
||||||
shadow_rs::shadow!(build);
|
shadow_rs::shadow!(build);
|
||||||
|
|
||||||
/// Default Access Key
|
/// Default Access Key
|
||||||
@@ -47,16 +48,16 @@ const SHORT_VERSION: &str = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const LONG_VERSION: &str = concat!(
|
const LONG_VERSION: &str = concat!(
|
||||||
concat!(SHORT_VERSION, "\n"),
|
concat!(SHORT_VERSION, "\n"),
|
||||||
concat!("build time : ", build::BUILD_TIME, "\n"),
|
concat!("build time : ", build::BUILD_TIME, "\n"),
|
||||||
concat!("build profile: ", build::BUILD_RUST_CHANNEL, "\n"),
|
concat!("build profile: ", build::BUILD_RUST_CHANNEL, "\n"),
|
||||||
concat!("build os : ", build::BUILD_OS, "\n"),
|
concat!("build os : ", build::BUILD_OS, "\n"),
|
||||||
concat!("rust version : ", build::RUST_VERSION, "\n"),
|
concat!("rust version : ", build::RUST_VERSION, "\n"),
|
||||||
concat!("rust channel : ", build::RUST_CHANNEL, "\n"),
|
concat!("rust channel : ", build::RUST_CHANNEL, "\n"),
|
||||||
concat!("git branch : ", build::BRANCH, "\n"),
|
concat!("git branch : ", build::BRANCH, "\n"),
|
||||||
concat!("git commit : ", build::COMMIT_HASH, "\n"),
|
concat!("git commit : ", build::COMMIT_HASH, "\n"),
|
||||||
concat!("git tag : ", build::TAG, "\n"),
|
concat!("git tag : ", build::TAG, "\n"),
|
||||||
concat!("git status :\n", build::GIT_STATUS_FILE),
|
concat!("git status :\n", build::GIT_STATUS_FILE),
|
||||||
);
|
);
|
||||||
|
|
||||||
#[derive(Debug, Parser, Clone)]
|
#[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")]
|
#[arg(long, default_value_t = format!("0.0.0.0:{}", DEFAULT_PORT), env = "RUSTFS_ADDRESS")]
|
||||||
pub address: String,
|
pub address: String,
|
||||||
|
|
||||||
|
/// Domain name used for virtual-hosted-style requests.
|
||||||
#[arg(long, env = "RUSTFS_SERVER_DOMAINS")]
|
#[arg(long, env = "RUSTFS_SERVER_DOMAINS")]
|
||||||
pub server_domains: Vec<String>,
|
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")]
|
#[arg(long, default_value_t = DEFAULT_SECRET_KEY.to_string(), env = "RUSTFS_SECRET_KEY")]
|
||||||
pub secret_key: String,
|
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")]
|
#[arg(long, default_value_t = false, env = "RUSTFS_CONSOLE_ENABLE")]
|
||||||
pub console_enable: bool,
|
pub console_enable: bool,
|
||||||
|
|
||||||
#[arg(long, default_value_t = format!("127.0.0.1:{}", 9002), env = "RUSTFS_CONSOLE_ADDRESS")]
|
#[arg(long, default_value_t = format!("127.0.0.1:{}", 9002), env = "RUSTFS_CONSOLE_ADDRESS")]
|
||||||
pub console_address: String,
|
pub console_address: String,
|
||||||
|
|
||||||
|
/// rustfs endpoint for console
|
||||||
|
#[arg(long, env = "RUSTFS_CONSOLE_FS_ENDPOINT")]
|
||||||
|
pub console_fs_endpoint: Option<String>,
|
||||||
|
|
||||||
/// Observability configuration file
|
/// Observability configuration file
|
||||||
/// Default value: config/obs.toml
|
/// Default value: config/obs.toml
|
||||||
#[arg(long, default_value_t = DEFAULT_OBS_CONFIG.to_string(), env = "RUSTFS_OBS_CONFIG")]
|
#[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>,
|
pub license: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
// pub static ref OPT: OnceLock<Opt> = OnceLock::new();
|
pub(crate) static ref OPT: OnceLock<Opt> = OnceLock::new();
|
||||||
// }
|
}
|
||||||
|
|
||||||
// pub fn init_config() {
|
pub fn init_config(opt: Opt) {
|
||||||
// let opt = Opt::parse();
|
OPT.set(opt).expect("Failed to set global config");
|
||||||
// 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
@@ -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 crate::license::get_license;
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
@@ -185,38 +185,44 @@ fn is_private_ip(ip: std::net::IpAddr) -> bool {
|
|||||||
#[allow(clippy::const_is_empty)]
|
#[allow(clippy::const_is_empty)]
|
||||||
#[instrument(fields(host))]
|
#[instrument(fields(host))]
|
||||||
async fn config_handler(Host(host): Host) -> impl IntoResponse {
|
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();
|
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());
|
let url = if let Some(endpoint) = &config::get_config().console_fs_endpoint {
|
||||||
debug!("axum Using host with port: {}, Socket address: {:?}", host_with_port, socket_addr);
|
debug!("axum Using rustfs endpoint address: {}", endpoint);
|
||||||
let url = match socket_addr {
|
endpoint.clone()
|
||||||
Some(addr) if addr.ip().is_ipv4() => {
|
} else {
|
||||||
let ipv4 = addr.ip().to_string();
|
let host_with_port = if host.contains(':') {
|
||||||
// 如果是私有 IP、环回地址或未指定地址,保留原始域名
|
host.clone()
|
||||||
if is_private_ip(addr.ip()) || addr.ip().is_loopback() || addr.ip().is_unspecified() {
|
} 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"));
|
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)
|
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.api.base_url = format!("{}{}", url, RUSTFS_ADMIN_PREFIX);
|
||||||
cfg.s3.endpoint = url;
|
cfg.s3.endpoint = url;
|
||||||
|
|
||||||
@@ -329,3 +335,4 @@ async fn shutdown_signal() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user