2024-10-14 22:34:41 +01:00
|
|
|
package stirling.software.SPDF.EE;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2025-03-25 17:57:17 +00:00
|
|
|
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;
|
2025-02-23 13:36:21 +00:00
|
|
|
|
2025-03-27 12:42:45 +00:00
|
|
|
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 {
|
|
|
|
|
2025-03-25 17:57:17 +00:00
|
|
|
private static final String FILE_PREFIX = "file:";
|
|
|
|
|
2024-10-14 22:34:41 +01:00
|
|
|
private final KeygenLicenseVerifier licenseService;
|
|
|
|
|
|
|
|
private final ApplicationProperties applicationProperties;
|
|
|
|
|
2025-03-27 12:42:45 +00:00
|
|
|
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;
|
2024-11-29 15:11:59 +00:00
|
|
|
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() {
|
2025-03-25 17:57:17 +00:00
|
|
|
if (!applicationProperties.getPremium().isEnabled()) {
|
2025-03-27 12:42:45 +00:00
|
|
|
premiumEnabledResult = License.NORMAL;
|
2024-10-14 22:34:41 +01:00
|
|
|
} else {
|
2025-03-25 17:57:17 +00:00
|
|
|
String licenseKey = getLicenseKeyContent(applicationProperties.getPremium().getKey());
|
|
|
|
if (licenseKey != null) {
|
|
|
|
premiumEnabledResult = licenseService.verifyLicense(licenseKey);
|
2025-03-27 12:42:45 +00:00
|
|
|
if (License.ENTERPRISE == premiumEnabledResult) {
|
|
|
|
log.info("License key is Enterprise.");
|
|
|
|
} else if (License.PRO == premiumEnabledResult) {
|
|
|
|
log.info("License key is Pro.");
|
2025-03-25 17:57:17 +00:00
|
|
|
} else {
|
2025-03-27 12:42:45 +00:00
|
|
|
log.info("License key is invalid, defaulting to non pro license.");
|
2025-03-25 17:57:17 +00:00
|
|
|
}
|
2024-10-14 22:34:41 +01:00
|
|
|
} else {
|
2025-03-25 17:57:17 +00:00
|
|
|
log.error("Failed to obtain license key content.");
|
2025-03-27 12:42:45 +00:00
|
|
|
premiumEnabledResult = License.NORMAL;
|
2025-03-25 17:57:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2025-03-25 17:57:17 +00:00
|
|
|
|
|
|
|
// It's a direct license key
|
|
|
|
return keyOrFilePath;
|
2024-10-14 22:34:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void updateLicenseKey(String newKey) throws IOException {
|
2025-03-25 17:57:17 +00:00
|
|
|
applicationProperties.getPremium().setKey(newKey);
|
Improved Configuration and YAML Management (#2966)
# Description of Changes
**What was changed:**
- **Configuration Updates:**
Replaced all calls to `GeneralUtils.saveKeyToConfig` with the new
`GeneralUtils.saveKeyToSettings` method across multiple classes (e.g.,
`LicenseKeyChecker`, `InitialSetup`, `SettingsController`, etc.). This
update ensures consistent management of configuration settings.
- **File Path and Exception Handling:**
Updated file path handling in `SPDFApplication` by creating `Path`
objects from string paths and logging these paths for clarity. Also
refined exception handling by catching more specific exceptions (e.g.,
using `IOException` instead of a generic `Exception`).
- **Analytics Flag and Rate Limiting:**
Changed the analytics flag in the application properties from a `String`
to a `Boolean`, and updated related logic in `AppConfig` and
`PostHogService`. The rate-limiting property retrieval in `AppConfig`
was also refined for clarity.
- **YAML Configuration Management:**
Replaced the previous manual, line-based YAML merging logic in
`ConfigInitializer` with a new `YamlHelper` class. This helper leverages
the SnakeYAML engine to load, update, and save YAML configurations more
robustly while preserving comments and formatting.
**Why the change was made:**
- **Improved Maintainability:**
Consolidating configuration update logic into a single utility method
(`saveKeyToSettings`) reduces code duplication and simplifies future
maintenance.
- **Enhanced Robustness:**
The new `YamlHelper` class ensures that configuration files are merged
accurately and safely, minimizing risks of data loss or format
corruption.
- **Better Type Safety and Exception Handling:**
Switching the analytics flag to a Boolean and refining exception
handling improves code robustness and debugging efficiency.
- **Clarity and Consistency:**
Standardizing file path handling and logging practices enhances code
readability across the project.
**Challenges encountered:**
- **YAML Merging Complexity:**
Integrating the new `YamlHelper` required careful handling to preserve
existing settings, comments, and formatting during merges.
- **Type Conversion and Backward Compatibility:**
Updating the analytics flag from a string to a Boolean required
extensive testing to ensure backward compatibility and proper
functionality.
- **Exception Granularity:**
Refactoring exception handling from a generic to a more specific
approach involved a detailed review to cover all edge cases.
Closes #<issue_number>
---
## Checklist
- [x] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [ ] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md)
(if applicable)
- [ ] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md)
(if applicable)
- [x] I have performed a self-review of my own code
- [x] My changes generate no new warnings
### Documentation
- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)
### UI Changes (if applicable)
- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)
### Testing (if applicable)
- [x] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing)
for more details.
---------
Co-authored-by: Anthony Stirling <77850077+Frooodle@users.noreply.github.com>
2025-02-25 22:52:59 +01:00
|
|
|
GeneralUtils.saveKeyToSettings("EnterpriseEdition.key", newKey);
|
2024-10-14 22:34:41 +01:00
|
|
|
checkLicense();
|
|
|
|
}
|
|
|
|
|
2025-03-27 12:42:45 +00:00
|
|
|
public License getPremiumLicenseEnabledResult() {
|
2025-03-25 17:57:17 +00:00
|
|
|
return premiumEnabledResult;
|
2024-10-14 22:34:41 +01:00
|
|
|
}
|
|
|
|
}
|