mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-31 09:18:28 +00:00
fix config_handler
This commit is contained in:
@@ -2,7 +2,6 @@ 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
|
||||||
@@ -106,15 +105,14 @@ pub struct Opt {
|
|||||||
pub license: Option<String>,
|
pub license: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
// lazy_static::lazy_static! {
|
||||||
pub(crate) static ref OPT: OnceLock<Opt> = OnceLock::new();
|
// pub(crate) static ref OPT: OnceLock<Opt> = OnceLock::new();
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub fn init_config(opt: Opt) {
|
// pub fn init_config(opt: Opt) {
|
||||||
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")
|
||||||
|
// }
|
||||||
|
|||||||
+54
-39
@@ -1,4 +1,4 @@
|
|||||||
use crate::config::{self, RUSTFS_TLS_CERT, RUSTFS_TLS_KEY};
|
use crate::config::{RUSTFS_TLS_CERT, RUSTFS_TLS_KEY};
|
||||||
use crate::license::get_license;
|
use crate::license::get_license;
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
@@ -8,12 +8,14 @@ use axum::{
|
|||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
use axum_extra::extract::Host;
|
use axum_extra::extract::Host;
|
||||||
|
|
||||||
use axum_server::tls_rustls::RustlsConfig;
|
use axum_server::tls_rustls::RustlsConfig;
|
||||||
|
use http::Uri;
|
||||||
use mime_guess::from_path;
|
use mime_guess::from_path;
|
||||||
use rust_embed::RustEmbed;
|
use rust_embed::RustEmbed;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use shadow_rs::shadow;
|
use shadow_rs::shadow;
|
||||||
use std::net::{Ipv4Addr, SocketAddr, ToSocketAddrs};
|
use std::net::{Ipv4Addr, SocketAddr};
|
||||||
use std::sync::OnceLock;
|
use std::sync::OnceLock;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::signal;
|
use tokio::signal;
|
||||||
@@ -167,7 +169,7 @@ async fn license_handler() -> impl IntoResponse {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_private_ip(ip: std::net::IpAddr) -> bool {
|
fn _is_private_ip(ip: std::net::IpAddr) -> bool {
|
||||||
match ip {
|
match ip {
|
||||||
std::net::IpAddr::V4(ip) => {
|
std::net::IpAddr::V4(ip) => {
|
||||||
let octets = ip.octets();
|
let octets = ip.octets();
|
||||||
@@ -184,44 +186,58 @@ 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(uri: Uri, Host(host): Host) -> impl IntoResponse {
|
||||||
|
let scheme = uri.scheme().map(|s| s.as_str()).unwrap_or("http");
|
||||||
|
|
||||||
|
// 从 uri 中获取 host,如果没有则使用 Host extractor 的值
|
||||||
|
let host = uri.host().unwrap_or(host.as_str());
|
||||||
|
|
||||||
|
let host = if host.contains(':') {
|
||||||
|
let (host, _) = host.split_once(':').unwrap_or((host, "80"));
|
||||||
|
host
|
||||||
|
} else {
|
||||||
|
host
|
||||||
|
};
|
||||||
|
|
||||||
// 将当前配置复制一份
|
// 将当前配置复制一份
|
||||||
let mut cfg = CONSOLE_CONFIG.get().unwrap().clone();
|
let mut cfg = CONSOLE_CONFIG.get().unwrap().clone();
|
||||||
|
|
||||||
// 如果指定入口, 直接使用
|
let url = format!("{}://{}:{}", scheme, host, cfg.port);
|
||||||
let url = if let Some(endpoint) = &config::get_config().console_fs_endpoint {
|
|
||||||
debug!("axum Using rustfs endpoint address: {}", endpoint);
|
// // 如果指定入口, 直接使用
|
||||||
endpoint.clone()
|
// let url = if let Some(endpoint) = &config::get_config().console_fs_endpoint {
|
||||||
} else {
|
// debug!("axum Using rustfs endpoint address: {}", endpoint);
|
||||||
let host_with_port = if host.contains(':') {
|
// endpoint.clone()
|
||||||
host.clone()
|
// } else {
|
||||||
} else {
|
// let host_with_port = if host.contains(':') {
|
||||||
format!("{}:80", host)
|
// host.clone()
|
||||||
};
|
// } else {
|
||||||
// 尝试解析为 socket address,但不强制要求一定要是 IP 地址
|
// format!("{}:80", host)
|
||||||
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);
|
// // 尝试解析为 socket address,但不强制要求一定要是 IP 地址
|
||||||
match socket_addr {
|
// let socket_addr = host_with_port.to_socket_addrs().ok().and_then(|mut addrs| addrs.next());
|
||||||
Some(addr) if addr.ip().is_ipv4() => {
|
// debug!("axum Using host with port: {}, Socket address: {:?}", host_with_port, socket_addr);
|
||||||
let ipv4 = addr.ip().to_string();
|
// match socket_addr {
|
||||||
// 如果是私有 IP、环回地址或未指定地址,保留原始域名
|
// Some(addr) if addr.ip().is_ipv4() => {
|
||||||
if is_private_ip(addr.ip()) || addr.ip().is_loopback() || addr.ip().is_unspecified() {
|
// let ipv4 = addr.ip().to_string();
|
||||||
let (host, _) = host_with_port.split_once(':').unwrap_or((&host, "80"));
|
// // 如果是私有 IP、环回地址或未指定地址,保留原始域名
|
||||||
debug!("axum Using private IPv4 address: {}", host);
|
// if is_private_ip(addr.ip()) || addr.ip().is_loopback() || addr.ip().is_unspecified() {
|
||||||
format!("http://{}:{}", host, cfg.port)
|
// let (host, _) = host_with_port.split_once(':').unwrap_or((&host, "80"));
|
||||||
} else {
|
// debug!("axum Using private IPv4 address: {}", host);
|
||||||
debug!("axum Using public IPv4 address");
|
// format!("http://{}:{}", host, cfg.port)
|
||||||
format!("http://{}:{}", ipv4, 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);
|
// // 如果不是有效的 IPv4 地址,保留原始域名
|
||||||
format!("http://{}:{}", host, cfg.port)
|
// 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;
|
||||||
@@ -335,4 +351,3 @@ async fn shutdown_signal() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -77,11 +77,11 @@ fn print_server_info() {
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
// config::init_config();
|
|
||||||
|
|
||||||
// Parse the obtained parameters
|
// Parse the obtained parameters
|
||||||
let opt = config::Opt::parse();
|
let opt = config::Opt::parse();
|
||||||
|
|
||||||
|
// config::init_config(opt.clone());
|
||||||
|
|
||||||
init_license(opt.license.clone());
|
init_license(opt.license.clone());
|
||||||
|
|
||||||
// Load the configuration file
|
// Load the configuration file
|
||||||
|
|||||||
Reference in New Issue
Block a user