2026-03-10 15:33:06 +00:00
|
|
|
pub mod vfs;
|
|
|
|
|
pub mod security;
|
|
|
|
|
pub mod server;
|
2026-03-10 15:52:16 +00:00
|
|
|
pub mod metrics;
|
2026-03-10 15:33:06 +00:00
|
|
|
|
2026-03-10 15:52:16 +00:00
|
|
|
use std::net::SocketAddr;
|
2026-03-10 15:33:06 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::sync::Arc;
|
2026-03-10 15:52:16 +00:00
|
|
|
use axum::{Router, routing::get};
|
2026-03-10 15:33:06 +00:00
|
|
|
use clap::Parser;
|
2026-03-10 16:20:19 +00:00
|
|
|
use tonic::transport::{Identity, Server, ServerTlsConfig};
|
2026-03-10 15:33:06 +00:00
|
|
|
use vfs::local::LocalVfs;
|
|
|
|
|
use furumi_common::proto::remote_file_system_server::RemoteFileSystemServer;
|
|
|
|
|
use server::RemoteFileSystemImpl;
|
|
|
|
|
use security::AuthInterceptor;
|
|
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
2026-03-10 16:20:19 +00:00
|
|
|
#[command(version, about = "Furumi-ng: remote filesystem server over encrypted gRPC")]
|
2026-03-10 15:33:06 +00:00
|
|
|
struct Args {
|
2026-03-10 15:52:16 +00:00
|
|
|
/// IP address and port to bind the gRPC server to
|
2026-03-10 16:20:19 +00:00
|
|
|
#[arg(short, long, env = "FURUMI_BIND", default_value = "0.0.0.0:50051")]
|
2026-03-10 15:33:06 +00:00
|
|
|
bind: String,
|
|
|
|
|
|
|
|
|
|
/// Document root directory to expose via VFS
|
|
|
|
|
#[arg(short, long, env = "FURUMI_ROOT", default_value = ".")]
|
|
|
|
|
root: PathBuf,
|
|
|
|
|
|
|
|
|
|
/// Authentication Bearer token (leave empty to disable auth)
|
|
|
|
|
#[arg(short, long, env = "FURUMI_TOKEN", default_value = "")]
|
|
|
|
|
token: String,
|
2026-03-10 15:52:16 +00:00
|
|
|
|
|
|
|
|
/// IP address and port for the Prometheus metrics HTTP endpoint
|
|
|
|
|
#[arg(long, env = "FURUMI_METRICS_BIND", default_value = "0.0.0.0:9090")]
|
|
|
|
|
metrics_bind: String,
|
2026-03-10 16:20:19 +00:00
|
|
|
|
|
|
|
|
/// Disable TLS encryption (not recommended, use only for debugging)
|
|
|
|
|
#[arg(long, default_value_t = false)]
|
|
|
|
|
no_tls: bool,
|
2026-03-10 15:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn metrics_handler() -> String {
|
|
|
|
|
metrics::render_metrics()
|
2026-03-10 15:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2026-03-10 16:20:19 +00:00
|
|
|
// Install ring as the default crypto provider for rustls (must be before any TLS usage)
|
|
|
|
|
rustls::crypto::ring::default_provider()
|
|
|
|
|
.install_default()
|
|
|
|
|
.expect("Failed to install rustls crypto provider");
|
|
|
|
|
|
2026-03-10 15:33:06 +00:00
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
|
|
|
|
|
let args = Args::parse();
|
2026-03-10 15:52:16 +00:00
|
|
|
let addr: SocketAddr = args.bind.parse().unwrap_or_else(|e| {
|
|
|
|
|
eprintln!("Error: Invalid bind address '{}': {}", args.bind, e);
|
2026-03-10 16:20:19 +00:00
|
|
|
eprintln!(" Expected format: IP:PORT (e.g. 0.0.0.0:50051)");
|
2026-03-10 15:52:16 +00:00
|
|
|
std::process::exit(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let metrics_addr: SocketAddr = args.metrics_bind.parse().unwrap_or_else(|e| {
|
|
|
|
|
eprintln!("Error: Invalid metrics bind address '{}': {}", args.metrics_bind, e);
|
|
|
|
|
eprintln!(" Expected format: IP:PORT (e.g. 0.0.0.0:9090)");
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
});
|
2026-03-10 15:33:06 +00:00
|
|
|
|
|
|
|
|
// Resolve the document root to an absolute path for safety and clarity
|
|
|
|
|
let root_path = std::fs::canonicalize(&args.root)
|
|
|
|
|
.unwrap_or_else(|_| args.root.clone());
|
|
|
|
|
|
|
|
|
|
if !root_path.exists() || !root_path.is_dir() {
|
|
|
|
|
eprintln!("Error: Root path {:?} does not exist or is not a directory", root_path);
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let vfs = Arc::new(LocalVfs::new(&root_path));
|
|
|
|
|
let remote_fs = RemoteFileSystemImpl::new(vfs);
|
|
|
|
|
let auth = AuthInterceptor::new(args.token.clone());
|
|
|
|
|
let svc = RemoteFileSystemServer::with_interceptor(remote_fs, auth.clone());
|
|
|
|
|
|
2026-03-10 16:20:19 +00:00
|
|
|
// Print startup info
|
2026-03-10 15:33:06 +00:00
|
|
|
println!("Furumi-ng Server listening on {}", addr);
|
2026-03-10 16:20:19 +00:00
|
|
|
if args.no_tls {
|
|
|
|
|
println!("WARNING: TLS is DISABLED — traffic is unencrypted");
|
|
|
|
|
} else {
|
|
|
|
|
println!("TLS: enabled (auto-generated self-signed certificate)");
|
|
|
|
|
}
|
2026-03-10 15:33:06 +00:00
|
|
|
if args.token.is_empty() {
|
|
|
|
|
println!("WARNING: Authentication is DISABLED");
|
|
|
|
|
} else {
|
2026-03-10 16:20:19 +00:00
|
|
|
println!("Authentication: enabled (Bearer token)");
|
2026-03-10 15:33:06 +00:00
|
|
|
}
|
|
|
|
|
println!("Document Root: {:?}", root_path);
|
2026-03-10 16:20:19 +00:00
|
|
|
println!("Metrics: http://{}/metrics", metrics_addr);
|
2026-03-10 15:52:16 +00:00
|
|
|
|
|
|
|
|
// Spawn the Prometheus metrics HTTP server on a separate port
|
|
|
|
|
let metrics_app = Router::new().route("/metrics", get(metrics_handler));
|
|
|
|
|
let metrics_listener = tokio::net::TcpListener::bind(metrics_addr).await?;
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
axum::serve(metrics_listener, metrics_app).await.unwrap();
|
|
|
|
|
});
|
2026-03-10 15:33:06 +00:00
|
|
|
|
2026-03-10 16:20:19 +00:00
|
|
|
let mut builder = Server::builder()
|
2026-03-10 15:33:06 +00:00
|
|
|
.tcp_keepalive(Some(std::time::Duration::from_secs(60)))
|
2026-03-10 16:20:19 +00:00
|
|
|
.http2_keepalive_interval(Some(std::time::Duration::from_secs(60)));
|
|
|
|
|
|
|
|
|
|
if !args.no_tls {
|
|
|
|
|
let cert_pair = rcgen::generate_simple_self_signed(vec![
|
|
|
|
|
"localhost".to_string(),
|
|
|
|
|
"furumi-ng".to_string(),
|
|
|
|
|
])
|
|
|
|
|
.expect("Failed to generate self-signed certificate");
|
|
|
|
|
|
|
|
|
|
let cert_pem = cert_pair.cert.pem();
|
|
|
|
|
let key_pem = cert_pair.signing_key.serialize_pem();
|
|
|
|
|
|
|
|
|
|
let identity = Identity::from_pem(cert_pem, key_pem);
|
|
|
|
|
let tls_config = ServerTlsConfig::new().identity(identity);
|
|
|
|
|
builder = builder.tls_config(tls_config)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder
|
2026-03-10 15:33:06 +00:00
|
|
|
.add_service(svc)
|
|
|
|
|
.serve(addr)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|