Files
Stirling-PDF/src/main/java/stirling/software/SPDF/EE/LicenseKeyChecker.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
3.4 KiB
Java
Raw Normal View History

2024-10-14 22:34:41 +01:00
package stirling.software.SPDF.EE;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
2024-10-14 22:34:41 +01:00
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.EE.KeygenLicenseVerifier.License;
2024-10-14 22:34:41 +01:00
import stirling.software.SPDF.model.ApplicationProperties;
import stirling.software.SPDF.utils.GeneralUtils;
@Component
@Slf4j
public class LicenseKeyChecker {
private static final String FILE_PREFIX = "file:";
2024-10-14 22:34:41 +01:00
private final KeygenLicenseVerifier licenseService;
private final ApplicationProperties applicationProperties;
private License premiumEnabledResult = License.NORMAL;
2024-10-14 22:34:41 +01:00
public LicenseKeyChecker(
KeygenLicenseVerifier licenseService, ApplicationProperties applicationProperties) {
this.licenseService = licenseService;
this.applicationProperties = applicationProperties;
this.checkLicense();
2024-10-14 22:34:41 +01:00
}
2024-12-09 20:41:13 +00:00
@Scheduled(initialDelay = 604800000, fixedRate = 604800000) // 7 days in milliseconds
2024-10-14 22:34:41 +01:00
public void checkLicensePeriodically() {
checkLicense();
}
private void checkLicense() {
if (!applicationProperties.getPremium().isEnabled()) {
premiumEnabledResult = License.NORMAL;
2024-10-14 22:34:41 +01:00
} else {
String licenseKey = getLicenseKeyContent(applicationProperties.getPremium().getKey());
if (licenseKey != null) {
premiumEnabledResult = licenseService.verifyLicense(licenseKey);
if (License.ENTERPRISE == premiumEnabledResult) {
log.info("License key is Enterprise.");
} else if (License.PRO == premiumEnabledResult) {
log.info("License key is Pro.");
} else {
log.info("License key is invalid, defaulting to non pro license.");
}
2024-10-14 22:34:41 +01:00
} else {
log.error("Failed to obtain license key content.");
premiumEnabledResult = License.NORMAL;
}
}
}
private String getLicenseKeyContent(String keyOrFilePath) {
if (keyOrFilePath == null || keyOrFilePath.trim().isEmpty()) {
log.error("License key is not specified");
return null;
}
// Check if it's a file reference
if (keyOrFilePath.startsWith(FILE_PREFIX)) {
String filePath = keyOrFilePath.substring(FILE_PREFIX.length());
try {
Path path = Paths.get(filePath);
if (!Files.exists(path)) {
log.error("License file does not exist: {}", filePath);
return null;
}
log.info("Reading license from file: {}", filePath);
return Files.readString(path);
} catch (IOException e) {
log.error("Failed to read license file: {}", e.getMessage());
return null;
2024-10-14 22:34:41 +01:00
}
}
// It's a direct license key
return keyOrFilePath;
2024-10-14 22:34:41 +01:00
}
public void updateLicenseKey(String newKey) throws IOException {
applicationProperties.getPremium().setKey(newKey);
GeneralUtils.saveKeyToSettings("EnterpriseEdition.key", newKey);
2024-10-14 22:34:41 +01:00
checkLicense();
}
public License getPremiumLicenseEnabledResult() {
return premiumEnabledResult;
2024-10-14 22:34:41 +01:00
}
}