diff --git a/Cargo.lock b/Cargo.lock index f6a2267af..e6788f39e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1156,6 +1156,7 @@ dependencies = [ "http", "hyper-util", "mime", + "netif", "s3s", "time", "tokio", diff --git a/rustfs/Cargo.toml b/rustfs/Cargo.toml index 6c1849cff..980281833 100644 --- a/rustfs/Cargo.toml +++ b/rustfs/Cargo.toml @@ -35,4 +35,5 @@ hyper-util = { version = "0.1.5", features = [ ] } mime = "0.3.17" transform-stream = "0.3.0" +netif = "0.1.6" # pin-utils = "0.1.0" diff --git a/rustfs/src/config/mod.rs b/rustfs/src/config/mod.rs index a9c1f40c7..1919351cb 100644 --- a/rustfs/src/config/mod.rs +++ b/rustfs/src/config/mod.rs @@ -4,6 +4,8 @@ use clap::Parser; /// /// Used if no port is specified. pub const DEFAULT_PORT: u16 = 9000; +pub const DEFAULT_ACCESS_KEY: &str = "rustfsadmin"; +pub const DEFAULT_SECRET_KEY: &str = "rustfsadmin"; #[derive(Debug, Parser)] pub struct Opt { diff --git a/rustfs/src/main.rs b/rustfs/src/main.rs index 16c1935c1..f2d6fce64 100644 --- a/rustfs/src/main.rs +++ b/rustfs/src/main.rs @@ -8,7 +8,7 @@ use hyper_util::{ server::conn::auto::Builder as ConnBuilder, }; use s3s::{auth::SimpleAuth, service::S3ServiceBuilder}; -use std::io::IsTerminal; +use std::{io::IsTerminal, net::SocketAddr, str::FromStr}; use tokio::net::TcpListener; use tracing::{debug, info}; use tracing_error::ErrorLayer; @@ -41,28 +41,57 @@ fn main() -> Result<()> { #[tokio::main] async fn run(opt: config::Opt) -> Result<()> { debug!("opt: {:?}", &opt); + + let listener = TcpListener::bind(opt.address.clone()).await?; + let local_addr: SocketAddr = listener.local_addr()?; + + let mut domain_name = { + netif::up()? + .map(|x| x.address().to_owned()) + // .filter(|v| v.is_ipv4() && !v.is_loopback() && !v.is_unspecified()) + .map(|v| format!("{}", v)) + .next() + .and_then(|ip| { + if let SocketAddr::V4(ipv4) = local_addr { + Some(format!("{}:{}", ip, ipv4.port())) + } else { + None + } + }) + }; + // Setup S3 service let service = { let mut b = S3ServiceBuilder::new(storage::ecfs::FS::new(opt.address.clone(), opt.volumes.clone()).await?); + let mut access_key = String::from_str(config::DEFAULT_ACCESS_KEY).unwrap(); + let mut secret_key = String::from_str(config::DEFAULT_SECRET_KEY).unwrap(); + // Enable authentication if let (Some(ak), Some(sk)) = (opt.access_key, opt.secret_key) { - b.set_auth(SimpleAuth::from_single(ak, sk)); - info!("authentication is enabled"); + access_key = ak; + secret_key = sk; } + info!("authentication is enabled {}, {}", &access_key, &secret_key); + b.set_auth(SimpleAuth::from_single(access_key, secret_key)); + // Enable parsing virtual-hosted-style requests - if let Some(domain_name) = opt.domain_name { - b.set_base_domain(domain_name); - info!("virtual-hosted-style requests are enabled"); + if let Some(dm) = opt.domain_name { + domain_name = Some(dm) + } + + if domain_name.is_some() { + info!( + "virtual-hosted-style requests are enabled use domain_name {}", + domain_name.as_ref().unwrap() + ); + b.set_base_domain(domain_name.unwrap()); } b.build() }; - let listener = TcpListener::bind(opt.address).await?; - let local_addr = listener.local_addr()?; - let hyper_service = service.into_shared(); let http_server = ConnBuilder::new(TokioExecutor::new()); diff --git a/scripts/run.sh b/scripts/run.sh index cafaf0bf1..f3a1c2c7a 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -14,9 +14,11 @@ if [ -z "$RUST_LOG" ]; then export RUST_LOG="rustfs=debug,ecstore=debug,s3s=debug" fi -cargo run \ - -- --access-key AKEXAMPLERUSTFS \ - --secret-key SKEXAMPLERUSTFS \ - --address 0.0.0.0:9010 \ - --domain-name 127.0.0.1:9010 \ +cargo run "$DATA_DIR" + # -- --access-key AKEXAMPLERUSTFS \ + # --secret-key SKEXAMPLERUSTFS \ + # --address 0.0.0.0:9010 \ + # --domain-name 127.0.0.1:9010 \ "$DATA_DIR" + +# cargo run "$DATA_DIR" \ No newline at end of file