mirror of
https://github.com/mitb-archive/filebot
synced 2024-11-16 06:15:02 -05:00
Refactor license parser
This commit is contained in:
parent
671703adb2
commit
e3877dcffb
@ -1,5 +1,6 @@
|
|||||||
package net.filebot;
|
package net.filebot;
|
||||||
|
|
||||||
|
import static java.nio.charset.StandardCharsets.*;
|
||||||
import static java.util.stream.Collectors.*;
|
import static java.util.stream.Collectors.*;
|
||||||
import static net.filebot.Settings.*;
|
import static net.filebot.Settings.*;
|
||||||
import static net.filebot.util.JsonUtilities.*;
|
import static net.filebot.util.JsonUtilities.*;
|
||||||
@ -20,8 +21,6 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
|
|
||||||
import net.filebot.util.SystemProperty;
|
import net.filebot.util.SystemProperty;
|
||||||
import net.filebot.web.WebRequest;
|
import net.filebot.web.WebRequest;
|
||||||
|
|
||||||
@ -88,10 +87,10 @@ public class License {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// read and verify license file
|
// read and verify license file
|
||||||
byte[] bytes = Files.readAllBytes(file.toPath());
|
String psm = findClearSignMessage(file);
|
||||||
|
|
||||||
// verify and get clear signed content
|
// verify and get clear signed content
|
||||||
Map<String, String> properties = getProperties(bytes);
|
Map<String, String> properties = getProperties(psm);
|
||||||
|
|
||||||
String product = properties.get("Product");
|
String product = properties.get("Product");
|
||||||
String id = properties.get("Order");
|
String id = properties.get("Order");
|
||||||
@ -101,7 +100,7 @@ public class License {
|
|||||||
checkExpirationDate(expires);
|
checkExpirationDate(expires);
|
||||||
|
|
||||||
// verify license online
|
// verify license online
|
||||||
verifyLicense(id, bytes);
|
verifyLicense(id, psm);
|
||||||
|
|
||||||
return new License(product, id, expires);
|
return new License(product, id, expires);
|
||||||
} catch (Exception error) {
|
} catch (Exception error) {
|
||||||
@ -109,9 +108,8 @@ public class License {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, String> getProperties(byte[] bytes) throws Exception {
|
private static Map<String, String> getProperties(String psm) throws Exception {
|
||||||
byte[] pub = IOUtils.toByteArray(License.class.getResource("license.key"));
|
String msg = verifyClearSignMessage(psm, License.class.getResourceAsStream("license.key"));
|
||||||
String msg = verifyClearSignMessage(bytes, pub);
|
|
||||||
|
|
||||||
return NEWLINE.splitAsStream(msg).map(s -> s.split(": ", 2)).collect(toMap(a -> a[0], a -> a[1]));
|
return NEWLINE.splitAsStream(msg).map(s -> s.split(": ", 2)).collect(toMap(a -> a[0], a -> a[1]));
|
||||||
}
|
}
|
||||||
@ -130,9 +128,9 @@ public class License {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void verifyLicense(String id, byte[] bytes) throws Exception {
|
private static void verifyLicense(String id, String psm) throws Exception {
|
||||||
Cache cache = CacheManager.getInstance().getCache("license", CacheType.Persistent);
|
Cache cache = CacheManager.getInstance().getCache("license", CacheType.Persistent);
|
||||||
Object json = cache.json(id, i -> new URL("https://license.filebot.net/verify/" + i)).fetch((url, modified) -> WebRequest.post(url, bytes, "application/octet-stream", getRequestParameters())).expire(Cache.ONE_MONTH).get();
|
Object json = cache.json(id, i -> new URL("https://license.filebot.net/verify/" + i)).fetch((url, modified) -> WebRequest.post(url, psm.getBytes(UTF_8), "application/octet-stream", getRequestParameters())).expire(Cache.ONE_MONTH).get();
|
||||||
|
|
||||||
if (getInteger(json, "status") != 200) {
|
if (getInteger(json, "status") != 200) {
|
||||||
throw new IllegalStateException(getString(json, "message"));
|
throw new IllegalStateException(getString(json, "message"));
|
||||||
|
@ -5,6 +5,15 @@ import static java.util.stream.Collectors.*;
|
|||||||
import static net.filebot.util.RegularExpressions.*;
|
import static net.filebot.util.RegularExpressions.*;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.regex.MatchResult;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.bouncycastle.bcpg.ArmoredInputStream;
|
import org.bouncycastle.bcpg.ArmoredInputStream;
|
||||||
import org.bouncycastle.openpgp.PGPException;
|
import org.bouncycastle.openpgp.PGPException;
|
||||||
@ -18,8 +27,8 @@ import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
|
|||||||
|
|
||||||
public class PGP {
|
public class PGP {
|
||||||
|
|
||||||
public static String verifyClearSignMessage(byte[] psm, byte[] pub) throws Exception {
|
public static String verifyClearSignMessage(String psm, InputStream pub) throws Exception {
|
||||||
ArmoredInputStream armoredInput = new ArmoredInputStream(new ByteArrayInputStream(psm));
|
ArmoredInputStream armoredInput = new ArmoredInputStream(new ByteArrayInputStream(psm.getBytes(UTF_8)));
|
||||||
|
|
||||||
// read content
|
// read content
|
||||||
ByteBufferOutputStream content = new ByteBufferOutputStream(256);
|
ByteBufferOutputStream content = new ByteBufferOutputStream(256);
|
||||||
@ -51,4 +60,26 @@ public class PGP {
|
|||||||
return clearSignMessage;
|
return clearSignMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Pattern PGP_SIGNED_MESSAGE = Pattern.compile("-----BEGIN PGP SIGNED MESSAGE-----(.*?)-----END PGP SIGNATURE-----", Pattern.MULTILINE | Pattern.DOTALL);
|
||||||
|
|
||||||
|
public static String findClearSignMessage(CharSequence content) {
|
||||||
|
Matcher matcher = PGP_SIGNED_MESSAGE.matcher(content);
|
||||||
|
if (matcher.find()) {
|
||||||
|
return matcher.group();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("PGP SIGNED MESSAGE not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String findClearSignMessage(File file) throws IOException {
|
||||||
|
try (Scanner scanner = new Scanner(new FileInputStream(file), UTF_8)) {
|
||||||
|
Optional<MatchResult> match = scanner.findAll(PGP_SIGNED_MESSAGE).findFirst();
|
||||||
|
if (match.isPresent()) {
|
||||||
|
return match.get().group();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("PGP SIGNED MESSAGE not found");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user