Support more license valid data, and the test license

This commit is contained in:
Herbert Wolverson 2023-04-07 16:35:08 +00:00
parent 2cb68ce06b
commit ac97eb450e
4 changed files with 31 additions and 8 deletions

View File

@ -68,7 +68,10 @@ async fn check_license(
log::info!("Checking license from {address:?}, key: {}", request.key);
if request.key == "test" {
log::info!("License is valid");
Ok(LicenseReply::Valid)
Ok(LicenseReply::Valid {
expiry: 0, // Temporary value
stats_host: "127.0.0.1:9127".to_string(), // Also temporary
})
} else {
log::info!("License is denied");
Ok(LicenseReply::Denied)

View File

@ -83,7 +83,12 @@ pub enum LicenseReply {
/// The license is denied
Denied,
/// The license is valid
Valid,
Valid {
/// When does the license expire?
expiry: u64,
/// Address to which statistics should be submitted
stats_host: String,
},
}
/// Errors that can occur when checking licenses

View File

@ -11,12 +11,17 @@ struct LicenseStatus {
last_check: u64,
}
#[derive(Default, Clone, Copy, PartialEq, Debug)]
#[derive(Default, Clone, PartialEq, Debug)]
pub(crate) enum LicenseState {
#[default]
Unknown,
Denied,
Valid,
Valid {
/// When does the license expire?
expiry: u64,
/// Host to which to send stats
stats_host: String,
},
}
static LICENSE_STATUS: Lazy<RwLock<LicenseStatus>> =
@ -35,10 +40,17 @@ pub(crate) async fn get_license_status() -> LicenseState {
LicenseState::Unknown
}
const MISERLY_NO_KEY: &str = "IDontSupportDevelopersAndShouldFeelBad";
async fn check_license(unix_time: u64) -> LicenseState {
if let Ok(cfg) = EtcLqos::load() {
if let Some(cfg) = cfg.long_term_stats {
if let Some(key) = cfg.license_key {
if key == MISERLY_NO_KEY {
log::warn!("You are using the self-hosting license key. We'd be happy to sell you a real one.");
return LicenseState::Valid { expiry: 0, stats_host: "127.0.0.1:9127".to_string() }
}
let mut lock = LICENSE_STATUS.write().await;
lock.last_check = unix_time;
lock.key = key.clone();
@ -49,12 +61,14 @@ async fn check_license(unix_time: u64) -> LicenseState {
log::warn!("License is in state: DENIED.");
lock.state = LicenseState::Denied;
}
LicenseReply::Valid => {
LicenseReply::Valid{expiry, stats_host} => {
log::info!("License is in state: VALID.");
lock.state = LicenseState::Valid;
lock.state = LicenseState::Valid{
expiry, stats_host
};
}
}
return lock.state;
return lock.state.clone();
}
Err(e) => {
log::error!("Error checking licensing server");

View File

@ -16,8 +16,9 @@ pub(crate) async fn new_submission(data: StatsSubmission) {
LicenseState::Denied => {
log::error!("Your license is invalid. Please contact support.");
}
LicenseState::Valid => {
LicenseState::Valid{ expiry, stats_host } => {
// TODO: Send to server
println!("Send stats to {stats_host} before {expiry}");
}
}
}