|
@@ -1,4 +1,5 @@
|
|
using System.Reflection;
|
|
using System.Reflection;
|
|
|
|
+using FluentResults;
|
|
using InABox.Clients;
|
|
using InABox.Clients;
|
|
using InABox.Configuration;
|
|
using InABox.Configuration;
|
|
using InABox.Core;
|
|
using InABox.Core;
|
|
@@ -157,32 +158,37 @@ public static class DbFactory
|
|
Tampered
|
|
Tampered
|
|
}
|
|
}
|
|
|
|
|
|
- private static LicenseValidation CheckLicenseValidity(out License? license, out LicenseData? licenseData)
|
|
|
|
|
|
+ private static LicenseValidation CheckLicenseValidity(out DateTime expiry)
|
|
{
|
|
{
|
|
- license = Provider.Load<License>().FirstOrDefault();
|
|
|
|
|
|
+ expiry = DateTime.MinValue;
|
|
|
|
+ var license = Provider.Load<License>().FirstOrDefault();
|
|
if (license is null)
|
|
if (license is null)
|
|
- {
|
|
|
|
- licenseData = null;
|
|
|
|
return LicenseValidation.Missing;
|
|
return LicenseValidation.Missing;
|
|
- }
|
|
|
|
|
|
|
|
- if (!LicenseUtils.TryDecryptLicense(license.Data, out licenseData, out var error))
|
|
|
|
|
|
+ if (!LicenseUtils.TryDecryptLicense(license.Data, out var licenseData, out var error))
|
|
return LicenseValidation.Corrupt;
|
|
return LicenseValidation.Corrupt;
|
|
-
|
|
|
|
- if (licenseData.Expiry < DateTime.Now)
|
|
|
|
- return LicenseValidation.Expired;
|
|
|
|
|
|
+
|
|
|
|
+ if (!LicenseUtils.ValidateMacAddresses(licenseData.Addresses))
|
|
|
|
+ return LicenseValidation.Tampered;
|
|
|
|
|
|
var userTrackingItems = Provider.Query(
|
|
var userTrackingItems = Provider.Query(
|
|
new Filter<UserTracking>(x => x.ID).InList(licenseData.UserTrackingItems),
|
|
new Filter<UserTracking>(x => x.ID).InList(licenseData.UserTrackingItems),
|
|
- new Columns<UserTracking>(x => x.ID), log: false).Rows.Select(x => x.Get<UserTracking, Guid>(x => x.ID));
|
|
|
|
|
|
+ new Columns<UserTracking>(x => x.ID)
|
|
|
|
+ , log: false
|
|
|
|
+ ).Rows
|
|
|
|
+ .Select(r => r.Get<UserTracking, Guid>(c => c.ID))
|
|
|
|
+ .ToArray();
|
|
|
|
|
|
foreach(var item in licenseData.UserTrackingItems)
|
|
foreach(var item in licenseData.UserTrackingItems)
|
|
{
|
|
{
|
|
if (!userTrackingItems.Contains(item))
|
|
if (!userTrackingItems.Contains(item))
|
|
- {
|
|
|
|
return LicenseValidation.Tampered;
|
|
return LicenseValidation.Tampered;
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ expiry = licenseData.Expiry;
|
|
|
|
+ if (licenseData.Expiry < DateTime.Now)
|
|
|
|
+ return LicenseValidation.Expired;
|
|
|
|
+
|
|
return LicenseValidation.Valid;
|
|
return LicenseValidation.Valid;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -198,6 +204,12 @@ public static class DbFactory
|
|
{
|
|
{
|
|
LogImportant($"{message} Please renew your license before then, or your database will go into read-only mode; it will be locked for saving anything until you renew your license. For help with renewing your license, please see the documentation at https://prsdigital.com.au/wiki/index.php/License_Renewal.");
|
|
LogImportant($"{message} Please renew your license before then, or your database will go into read-only mode; it will be locked for saving anything until you renew your license. For help with renewing your license, please see the documentation at https://prsdigital.com.au/wiki/index.php/License_Renewal.");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public static void LogReadOnly()
|
|
|
|
+ {
|
|
|
|
+ LogImportant($"Your database is in read-only mode; please renew your license to enable database updates.");
|
|
|
|
+ }
|
|
|
|
+
|
|
private static void LogLicenseExpiry(DateTime expiry)
|
|
private static void LogLicenseExpiry(DateTime expiry)
|
|
{
|
|
{
|
|
if (expiry.Date == DateTime.Today)
|
|
if (expiry.Date == DateTime.Today)
|
|
@@ -222,21 +234,23 @@ public static class DbFactory
|
|
}
|
|
}
|
|
++_expiredLicenseCounter;
|
|
++_expiredLicenseCounter;
|
|
}
|
|
}
|
|
-
|
|
|
|
- public static void LogReadOnly()
|
|
|
|
- {
|
|
|
|
- LogError("Database is read-only because your license is invalid!");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+
|
|
private static void BeginReadOnly()
|
|
private static void BeginReadOnly()
|
|
{
|
|
{
|
|
- LogImportant("Your database is now in read-only mode, since your license is invalid; you will be unable to save any records to the database until you renew your license. For help with renewing your license, please see the documentation at https://prsdigital.com.au/wiki/index.php/License_Renewal.");
|
|
|
|
- _readOnly = true;
|
|
|
|
|
|
+ if (!IsReadOnly)
|
|
|
|
+ {
|
|
|
|
+ LogImportant(
|
|
|
|
+ "Your database is now in read-only mode, since your license is invalid; you will be unable to save any records to the database until you renew your license. For help with renewing your license, please see the documentation at https://prsdigital.com.au/wiki/index.php/License_Renewal.");
|
|
|
|
+ _readOnly = true;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
private static void EndReadOnly()
|
|
private static void EndReadOnly()
|
|
{
|
|
{
|
|
- LogImportant("Valid license found; the database is no longer read-only.");
|
|
|
|
- _readOnly = false;
|
|
|
|
|
|
+ if (IsReadOnly)
|
|
|
|
+ {
|
|
|
|
+ LogImportant("Valid license found; the database is no longer read-only.");
|
|
|
|
+ _readOnly = false;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
private static void BeginLicenseCheckTimer()
|
|
private static void BeginLicenseCheckTimer()
|
|
@@ -249,69 +263,15 @@ public static class DbFactory
|
|
{
|
|
{
|
|
AssertLicense();
|
|
AssertLicense();
|
|
}
|
|
}
|
|
-
|
|
|
|
- private static Random LicenseIDGenerate = new Random();
|
|
|
|
- private static void UpdateValidLicense(License license, LicenseData licenseData)
|
|
|
|
- {
|
|
|
|
- var ids = Provider.Query(
|
|
|
|
- new Filter<UserTracking>(x => x.Created).IsGreaterThanOrEqualTo(licenseData.LastRenewal),
|
|
|
|
- new Columns<UserTracking>(x => x.ID), log: false);
|
|
|
|
- var newIDList = new List<Guid>();
|
|
|
|
- if(ids.Rows.Count > 0)
|
|
|
|
- {
|
|
|
|
- for (int i = 0; i < 10; i++)
|
|
|
|
- {
|
|
|
|
- newIDList.Add(ids.Rows[LicenseIDGenerate.Next(0, ids.Rows.Count)].Get<UserTracking, Guid>(x => x.ID));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- licenseData.UserTrackingItems = newIDList.ToArray();
|
|
|
|
-
|
|
|
|
- if(LicenseUtils.TryEncryptLicense(licenseData, out var newData, out var error))
|
|
|
|
- {
|
|
|
|
- license.Data = newData;
|
|
|
|
- Provider.Save(license);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private static void AssertLicense()
|
|
|
|
|
|
+
|
|
|
|
+ public static void AssertLicense()
|
|
{
|
|
{
|
|
- var result = CheckLicenseValidity(out var license, out var licenseData);
|
|
|
|
- if (IsReadOnly)
|
|
|
|
- {
|
|
|
|
- if(result == LicenseValidation.Valid)
|
|
|
|
- {
|
|
|
|
- EndReadOnly();
|
|
|
|
- }
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // TODO: Switch to real system
|
|
|
|
- if(result != LicenseValidation.Valid)
|
|
|
|
- {
|
|
|
|
- var newLicense = LicenseUtils.GenerateNewLicense();
|
|
|
|
- if (LicenseUtils.TryEncryptLicense(newLicense, out var newData, out var error))
|
|
|
|
- {
|
|
|
|
- if (license == null)
|
|
|
|
- license = new License();
|
|
|
|
- license.Data = newData;
|
|
|
|
- Provider.Save(license);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- Logger.Send(LogType.Error, "", $"Error updating license: {error}");
|
|
|
|
- }
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ var result = CheckLicenseValidity(out DateTime expiry);
|
|
switch (result)
|
|
switch (result)
|
|
{
|
|
{
|
|
case LicenseValidation.Valid:
|
|
case LicenseValidation.Valid:
|
|
- LogLicenseExpiry(licenseData!.Expiry);
|
|
|
|
- UpdateValidLicense(license, licenseData);
|
|
|
|
|
|
+ LogLicenseExpiry(expiry);
|
|
|
|
+ EndReadOnly();
|
|
break;
|
|
break;
|
|
case LicenseValidation.Missing:
|
|
case LicenseValidation.Missing:
|
|
LogImportant("Database is unlicensed!");
|
|
LogImportant("Database is unlicensed!");
|