aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-04-12 14:32:41 +0200
committerGitHub <noreply@github.com>2024-04-12 14:32:41 +0200
commitafd3f0f861ebe7f8957eb6abc6e19f92c7b5896a (patch)
tree0e7990ed1e7083dd4da5f90a842d0122919f8c7c /src/main/java
parenta4a1c15a0b0de01e862c0f11882b45fee2c0cca7 (diff)
downloadNotEnoughUpdates-afd3f0f861ebe7f8957eb6abc6e19f92c7b5896a.tar.gz
NotEnoughUpdates-afd3f0f861ebe7f8957eb6abc6e19f92c7b5896a.tar.bz2
NotEnoughUpdates-afd3f0f861ebe7f8957eb6abc6e19f92c7b5896a.zip
Add in-game updater (#1050)
Co-authored-by: IRONM00N <64110067+IRONM00N@users.noreply.github.com>
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java33
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/listener/NEUEventListener.java4
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/AutoUpdater.java256
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/LinuxBasedUpdater.java47
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SCUCompatUpdater.java100
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SigningGithubSource.kt64
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SigningPool.kt68
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/UpdateLoader.java141
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/options/NEUConfig.java8
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/options/customtypes/ConfigVersionDisplay.java27
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/options/separatesections/About.java111
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/util/JarUtil.kt68
12 files changed, 373 insertions, 554 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java b/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java
index 21a92faa..b129e644 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/NotEnoughUpdates.java
@@ -23,6 +23,7 @@ import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
+import io.github.moulberry.moulconfig.observer.PropertyTypeAdapterFactory;
import io.github.moulberry.notenoughupdates.autosubscribe.AutoLoad;
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe;
import io.github.moulberry.notenoughupdates.core.BackgroundBlur;
@@ -46,7 +47,6 @@ import io.github.moulberry.notenoughupdates.miscfeatures.StorageManager;
import io.github.moulberry.notenoughupdates.miscfeatures.customblockzones.CustomBlockSounds;
import io.github.moulberry.notenoughupdates.miscfeatures.inventory.MuseumCheapestItemOverlay;
import io.github.moulberry.notenoughupdates.miscfeatures.inventory.MuseumItemHighlighter;
-import io.github.moulberry.notenoughupdates.miscfeatures.updater.AutoUpdater;
import io.github.moulberry.notenoughupdates.mixins.AccessorMinecraft;
import io.github.moulberry.notenoughupdates.oneconfig.IOneConfigCompat;
import io.github.moulberry.notenoughupdates.options.NEUConfig;
@@ -88,6 +88,8 @@ import java.awt.*;
import java.io.File;
import java.util.HashMap;
import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
@NEUAutoSubscribe
@Mod(
@@ -95,10 +97,29 @@ import java.util.Set;
guiFactory = "io.github.moulberry.notenoughupdates.core.config.MoulConfigGuiForgeInterop")
public class NotEnoughUpdates {
public static final String MODID = "notenoughupdates";
- public static final String VERSION = "2.1.1-PRE";
- public static final int VERSION_ID = 20106; //2.1.1 only so update notif works
- public static final int PRE_VERSION_ID = 0;
- public static final int HOTFIX_VERSION_ID = 0;
+ public static final String VERSION = VersionConst.VERSION;
+ private static final Pattern versionPattern = Pattern.compile("([0-9]+)\\.([0-9]+)\\.([0-9]+)");
+ public static final int VERSION_ID = parseVersion(VERSION);
+
+ private static int parseVersion(String versionName) {
+ Matcher matcher = versionPattern.matcher(versionName);
+ if (!matcher.matches()) {
+ return 0;
+ }
+ int major = Integer.parseInt(matcher.group(1));
+ if (major < 0 || major > 99) {
+ return 0;
+ }
+ int minor = Integer.parseInt(matcher.group(2));
+ if (minor < 0 || minor > 99) {
+ return 0;
+ }
+ int patch = Integer.parseInt(matcher.group(3));
+ if (patch < 0 || patch > 99) {
+ return 0;
+ }
+ return major * 10000 + minor * 100 + patch;
+ }
public static final Logger LOGGER = LogManager.getLogger("NotEnoughUpdates");
/**
@@ -152,6 +173,7 @@ public class NotEnoughUpdates {
}};
public static ProfileViewer profileViewer;
private final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation()
+ .registerTypeAdapterFactory(new PropertyTypeAdapterFactory())
.registerTypeAdapterFactory(KotlinTypeAdapterFactory.INSTANCE).create();
public NEUManager manager;
public NEUOverlay overlay;
@@ -161,7 +183,6 @@ public class NotEnoughUpdates {
public long lastOpenedGui = 0;
public boolean packDevEnabled = false;
public Color[][] colourMap = null;
- public AutoUpdater autoUpdater = new AutoUpdater(this);
private File configFile;
private long lastChatMessage = 0;
private long secondLastChatMessage = 0;
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/listener/NEUEventListener.java b/src/main/java/io/github/moulberry/notenoughupdates/listener/NEUEventListener.java
index 15f1a954..7cb39a96 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/listener/NEUEventListener.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/listener/NEUEventListener.java
@@ -215,10 +215,6 @@ public class NEUEventListener {
if (!joinedSB) {
joinedSB = true;
- if (NotEnoughUpdates.INSTANCE.config.notifications.updateChannel != 0) {
- NotEnoughUpdates.INSTANCE.autoUpdater.displayUpdateMessageIfOutOfDate();
- }
-
if (NotEnoughUpdates.INSTANCE.config.notifications.doRamNotif) {
long maxMemoryMB = Runtime.getRuntime().maxMemory() / 1024L / 1024L;
if (maxMemoryMB > 4100) {
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/AutoUpdater.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/AutoUpdater.java
deleted file mode 100644
index ac77d276..00000000
--- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/AutoUpdater.java
+++ /dev/null
@@ -1,256 +0,0 @@
-/*
- * Copyright (C) 2022 NotEnoughUpdates contributors
- *
- * This file is part of NotEnoughUpdates.
- *
- * NotEnoughUpdates is free software: you can redistribute it
- * and/or modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation, either
- * version 3 of the License, or (at your option) any later version.
- *
- * NotEnoughUpdates is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
- */
-
-package io.github.moulberry.notenoughupdates.miscfeatures.updater;
-
-import com.google.common.collect.Iterables;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonPrimitive;
-import com.google.gson.JsonSyntaxException;
-import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
-import io.github.moulberry.notenoughupdates.util.MoulSigner;
-import io.github.moulberry.notenoughupdates.util.NotificationHandler;
-import io.github.moulberry.notenoughupdates.util.Utils;
-import net.minecraft.client.Minecraft;
-import net.minecraft.client.gui.FontRenderer;
-import net.minecraft.event.ClickEvent;
-import net.minecraft.event.HoverEvent;
-import net.minecraft.util.ChatComponentText;
-import net.minecraft.util.ChatStyle;
-import net.minecraft.util.EnumChatFormatting;
-import net.minecraft.util.IChatComponent;
-import net.minecraftforge.fml.common.Loader;
-import org.apache.commons.lang3.SystemUtils;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.Arrays;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
-
-public class AutoUpdater {
-
- NotEnoughUpdates neu;
-
- public AutoUpdater(NotEnoughUpdates neu) {
- this.neu = neu;
- }
-
- public void logProgress(String str) {
- logProgress(new ChatComponentText(str));
- }
-
- public void logProgress(IChatComponent chatComponent) {
- Minecraft.getMinecraft().addScheduledTask(() -> {
- IChatComponent chatComponent1 = new ChatComponentText("");
- chatComponent1.setChatStyle(new ChatStyle().setColor(EnumChatFormatting.AQUA));
- chatComponent1.appendSibling(chatComponent);
- Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("§e[NEU-AutoUpdater] ").appendSibling(
- chatComponent1));
- });
- }
-
- public UpdateLoader getUpdateLoader(URL url) {
- if (SystemUtils.IS_OS_UNIX) {
- return new LinuxBasedUpdater(this, url);
- }
- if (Loader.isModLoaded("skyblockclientupdater") && SCUCompatUpdater.IS_ENABLED) {
- return SCUCompatUpdater.tryCreate(this, url);
- }
- return null;
- }
-
- UpdateLoader updateLoader;
-
- public void updateFromURL(URL url) {
- if (updateLoader != null) {
- logProgress(
- "There is already an update in progress, so the auto updater cannot process this update (as it might already be installed or is currently being downloaded). Please restart your client to install another update");
- return;
- }
- if (!"https".equals(url.getProtocol())) {
- logProgress("§cInvalid protocol in url: " + url + ". Only https is a valid protocol.");
- return;
- }
- updateLoader = getUpdateLoader(url);
- if (updateLoader == null) {
- return;
- }
- updateLoader.greet();
- logProgress(new ChatComponentText("[Start download now]")
- .setChatStyle(new ChatStyle()
- .setColor(EnumChatFormatting.GREEN)
- .setChatHoverEvent(new HoverEvent(
- HoverEvent.Action.SHOW_TEXT,
- new ChatComponentText("Click to start download.")
- ))
- .setChatClickEvent(new ClickEvent(
- ClickEvent.Action.RUN_COMMAND,
- "/neuupdate scheduleDownload"
- ))));
- }
-
- public void scheduleDownload() {
- if (updateLoader == null) {
- logProgress("§cNo update found. Try running /neuupdate check first");
- return;
- }
- if (updateLoader.getState() != UpdateLoader.State.NOTHING) {
- logProgress("§cUpdate download already started. No need to start the download again.");
- return;
- }
- logProgress("Download started.");
- updateLoader.scheduleDownload();
- }
-
- private void displayUpdateMessage(
- JsonObject updateJson,
- String updateMessage,
- String downloadLink,
- String directDownload
- ) {
- int firstWidth = -1;
-
- for (String line : Iterables.concat(Arrays.asList(updateMessage.split("\n")), Arrays.asList("Download here"))) {
- FontRenderer fr = Minecraft.getMinecraft().fontRendererObj;
- boolean isDownloadLink = line.equals("Download here");
- int width = fr.getStringWidth(line);
- if (firstWidth == -1) {
- firstWidth = width;
- }
- int missingLen = firstWidth - width;
- if (missingLen > 0) {
- StringBuilder sb = new StringBuilder(missingLen / 4 / 2 + line.length());
- for (int i = 0; i < missingLen / 4 / 2; i++) { /* fr.getCharWidth(' ') == 4 */
- sb.append(" ");
- }
- sb.append(line);
- line = sb.toString();
- }
- ChatComponentText cp = new ChatComponentText(line);
- if (isDownloadLink)
- cp.setChatStyle(Utils.createClickStyle(ClickEvent.Action.OPEN_URL, downloadLink));
- Minecraft.getMinecraft().thePlayer.addChatMessage(cp);
- }
- neu.displayLinks(updateJson, firstWidth);
- NotificationHandler.displayNotification(Arrays.asList(
- "",
- "§eThere is a new version of NotEnoughUpdates available.",
- "§eCheck the chat for more information"
- ), false);
- try {
- if (directDownload != null)
- updateFromURL(new URL(directDownload));
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
-
- public void displayUpdateMessageIfOutOfDate() {
- File repo = neu.manager.repoLocation;
- File updateJson = new File(repo, "update.json");
- if (updateJson.exists()) {
- if (!MoulSigner.verifySignature(updateJson)) {
- NotEnoughUpdates.LOGGER.error("update.json found without signature");
- Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(
- "§e[NEU] §cThere has been an error checking for updates. Check the log or join the discord for more information.").setChatStyle(
- Utils.createClickStyle(
- ClickEvent.Action.OPEN_URL, "https://discord.gg/moulberry")));
- return;
- }
- try {
- JsonObject o = neu.manager.getJsonFromFile(updateJson);
-
- int fullReleaseVersion =
- o.has("version_id") && o.get("version_id").isJsonPrimitive() ? o.get("version_id").getAsInt() : -1;
- int preReleaseVersion =
- o.has("pre_version_id") && o.get("pre_version_id").isJsonPrimitive() ? o.get("pre_version_id").getAsInt() : -1;
- int hotfixVersion =
- o.has("hotfix_id") && o.get("hotfix_id").isJsonPrimitive() ? o.get("hotfix_id").getAsInt() : -1;
-
- boolean hasFullReleaseAvailableForUpgrade = fullReleaseVersion > NotEnoughUpdates.VERSION_ID;
- boolean hasHotfixAvailableForUpgrade =
- fullReleaseVersion == NotEnoughUpdates.VERSION_ID && hotfixVersion > NotEnoughUpdates.HOTFIX_VERSION_ID;
- boolean hasPreReleaseAvailableForUpdate =
- fullReleaseVersion == NotEnoughUpdates.VERSION_ID && preReleaseVersion > NotEnoughUpdates.PRE_VERSION_ID;
-
- int updateChannel = NotEnoughUpdates.INSTANCE.config.notifications.updateChannel; /* 1 = Full, 2 = Pre */
- if (hasFullReleaseAvailableForUpgrade || (hasHotfixAvailableForUpgrade && updateChannel == 1)) {
- displayUpdateMessage(
- o,
- o.get("update_msg").getAsString(),
- o.get("update_link").getAsString(),
- o.has("update_direct") ? o.get("update_direct").getAsString() : null
- );
- } else if (hasPreReleaseAvailableForUpdate && updateChannel == 2) {
- displayUpdateMessage(
- o,
- o.get("pre_update_msg").getAsString(),
- o.get("pre_update_link").getAsString(),
- o.has("pre_update_direct") ? o.get("pre_update_direct").getAsString() : null
- );
- }
- } catch (Exception e) {
- e.printStackTrace();
- Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(
- "§e[NEU] §cThere has been an error checking for updates. Check the log or join the discord for more information.").setChatStyle(
- Utils.createClickStyle(
- ClickEvent.Action.OPEN_URL, "https://discord.gg/moulberry")));
- }
- }
- }
-
- private boolean validateMcModInfo(JsonArray array) {
- if (array.size() != 1) return false;
- JsonElement jsonElement = array.get(0);
- if (!jsonElement.isJsonObject()) return false;
- JsonObject jsonObject = jsonElement.getAsJsonObject();
- if (!jsonObject.has("modid")) return false;
- JsonElement modid = jsonObject.get("modid");
- if (!modid.isJsonPrimitive()) return false;
- JsonPrimitive primitive = modid.getAsJsonPrimitive();
- if (!primitive.isString()) return false;
- return "notenoughupdates".equals(primitive.getAsString());
- }
-
- public boolean isNeuJar(File sus) {
- try (ZipFile zipFile = new ZipFile(sus)) {
- ZipEntry entry = zipFile.getEntry("mcmod.info");
- if (entry == null) {
- return false;
- }
- try (InputStream inputStream = zipFile.getInputStream(entry)) {
- JsonArray jsonArray = neu.manager.gson.fromJson(
- new InputStreamReader(inputStream),
- JsonArray.class
- );
- return validateMcModInfo(jsonArray);
- }
- } catch (IOException | JsonSyntaxException e) {
- e.printStackTrace();
- }
- return false;
- }
-}
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/LinuxBasedUpdater.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/LinuxBasedUpdater.java
deleted file mode 100644
index 3118b135..00000000
--- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/LinuxBasedUpdater.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (C) 2022 NotEnoughUpdates contributors
- *
- * This file is part of NotEnoughUpdates.
- *
- * NotEnoughUpdates is free software: you can redistribute it
- * and/or modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation, either
- * version 3 of the License, or (at your option) any later version.
- *
- * NotEnoughUpdates is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
- */
-
-package io.github.moulberry.notenoughupdates.miscfeatures.updater;
-
-import java.io.File;
-import java.net.URL;
-import java.util.List;
-
-class LinuxBasedUpdater /* Based on what? */ extends UpdateLoader {
-
- LinuxBasedUpdater(AutoUpdater updater, URL url) {
- super(updater, url);
- }
-
- @Override
- public void greet() {
- updater.logProgress(
- "Welcome Aristocrat! Your superior linux system configuration is supported for NEU auto updates.");
- }
-
- @Override
- public void deleteFiles(List<File> toDelete) {
- for (File toDel : toDelete) {
- if (!toDel.delete()) {
- updater.logProgress("§cCould not delete old version of NEU: " + toDel + ". Please manually delete file.");
- state = State.FAILED;
- }
- }
- }
-}
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SCUCompatUpdater.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SCUCompatUpdater.java
deleted file mode 100644
index 3cb600cb..00000000
--- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SCUCompatUpdater.java
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * Copyright (C) 2022 NotEnoughUpdates contributors
- *
- * This file is part of NotEnoughUpdates.
- *
- * NotEnoughUpdates is free software: you can redistribute it
- * and/or modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation, either
- * version 3 of the License, or (at your option) any later version.
- *
- * NotEnoughUpdates is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
- */
-
-package io.github.moulberry.notenoughupdates.miscfeatures.updater;
-
-import java.io.File;
-import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.net.URL;
-import java.util.List;
-
-/*
- * Legal considerations: Skyblock Client Updater is licensed under the GNU AGPL v3.0 or later (with modifications).
- * https://github.com/My-Name-Is-Jeff/SkyblockClient-Updater/blob/main/LICENSE
- *
- * However, even tho the AGPL License does not allow conveying covered work in combination with LGPL licensed code
- * (such as our own), we do not perceive ourselves as conveying neither an unmodified version of Skyblock Client Updater
- * nor a work based on Skyblock Client Updater (modified work) since our work is usable and functional in its entirety
- * without presence of Skyblock Client Updater and is not to be distributed along a copy of Skyblock Client Updater
- * unless that combined work is licensed with respect of both the LGPL and the AGPL, therefore is not adapting any part
- * of Skyblock Client Updater unless already part of a whole distribution.
- *
- * In case the Copyright owner (Lily aka My-Name-Is-Jeff on Github) disagrees, we are willing to take down this module
- * (or only convey this component of our work under a pure GPL license) with or without them providing legal grounds
- * for this request. However, due to them not being able to be reached for comment, we will include this
- * component for the time being.
- * */
-public class SCUCompatUpdater extends UpdateLoader {
-
- public static final boolean IS_ENABLED = false;
-
- private SCUCompatUpdater(AutoUpdater updater, URL url) {
- super(updater, url);
- }
-
- @Override
- public void greet() {
- updater.logProgress("Skyblock Client Updater compatibility layer loaded.");
- }
-
- @Override
- public void deleteFiles(List<File> toDelete) {
- try {
- for (File f : toDelete)
- ReflectionHolder.deleteFileOnShutdownHandle.invoke(ReflectionHolder.updateCheckerInstance, f, "");
- } catch (Throwable e) {
- e.printStackTrace();
- updater.logProgress("Invoking SCU failed. Check the log for more info.");
- state = State.FAILED;
- }
- }
-
- static class ReflectionHolder {
- static boolean isSCUFullyPresent = false;
- static Class<?> updateChecker;
- static Object updateCheckerInstance;
- static Method deleteFileOnShutdown;
- static MethodHandle deleteFileOnShutdownHandle;
-
- static {
- try {
- updateChecker = Class.forName("mynameisjeff.skyblockclientupdater.utils.UpdateChecker");
- Field instanceField = updateChecker.getDeclaredField("INSTANCE");
- instanceField.setAccessible(true);
- updateCheckerInstance = instanceField.get(null);
- deleteFileOnShutdown = updateChecker.getDeclaredMethod("deleteFileOnShutdown", File.class, String.class);
- deleteFileOnShutdownHandle = MethodHandles.publicLookup().unreflect(deleteFileOnShutdown);
- isSCUFullyPresent = true;
- } catch (NoSuchFieldException | ClassNotFoundException | IllegalAccessException | NoSuchMethodException e) {
- e.printStackTrace();
- }
- }
- }
-
- public static UpdateLoader tryCreate(AutoUpdater updater, URL url) {
- if (!ReflectionHolder.isSCUFullyPresent) {
- updater.logProgress("§cFound Skyclient Updater Mod, however our hooks did not function properly.");
- return null;
- }
- return new SCUCompatUpdater(updater, url);
- }
-}
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SigningGithubSource.kt b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SigningGithubSource.kt
new file mode 100644
index 00000000..b8c804ff
--- /dev/null
+++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SigningGithubSource.kt
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2024 NotEnoughUpdates contributors
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.miscfeatures.updater
+
+import moe.nea.libautoupdate.GithubReleaseUpdateSource
+import moe.nea.libautoupdate.UpdateData
+import java.io.ByteArrayInputStream
+import java.io.File
+import java.net.URL
+
+class SigningGithubSource(username: String, repo: String) :
+ GithubReleaseUpdateSource(username, repo) {
+
+ val hashRegex = "sha256sum: `(?<hash>[a-fA-F0-9]{64})`".toPattern()
+ override fun findAsset(release: GithubRelease): UpdateData? {
+ var asset = super.findAsset(release) ?: return null
+ val match = release.body.lines()
+ .firstNotNullOfOrNull { line -> hashRegex.matcher(line).takeIf { it.matches() } }
+ ?: return null
+ // Inject our custom sha256sum
+ asset = UpdateData(asset.versionName, asset.versionNumber, match.group("hash"), asset.download)
+ // Verify at least 2 signatures are present on this release
+ if (!verifyAnySignature(release, asset))
+ return null
+ return asset
+ }
+
+ private fun verifyAnySignature(release: GithubRelease, asset: UpdateData): Boolean {
+ val signatories = findValidSignatories(release, asset)
+ for (signatory in signatories) {
+ println("Accepted signature from ${signatory.name}")
+ }
+ return signatories.size >= 2
+ }
+
+ fun findValidSignatories(release: GithubRelease, asset: UpdateData): List<GithubRelease.Download> {
+ val signatures = release.assets?.filter { it.name.endsWith(".asc") } ?: emptyList()
+ return signatures.filter { verifySignature(it, asset) }
+ }
+
+ fun verifySignature(signatureDownload: GithubRelease.Download, asset: UpdateData): Boolean {
+ val name = signatureDownload.name.substringBeforeLast('.').removePrefix("_")
+ val signatureBytes = URL(signatureDownload.browserDownloadUrl).openStream().readBytes()
+ val hashBytes = ByteArrayInputStream(asset.sha256.uppercase().encodeToByteArray())
+ return SigningPool.verifySignature(name, hashBytes, signatureBytes)
+ }
+}
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SigningPool.kt b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SigningPool.kt
new file mode 100644
index 00000000..c75806c0
--- /dev/null
+++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/SigningPool.kt
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2024 NotEnoughUpdates contributors
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.miscfeatures.updater
+
+import io.github.moulberry.notenoughupdates.util.JarUtil
+import java.io.InputStream
+import java.security.KeyFactory
+import java.security.PublicKey
+import java.security.Signature
+import java.security.spec.X509EncodedKeySpec
+
+
+object SigningPool {
+
+ data class PK(
+ val name: String,
+ val publicKey: PublicKey,
+ )
+
+ fun load(): List<PK> {
+ val l = JarUtil.access.listFiles("trusted_team_members")
+ return l.filter { it.endsWith(".key") }
+ .map {
+ loadPK(
+ it.substringBeforeLast('.'),
+ JarUtil.access.read("trusted_team_members/$it")
+ )
+ }
+ }
+
+ fun loadPK(name: String, inputStream: InputStream): PK {
+ val publicKeyBytes = inputStream.readBytes()
+ val x509EncodedKeySpec = X509EncodedKeySpec(publicKeyBytes)
+ return PK(name, KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec))
+ }
+
+ val keyPool = load().associateBy { it.name }
+
+ fun verifySignature(name: String, inputStream: InputStream, signatureBytes: ByteArray): Boolean {
+ val signature = Signature.getInstance("SHA256withRSA")
+ signature.initVerify(keyPool[name]?.publicKey ?: return false)
+
+ val b = ByteArray(4096)
+ while (true) {
+ val read = inputStream.read(b)
+ if (read < 0) break
+ signature.update(b, 0, read)
+ }
+ return signature.verify(signatureBytes)
+ }
+}
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/UpdateLoader.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/UpdateLoader.java
deleted file mode 100644
index 1a1a8504..00000000
--- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/updater/UpdateLoader.java
+++ /dev/null
@@ -1,141 +0,0 @@
-/*
- * Copyright (C) 2022 NotEnoughUpdates contributors
- *
- * This file is part of NotEnoughUpdates.
- *
- * NotEnoughUpdates is free software: you can redistribute it
- * and/or modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation, either
- * version 3 of the License, or (at your option) any later version.
- *
- * NotEnoughUpdates is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
- */
-
-package io.github.moulberry.notenoughupdates.miscfeatures.updater;
-
-import io.github.moulberry.notenoughupdates.util.NetUtils;
-import net.minecraft.client.Minecraft;
-import org.apache.commons.io.IOUtils;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.URL;
-import java.nio.file.Files;
-import java.util.ArrayList;
-import java.util.List;
-
-abstract class UpdateLoader {
-
- enum State {
- NOTHING, DOWNLOAD_STARTED, DOWNLOAD_FINISHED, INSTALLED, FAILED
- }
-
- URL url;
- AutoUpdater updater;
-
- State state = State.NOTHING;
-
- UpdateLoader(AutoUpdater updater, URL url) {
- this.url = url;
- this.updater = updater;
- }
-
- public State getState() {
- return state;
- }
-
- public void setState(State state) {
- this.state = state;
- }
-
- public URL getUrl() {
- return url;
- }
-
- public void scheduleDownload() {
- state = State.DOWNLOAD_STARTED;
- try {
- NetUtils.downloadAsync(url, File.createTempFile("NotEnoughUpdates-update", ".jar"))
- .handle(
- (f, exc) -> {