Kavita+ Enhancements (#2616)
This commit is contained in:
parent
625c56b265
commit
dd44f55747
43 changed files with 1056 additions and 468 deletions
|
|
@ -7,7 +7,6 @@ using API.DTOs.License;
|
|||
using API.Entities.Enums;
|
||||
using EasyCaching.Core;
|
||||
using Flurl.Http;
|
||||
using Hangfire;
|
||||
using Kavita.Common;
|
||||
using Kavita.Common.EnvironmentInfo;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
|
@ -24,31 +23,25 @@ internal class RegisterLicenseResponseDto
|
|||
|
||||
public interface ILicenseService
|
||||
{
|
||||
Task ValidateLicenseStatus();
|
||||
//Task ValidateLicenseStatus();
|
||||
Task RemoveLicense();
|
||||
Task AddLicense(string license, string email, string? discordId);
|
||||
Task<bool> HasActiveLicense(bool forceCheck = false);
|
||||
Task<bool> HasActiveSubscription(string? license);
|
||||
Task<bool> ResetLicense(string license, string email);
|
||||
}
|
||||
|
||||
public class LicenseService : ILicenseService
|
||||
public class LicenseService(
|
||||
IEasyCachingProviderFactory cachingProviderFactory,
|
||||
IUnitOfWork unitOfWork,
|
||||
ILogger<LicenseService> logger)
|
||||
: ILicenseService
|
||||
{
|
||||
private readonly IEasyCachingProviderFactory _cachingProviderFactory;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ILogger<LicenseService> _logger;
|
||||
private readonly TimeSpan _licenseCacheTimeout = TimeSpan.FromHours(8);
|
||||
public const string Cron = "0 */4 * * *";
|
||||
private const string CacheKey = "license";
|
||||
|
||||
|
||||
public LicenseService(IEasyCachingProviderFactory cachingProviderFactory, IUnitOfWork unitOfWork, ILogger<LicenseService> logger)
|
||||
{
|
||||
_cachingProviderFactory = cachingProviderFactory;
|
||||
_unitOfWork = unitOfWork;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Performs license lookup to API layer
|
||||
/// </summary>
|
||||
|
|
@ -77,7 +70,7 @@ public class LicenseService : ILicenseService
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "An error happened during the request to Kavita+ API");
|
||||
logger.LogError(e, "An error happened during the request to Kavita+ API");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
|
@ -115,12 +108,12 @@ public class LicenseService : ILicenseService
|
|||
return response.EncryptedLicense;
|
||||
}
|
||||
|
||||
_logger.LogError("An error happened during the request to Kavita+ API: {ErrorMessage}", response.ErrorMessage);
|
||||
logger.LogError("An error happened during the request to Kavita+ API: {ErrorMessage}", response.ErrorMessage);
|
||||
throw new KavitaException(response.ErrorMessage);
|
||||
}
|
||||
catch (FlurlHttpException e)
|
||||
{
|
||||
_logger.LogError(e, "An error happened during the request to Kavita+ API");
|
||||
logger.LogError(e, "An error happened during the request to Kavita+ API");
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
|
@ -129,57 +122,41 @@ public class LicenseService : ILicenseService
|
|||
/// Checks licenses and updates cache
|
||||
/// </summary>
|
||||
/// <remarks>Expected to be called at startup and on reoccurring basis</remarks>
|
||||
public async Task ValidateLicenseStatus()
|
||||
{
|
||||
var provider = _cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.License);
|
||||
try
|
||||
{
|
||||
var license = await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey);
|
||||
if (string.IsNullOrEmpty(license.Value)) {
|
||||
await provider.SetAsync(CacheKey, false, _licenseCacheTimeout);
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Validating Kavita+ License");
|
||||
|
||||
await provider.FlushAsync();
|
||||
var isValid = await IsLicenseValid(license.Value);
|
||||
await provider.SetAsync(CacheKey, isValid, _licenseCacheTimeout);
|
||||
|
||||
_logger.LogInformation("Validating Kavita+ License - Complete");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "There was an error talking with Kavita+ API for license validation. Rescheduling check in 30 mins");
|
||||
await provider.SetAsync(CacheKey, false, _licenseCacheTimeout);
|
||||
BackgroundJob.Schedule(() => ValidateLicenseStatus(), TimeSpan.FromMinutes(30));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RemoveLicense()
|
||||
{
|
||||
var serverSetting = await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey);
|
||||
serverSetting.Value = string.Empty;
|
||||
_unitOfWork.SettingsRepository.Update(serverSetting);
|
||||
await _unitOfWork.CommitAsync();
|
||||
var provider = _cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.License);
|
||||
await provider.RemoveAsync(CacheKey);
|
||||
}
|
||||
|
||||
public async Task AddLicense(string license, string email, string? discordId)
|
||||
{
|
||||
var serverSetting = await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey);
|
||||
var lic = await RegisterLicense(license, email, discordId);
|
||||
if (string.IsNullOrWhiteSpace(lic))
|
||||
throw new KavitaException("unable-to-register-k+");
|
||||
serverSetting.Value = lic;
|
||||
_unitOfWork.SettingsRepository.Update(serverSetting);
|
||||
await _unitOfWork.CommitAsync();
|
||||
}
|
||||
// public async Task ValidateLicenseStatus()
|
||||
// {
|
||||
// var provider = _cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.License);
|
||||
// try
|
||||
// {
|
||||
// var license = await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey);
|
||||
// if (string.IsNullOrEmpty(license.Value)) {
|
||||
// await provider.SetAsync(CacheKey, false, _licenseCacheTimeout);
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// _logger.LogInformation("Validating Kavita+ License");
|
||||
//
|
||||
// await provider.FlushAsync();
|
||||
// var isValid = await IsLicenseValid(license.Value);
|
||||
// await provider.SetAsync(CacheKey, isValid, _licenseCacheTimeout);
|
||||
//
|
||||
// _logger.LogInformation("Validating Kavita+ License - Complete");
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// _logger.LogError(ex, "There was an error talking with Kavita+ API for license validation. Rescheduling check in 30 mins");
|
||||
// await provider.SetAsync(CacheKey, false, _licenseCacheTimeout);
|
||||
// BackgroundJob.Schedule(() => ValidateLicenseStatus(), TimeSpan.FromMinutes(30));
|
||||
// }
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Checks licenses and updates cache
|
||||
/// </summary>
|
||||
/// <param name="forceCheck">Skip what's in cache</param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> HasActiveLicense(bool forceCheck = false)
|
||||
{
|
||||
var provider = _cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.License);
|
||||
var provider = cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.License);
|
||||
if (!forceCheck)
|
||||
{
|
||||
var cacheValue = await provider.GetAsync<bool>(CacheKey);
|
||||
|
|
@ -188,7 +165,7 @@ public class LicenseService : ILicenseService
|
|||
|
||||
try
|
||||
{
|
||||
var serverSetting = await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey);
|
||||
var serverSetting = await unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey);
|
||||
var result = await IsLicenseValid(serverSetting.Value);
|
||||
await provider.FlushAsync();
|
||||
await provider.SetAsync(CacheKey, result, _licenseCacheTimeout);
|
||||
|
|
@ -196,17 +173,77 @@ public class LicenseService : ILicenseService
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "There was an issue connecting to Kavita+");
|
||||
logger.LogError(ex, "There was an issue connecting to Kavita+");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<bool> HasActiveSubscription(string? license)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(license)) return false;
|
||||
try
|
||||
{
|
||||
var response = await (Configuration.KavitaPlusApiUrl + "/api/license/check-sub")
|
||||
.WithHeader("Accept", "application/json")
|
||||
.WithHeader("User-Agent", "Kavita")
|
||||
.WithHeader("x-license-key", license)
|
||||
.WithHeader("x-installId", HashUtil.ServerToken())
|
||||
.WithHeader("x-kavita-version", BuildInfo.Version)
|
||||
.WithHeader("Content-Type", "application/json")
|
||||
.WithTimeout(TimeSpan.FromSeconds(Configuration.DefaultTimeOutSecs))
|
||||
.PostJsonAsync(new LicenseValidDto()
|
||||
{
|
||||
License = license,
|
||||
InstallId = HashUtil.ServerToken()
|
||||
})
|
||||
.ReceiveString();
|
||||
var result = bool.Parse(response);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
var provider = cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.License);
|
||||
await provider.FlushAsync();
|
||||
await provider.SetAsync(CacheKey, result, _licenseCacheTimeout);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "An error happened during the request to Kavita+ API");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RemoveLicense()
|
||||
{
|
||||
var serverSetting = await unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey);
|
||||
serverSetting.Value = string.Empty;
|
||||
unitOfWork.SettingsRepository.Update(serverSetting);
|
||||
await unitOfWork.CommitAsync();
|
||||
var provider = cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.License);
|
||||
await provider.RemoveAsync(CacheKey);
|
||||
}
|
||||
|
||||
public async Task AddLicense(string license, string email, string? discordId)
|
||||
{
|
||||
var serverSetting = await unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey);
|
||||
var lic = await RegisterLicense(license, email, discordId);
|
||||
if (string.IsNullOrWhiteSpace(lic))
|
||||
throw new KavitaException("unable-to-register-k+");
|
||||
serverSetting.Value = lic;
|
||||
unitOfWork.SettingsRepository.Update(serverSetting);
|
||||
await unitOfWork.CommitAsync();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<bool> ResetLicense(string license, string email)
|
||||
{
|
||||
try
|
||||
{
|
||||
var encryptedLicense = await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey);
|
||||
var encryptedLicense = await unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey);
|
||||
var response = await (Configuration.KavitaPlusApiUrl + "/api/license/reset")
|
||||
.WithHeader("Accept", "application/json")
|
||||
.WithHeader("User-Agent", "Kavita")
|
||||
|
|
@ -225,17 +262,17 @@ public class LicenseService : ILicenseService
|
|||
|
||||
if (string.IsNullOrEmpty(response))
|
||||
{
|
||||
var provider = _cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.License);
|
||||
var provider = cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.License);
|
||||
await provider.RemoveAsync(CacheKey);
|
||||
return true;
|
||||
}
|
||||
|
||||
_logger.LogError("An error happened during the request to Kavita+ API: {ErrorMessage}", response);
|
||||
logger.LogError("An error happened during the request to Kavita+ API: {ErrorMessage}", response);
|
||||
throw new KavitaException(response);
|
||||
}
|
||||
catch (FlurlHttpException e)
|
||||
{
|
||||
_logger.LogError(e, "An error happened during the request to Kavita+ API");
|
||||
logger.LogError(e, "An error happened during the request to Kavita+ API");
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue