cache license

This commit is contained in:
weisd
2025-04-08 17:46:46 +08:00
committed by weisd
parent 885944802a
commit d29bfcca9a
5 changed files with 57 additions and 37 deletions
+1 -1
View File
@@ -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时间戳)
+10 -12
View File
@@ -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<String>,
}
lazy_static::lazy_static! {
pub static ref OPT: OnceLock<Opt> = OnceLock::new();
}
// lazy_static::lazy_static! {
// pub static ref OPT: OnceLock<Opt> = 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")
// }
+2 -14
View File
@@ -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")
+40 -9
View File
@@ -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<Token> = OnceLock::new();
}
pub fn init_license(license: Option<String>) {
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<Token> {
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()));
}
+4 -1
View File
@@ -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));