From d29bfcca9a7b594b44da9d576452915048ee61c0 Mon Sep 17 00:00:00 2001 From: weisd Date: Tue, 8 Apr 2025 17:46:46 +0800 Subject: [PATCH] cache license --- appauth/src/token.rs | 2 +- rustfs/src/config/mod.rs | 22 ++++++++---------- rustfs/src/console.rs | 16 ++----------- rustfs/src/license.rs | 49 ++++++++++++++++++++++++++++++++-------- rustfs/src/main.rs | 5 +++- 5 files changed, 57 insertions(+), 37 deletions(-) diff --git a/appauth/src/token.rs b/appauth/src/token.rs index 6be037dd5..f18ae57f7 100644 --- a/appauth/src/token.rs +++ b/appauth/src/token.rs @@ -6,7 +6,7 @@ use rsa::{ }; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Default, Clone)] pub struct Token { pub name: String, // 应用ID pub expired: u64, // 到期时间 (UNIX时间戳) diff --git a/rustfs/src/config/mod.rs b/rustfs/src/config/mod.rs index 673cffc80..54dbc8d03 100644 --- a/rustfs/src/config/mod.rs +++ b/rustfs/src/config/mod.rs @@ -2,8 +2,6 @@ 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 @@ -106,15 +104,15 @@ pub struct Opt { pub license: Option, } -lazy_static::lazy_static! { - pub static ref OPT: OnceLock = OnceLock::new(); -} +// lazy_static::lazy_static! { +// pub static ref OPT: OnceLock = OnceLock::new(); +// } -pub fn init_config() { - let opt = Opt::parse(); - OPT.set(opt).expect("Failed to set global config"); -} +// pub fn init_config() { +// let opt = Opt::parse(); +// 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") +// } diff --git a/rustfs/src/console.rs b/rustfs/src/console.rs index 5ed9dc37e..bf50a4239 100644 --- a/rustfs/src/console.rs +++ b/rustfs/src/console.rs @@ -1,4 +1,4 @@ -use crate::config; +use crate::license::get_license; use axum::{ body::Body, extract::Host, @@ -160,19 +160,7 @@ pub(crate) fn init_console_cfg(local_ip: Ipv4Addr, port: u16) { // } async fn license_handler() -> impl IntoResponse { - let license = config::get_config() - .license - .as_ref() - .map(|license| { - if license.is_empty() { - return None; - } - match appauth::token::parse_license(license) { - Ok(token) => Some(token), - Err(_) => None, - } - }) - .unwrap_or_default(); + let license = get_license().unwrap_or_default(); Response::builder() .header("content-type", "application/json") diff --git a/rustfs/src/license.rs b/rustfs/src/license.rs index 752c10457..58daa1955 100644 --- a/rustfs/src/license.rs +++ b/rustfs/src/license.rs @@ -1,26 +1,57 @@ -use crate::config; +use appauth::token::Token; use common::error::{Error, Result}; +use std::sync::OnceLock; use std::time::SystemTime; use std::time::UNIX_EPOCH; use tracing::error; use tracing::info; +lazy_static::lazy_static! { + static ref LICENSE: OnceLock = OnceLock::new(); +} + +pub fn init_license(license: Option) { + if license.is_none() { + error!("License is None"); + return; + } + let license = license.unwrap(); + let token = appauth::token::parse_license(&license).unwrap_or_default(); + + LICENSE.set(token).unwrap_or_else(|_| { + error!("Failed to set license"); + }); +} + +pub fn get_license() -> Option { + LICENSE.get().cloned() +} + pub fn license_check() -> Result<()> { - let inval_license = config::get_config().license.as_ref().map(|license| { - if license.is_empty() { - error!("License is empty"); - return Err(Error::from_string("Incorrect license, please contact RustFS.".to_string())); - } - let token = appauth::token::parse_license(license)?; - if token.expired < SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() { + let inval_license = LICENSE.get().map(|token| { + if token.expired < SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() { error!("License expired"); return Err(Error::from_string("Incorrect license, please contact RustFS.".to_string())); } - info!("License is valid ! expired at {}", token.expired); Ok(()) }); + // let inval_license = config::get_config().license.as_ref().map(|license| { + // if license.is_empty() { + // error!("License is empty"); + // return Err(Error::from_string("Incorrect license, please contact RustFS.".to_string())); + // } + // let token = appauth::token::parse_license(license)?; + // if token.expired < SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() { + // error!("License expired"); + // return Err(Error::from_string("Incorrect license, please contact RustFS.".to_string())); + // } + + // info!("License is valid ! expired at {}", token.expired); + // Ok(()) + // }); + if inval_license.is_none() || inval_license.is_some_and(|v| v.is_err()) { return Err(Error::from_string("Incorrect license, please contact RustFS.".to_string())); } diff --git a/rustfs/src/main.rs b/rustfs/src/main.rs index 572665d2b..4481525bb 100644 --- a/rustfs/src/main.rs +++ b/rustfs/src/main.rs @@ -37,6 +37,7 @@ use hyper_util::{ service::TowerToHyperService, }; use iam::init_iam_sys; +use license::init_license; use protos::proto_gen::node_service::node_service_server::NodeServiceServer; use rustfs_obs::{init_obs, load_config, set_global_guard, InitLogStatus}; use rustls::ServerConfig; @@ -94,11 +95,13 @@ fn print_server_info() { #[tokio::main] async fn main() -> Result<()> { - config::init_config(); + // config::init_config(); // Parse the obtained parameters let opt = config::Opt::parse(); + init_license(opt.license.clone()); + // Load the configuration file let config = load_config(Some(opt.clone().obs_config));