diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/Mod.java b/common/src/main/java/com/adamcalculator/dynamicpack/Mod.java index d5c6105..07e178c 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/Mod.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/Mod.java @@ -21,7 +21,7 @@ public class Mod { ALLOWED_HOSTS.add("github.com"); ALLOWED_HOSTS.add("github.io"); ALLOWED_HOSTS.add("githubusercontent.com"); - if (!isRelease()) { + if (isLocalHostAllowed()) { ALLOWED_HOSTS.add("localhost"); } } @@ -60,4 +60,24 @@ private static long megabyte(long mb) { public static boolean isRelease() { return true; } + + // localhost allowed + private static boolean isLocalHostAllowed() { + return false; + } + + // file_debug_only:// allowed + public static boolean isFileDebugSchemeAllowed() { + return false; + } + + // http:// allowed + public static boolean isHTTPTrafficAllowed() { + return false; + } + + // DebugScreen allowed + public static boolean isDebugScreenAllowed() { + return true; + } } diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/SyncingTask.java b/common/src/main/java/com/adamcalculator/dynamicpack/SyncingTask.java index 4833f38..2d64278 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/SyncingTask.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/SyncingTask.java @@ -22,6 +22,7 @@ public void run() { try { pack.sync(createSyncProgressForPack(pack), manually); } catch (Exception e) { + onError(pack, e); Out.error("error while process pack: " + pack.getLocation().getName(), e); } } @@ -33,6 +34,10 @@ public void syncDone(boolean reloadRequired) { // to override } + public void onError(Pack pack, Exception e) { + // to override + } + public boolean isReloadRequired() { return reloadRequired; } diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/util/Urls.java b/common/src/main/java/com/adamcalculator/dynamicpack/util/Urls.java index 427c244..6a2b63e 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/util/Urls.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/util/Urls.java @@ -14,8 +14,12 @@ import java.util.zip.GZIPInputStream; public class Urls { - public static boolean isFileDebugScheme() { - return !Mod.isRelease(); + public static boolean isFileDebugSchemeAllowed() { + return Mod.isFileDebugSchemeAllowed(); + } + + public static boolean isHTTPTrafficAllowed() { + return Mod.isHTTPTrafficAllowed(); } public static String parseContentAndVerify(String signatureUrl, String url, String publicKeyBase64, long maxLimit) throws IOException { @@ -99,8 +103,13 @@ private static InputStream _getInputStreamOfUrl(String url, long sizeLimit) thro } private static InputStream _getInputStreamOfUrl(String url, long sizeLimit, /*@Nullable*/ LongConsumer progress) throws IOException { + if (url.contains(" ")) { + throw new IOException("URL can't contains spaces!"); + } + + if (url.startsWith("file_debug_only://")) { - if (!isFileDebugScheme()) { + if (!isFileDebugSchemeAllowed()) { throw new RuntimeException("Not allowed scheme."); } @@ -113,7 +122,26 @@ private static InputStream _getInputStreamOfUrl(String url, long sizeLimit, /*@N } else if (url.startsWith("http://")) { - throw new RuntimeException("HTTP (not secure) not allowed scheme."); + if (!isHTTPTrafficAllowed()) { + throw new RuntimeException("HTTP (not secure) not allowed scheme."); + } + + if (!Mod.isUrlHostTrusted(url)) { + if (Mod.isBlockAllNotTrustedNetworks()) { + throw new SecurityException("Url host is not trusted!"); + } + } + + URL urlObj = new URL(url); + URLConnection connection = urlObj.openConnection(); + long length = connection.getContentLengthLong(); + if (length > sizeLimit) { + throw new RuntimeException("[HTTP] File at " + url+ " so bigger. " + length + " > " + sizeLimit); + } + if (progress != null){ + progress.accept(length); + } + return connection.getInputStream(); } else if (url.startsWith("https://")) { @@ -123,9 +151,6 @@ private static InputStream _getInputStreamOfUrl(String url, long sizeLimit, /*@N } } - if (url.contains(" ")) { - Out.warn("URL " + url + " contains not encoded spaced! Use %20 for space symbol in links!"); - } URL urlObj = new URL(url); URLConnection connection = urlObj.openConnection(); long length = connection.getContentLengthLong(); diff --git a/gradle.properties b/gradle.properties index e82b27c..a9e4ad1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,6 +12,6 @@ loader_version=0.15.7 fabric_version=0.92.0+1.20.1 # Mod Properties -mod_version=1.0.5-mc1.20.1 +mod_version=1.0.6-mc1.20.1 maven_group=com.adamcalculator archives_base_name=dynamicpack \ No newline at end of file diff --git a/src/client/java/com/adamcalculator/dynamicpack/DebugScreen.java b/src/client/java/com/adamcalculator/dynamicpack/DebugScreen.java index 416d180..f0f6d7e 100644 --- a/src/client/java/com/adamcalculator/dynamicpack/DebugScreen.java +++ b/src/client/java/com/adamcalculator/dynamicpack/DebugScreen.java @@ -40,36 +40,49 @@ protected void init() { SystemToast toast = new SystemToast(SystemToast.Type.NARRATOR_TOGGLE, Text.literal("T"), Text.literal("d")); MinecraftClient.getInstance().getToastManager().add(toast); - try { - pack.sync(new SyncProgress() { - @Override - public void textLog(String s) { - toast.setContent(Text.literal("Log"), Text.literal(s)); - } + var task = new SyncingTask(true) { + @Override + public void syncDone(boolean reloadRequired) { + toast.setContent(Text.literal("Error"), Text.literal("rel_req="+reloadRequired)); + } - @Override - public void done(boolean b) { - if (b) { - toast.setContent(Text.literal("Done. Reload!"), Text.literal("Reload required!!!")); - } else { - toast.setContent(Text.literal("Done!"), Text.literal("")); - } - } + @Override + public void onError(Pack pack, Exception e) { + toast.setContent(Text.literal("Error"), Text.literal(e.getMessage())); + } - @Override - public void downloading(String name, float percentage) { + @Override + public SyncProgress createSyncProgressForPack(Pack pack) { + return new SyncProgress() { + @Override + public void textLog(String s) { + toast.setContent(Text.literal("Log"), Text.literal(s)); + } - } + @Override + public void done(boolean b) { + if (b) { + toast.setContent(Text.literal("Done. Reload!"), Text.literal("Reload required!!!")); + } else { + toast.setContent(Text.literal("Done!"), Text.literal("")); + } + } - @Override - public void start() { + @Override + public void downloading(String name, float percentage) { + toast.setContent(Text.literal("Download " + Math.round(percentage) + "%"), Text.literal(name)); + } - } - }, true); - } catch (Exception e) { - Out.e(e); - } + @Override + public void start() { + toast.setContent(Text.literal("Started!"), Text.literal("")); + } + }; + } + }; + new Thread(task, "DynamicPack-ManuallyCheckThread").start(); }).size(50, 20).position(190, height).build()); + } catch (Exception e) { addDrawableChild(ButtonWidget.builder(Text.of(e + ""), button -> { }).size(500, 20).position(10, height).build()); diff --git a/src/client/java/com/adamcalculator/dynamicpack/ModMenu.java b/src/client/java/com/adamcalculator/dynamicpack/ModMenu.java index cb76988..b06ef16 100644 --- a/src/client/java/com/adamcalculator/dynamicpack/ModMenu.java +++ b/src/client/java/com/adamcalculator/dynamicpack/ModMenu.java @@ -6,6 +6,6 @@ public class ModMenu implements ModMenuApi { @Override public ConfigScreenFactory getModConfigScreenFactory() { - return Mod.isRelease() ? null : parent -> new DebugScreen(); + return Mod.isDebugScreenAllowed() ? parent -> new DebugScreen() : null; } } \ No newline at end of file