Skip to content

Commit

Permalink
Support new spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed May 15, 2022
1 parent 51aee1c commit 868955b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
47 changes: 42 additions & 5 deletions src/main/java/io/github/gaming32/mrpacklib/packindex/PackFile.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.github.gaming32.mrpacklib.packindex;

import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import io.github.gaming32.mrpacklib.Mrpack.EnvCompatibility;
import io.github.gaming32.mrpacklib.Mrpack.EnvSide;
Expand All @@ -26,10 +29,23 @@ public EnvCompatibility get(EnvSide side) {
}
}

private static final Set<String> ALLOWED_HOSTS = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);

static {
ALLOWED_HOSTS.addAll(Arrays.asList(
"cdn.modrinth.com",
"edge.forgecdn.net",
"media.forgecdn.net",
"github.com",
"raw.githubusercontent.com"
));
}

private String path;
private Map<String, byte[]> hashes;
private FileEnv env;
private List<URL> downloads;
private long fileSize = -1;

public PackFile validate() throws IllegalArgumentException {
if (path == null) {
Expand All @@ -38,15 +54,21 @@ public PackFile validate() throws IllegalArgumentException {
if (hashes == null) {
throw new IllegalArgumentException("missing hashes");
}
if (!hashes.containsKey("sha1")) {
throw new IllegalArgumentException("missing hashes.sha1");
}
if (hashes.containsValue(null)) {
throw new IllegalArgumentException("hashes contains null hash");
}
if (!hashes.containsKey("sha1")) {
throw new IllegalArgumentException("missing hashes.sha1");
}
if (hashes.get("sha1").length != 20) {
throw new IllegalArgumentException("hashes.sha1.length != 20");
}
if (!hashes.containsKey("sha512")) {
throw new IllegalArgumentException("missing hashes.sha512");
}
if (hashes.get("sha512").length != 64) {
throw new IllegalArgumentException("hashes.sha512.length != 64");
}
if (env == null) {
env = new FileEnv();
env.client = EnvCompatibility.REQUIRED;
Expand All @@ -62,8 +84,19 @@ public PackFile validate() throws IllegalArgumentException {
if (downloads == null || downloads.size() == 0) {
throw new IllegalArgumentException("missing downloads");
}
if (downloads.contains(null)) {
throw new IllegalArgumentException("null download");
for (URL download : downloads) {
if (download == null) {
throw new IllegalArgumentException("null download");
}
if (!download.getProtocol().equals("https")) {
throw new IllegalArgumentException("download.protocol != \"https\"");
}
if (!ALLOWED_HOSTS.contains(download.getHost())) {
throw new IllegalArgumentException("\"" + download.getHost() + "\" not an allowed host");
}
}
if (fileSize == -1) {
throw new IllegalArgumentException("missing fileSize");
}
return this;
}
Expand All @@ -84,6 +117,10 @@ public List<URL> getDownloads() {
return downloads;
}

public long getFileSize() {
return fileSize;
}

@Override
public String toString() {
return GsonHelper.GSON.toJson(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public byte[] read(JsonReader in) throws IOException {
private GsonHelper() {
}

public static String toHexString(byte[] arr) {
private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
private static String toHexString(byte[] arr) {
StringBuilder result = new StringBuilder(arr.length << 1);
for (byte v : arr) {
result.append(Integer.toHexString(v & 0xff));
result.append(HEX_CHARS[(v & 0xff) >> 4]);
result.append(HEX_CHARS[v & 0xf]);
}
return result.toString();
}
Expand Down

0 comments on commit 868955b

Please sign in to comment.