diff --git a/source/net/filebot/License.java b/source/net/filebot/License.java index 080893f5..109755b9 100644 --- a/source/net/filebot/License.java +++ b/source/net/filebot/License.java @@ -3,13 +3,9 @@ package net.filebot; import static java.nio.charset.StandardCharsets.*; import static java.util.stream.Collectors.*; import static net.filebot.CachedResource.*; -import static net.filebot.Settings.*; -import static net.filebot.platform.windows.WinAppUtilities.*; -import static net.filebot.util.FileUtilities.*; import static net.filebot.util.RegularExpressions.*; import java.io.ByteArrayInputStream; -import java.io.File; import java.io.Serializable; import java.net.URL; import java.time.Instant; @@ -29,35 +25,10 @@ import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; import net.filebot.util.ByteBufferOutputStream; -import net.filebot.util.SystemProperty; import net.filebot.web.WebRequest; public class License implements Serializable { - public static final SystemProperty LICENSE_FILE = SystemProperty.of("net.filebot.license", File::new, ApplicationFolder.AppData.resolve("license.txt")); - - public static final Resource INSTANCE = Resource.lazy(() -> { - return new License(readFile(LICENSE_FILE.get())); - }); - - public static final void check() throws Exception { - if (isAppStore()) { - if (isWindowsApp() && "PointPlanck.FileBot".equals(getAppUserModelID())) { - return; - } else if (isMacSandbox() && !File.listRoots()[0].canRead()) { - return; - } - throw new Exception("BAD LICENSE: " + getAppStoreName() + " sandbox not found"); - } - - // check license file - License license = INSTANCE.get(); - - if (!license.isValid()) { - throw new Exception("BAD LICENSE: " + license); - } - }; - private byte[] bytes; private long id; @@ -76,8 +47,8 @@ public class License implements Serializable { verifyLicense(); } - public boolean isValid() { - return expires < System.currentTimeMillis(); + public boolean expired() { + return expires > System.currentTimeMillis(); } public Map getProperties() throws Exception { @@ -111,7 +82,7 @@ public class License implements Serializable { signature.update(clearSignMessage.getBytes(UTF_8)); if (!signature.verify()) { - throw new PGPException("BAD LICENSE: Signature does not match"); + throw new PGPException("Signature does not match"); } return clearSignMessage; diff --git a/source/net/filebot/LicenseError.java b/source/net/filebot/LicenseError.java new file mode 100644 index 00000000..54a90584 --- /dev/null +++ b/source/net/filebot/LicenseError.java @@ -0,0 +1,9 @@ +package net.filebot; + +public class LicenseError extends Error { + + public LicenseError(String message) { + super(message); + } + +} diff --git a/source/net/filebot/LicenseType.java b/source/net/filebot/LicenseType.java new file mode 100644 index 00000000..9f4602ef --- /dev/null +++ b/source/net/filebot/LicenseType.java @@ -0,0 +1,63 @@ +package net.filebot; + +import static net.filebot.Settings.*; +import static net.filebot.platform.windows.WinAppUtilities.*; +import static net.filebot.util.FileUtilities.*; + +import java.io.File; + +import net.filebot.util.SystemProperty; + +public enum LicenseType { + + MicrosoftStore { + + @Override + public void check() throws LicenseError { + if (!getAppUserModelID().equals("PointPlanck.FileBot")) { + throw new LicenseError("Invalid container state"); + } + } + }, + + MacAppStore { + + @Override + public void check() throws LicenseError { + if (File.listRoots()[0].canRead()) { + throw new LicenseError("Invalid container state"); + } + } + }, + + PGPSignedMessage { + + public final SystemProperty LICENSE_FILE = SystemProperty.of("net.filebot.license", File::new, ApplicationFolder.AppData.resolve("license.txt")); + public final Resource LICENSE = Resource.lazy(() -> new License(readFile(LICENSE_FILE.get()))); + + @Override + public void check() throws LicenseError { + try { + License license = LICENSE.get(); + + if (license.expired()) { + throw new LicenseError("Expired: " + license); + } + } catch (Exception e) { + throw new LicenseError(e.getMessage()); + } + } + }; + + public abstract void check() throws LicenseError; + + public static LicenseType get() { + if (isUWP()) + return MicrosoftStore; + if (isMacSandbox()) + return MacAppStore; + + return PGPSignedMessage; + } + +} diff --git a/source/net/filebot/Settings.java b/source/net/filebot/Settings.java index 93879154..d2c06fde 100644 --- a/source/net/filebot/Settings.java +++ b/source/net/filebot/Settings.java @@ -82,7 +82,7 @@ public final class Settings { } public static boolean isAppStore() { - return isApplicationDeployment("mas", "appx"); + return isApplicationDeployment("appx", "mas"); } public static boolean isWindowsApp() { @@ -101,6 +101,10 @@ public final class Settings { return isApplicationDeployment("mas"); } + public static boolean isUWP() { + return isApplicationDeployment("appx"); + } + public static boolean isAutoUpdateEnabled() { return isApplicationDeployment("mas", "appx", "snap", "spk", "aur"); }