Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
adam committed Mar 17, 2024
2 parents 40fe01c + 8ce0665 commit c69d19d
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 34 deletions.
22 changes: 21 additions & 1 deletion common/src/main/java/com/adamcalculator/dynamicpack/Mod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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;
}
Expand Down
39 changes: 32 additions & 7 deletions common/src/main/java/com/adamcalculator/dynamicpack/util/Urls.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.");
}

Expand All @@ -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://")) {
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
61 changes: 37 additions & 24 deletions src/client/java/com/adamcalculator/dynamicpack/DebugScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit c69d19d

Please sign in to comment.