From 38d29a8efd39ef6444939d06f110b86605a610a1 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Thu, 13 Jun 2024 13:39:43 +0200 Subject: [PATCH] add web services and update notifications --- .forgejo/workflows/build-loader.yml | 1 - .gitignore | 1 + .../java/de/com/baseband/client/BaseBand.java | 2 + .../java/de/com/baseband/client/DevStub.java | 9 +-- .../baseband/client/util/data/GitHash.java | 20 ++++- .../client/util/net/WebServiceClient.java | 78 ++++++++++++++++--- .../java/de/com/baseband/launcher/Loader.java | 5 +- .../com/baseband/launcher/util/GitHash.java | 20 ++++- .../de/com/baseband/server/ClientHandler.java | 69 ++++++++++++++++ .../de/com/baseband/server/LoaderHandler.java | 2 + .../java/de/com/baseband/server/Main.java | 14 +++- scripts/gen_hash.sh | 13 +--- 12 files changed, 200 insertions(+), 34 deletions(-) create mode 100644 Server/src/main/java/de/com/baseband/server/ClientHandler.java diff --git a/.forgejo/workflows/build-loader.yml b/.forgejo/workflows/build-loader.yml index 2bc935c..0489249 100644 --- a/.forgejo/workflows/build-loader.yml +++ b/.forgejo/workflows/build-loader.yml @@ -52,7 +52,6 @@ jobs: BB_PORT: ${{ vars.BB_PORT }} BB_PATH: ${{ vars.BB_PATH }} run: | - bash scripts/push_file.sh loader.version bash scripts/push_file.sh Loader/build/proguard/BaseBand-Loader.jar - name: Notify Action Completion if: always() diff --git a/.gitignore b/.gitignore index 6afc648..e26eee2 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ valid_hashes.txt Obf.tar /Server/run/ mobf.jar +resources/commit \ No newline at end of file diff --git a/Client/src/main/java/de/com/baseband/client/BaseBand.java b/Client/src/main/java/de/com/baseband/client/BaseBand.java index bb2da97..b906cd7 100644 --- a/Client/src/main/java/de/com/baseband/client/BaseBand.java +++ b/Client/src/main/java/de/com/baseband/client/BaseBand.java @@ -12,6 +12,7 @@ import de.com.baseband.client.feature.render.HUD; import de.com.baseband.client.registry.Configuration; import de.com.baseband.client.registry.Updater; import de.com.baseband.client.util.interact.Chat; +import de.com.baseband.client.util.net.WebServiceClient; import de.com.baseband.prod.LoadHandler; import de.tudbut.tools.Lock; import hint.Mobf; @@ -74,6 +75,7 @@ public class BaseBand { Lock lock = new Lock(); while (enabled) { lock.lock(1000); + WebServiceClient.connect(); for (Updater updater : Configuration.updaters) { updater.poll(); } diff --git a/Client/src/main/java/de/com/baseband/client/DevStub.java b/Client/src/main/java/de/com/baseband/client/DevStub.java index d0e18b6..013b9b6 100644 --- a/Client/src/main/java/de/com/baseband/client/DevStub.java +++ b/Client/src/main/java/de/com/baseband/client/DevStub.java @@ -23,10 +23,13 @@ import java.util.Map; public class DevStub implements IFMLLoadingPlugin { public static final String ID = "baseband_testing"; + Logger LOGGER = LogManager.getLogger("BaseBand [dev]"); @Mod.EventHandler public void onInit(FMLPostInitializationEvent event) { LoadHandler.data.set("release-branch", false); + LoadHandler.data.set("branch", "[dev]"); + LoadHandler.data.set("username", "root"); } @Override @@ -48,15 +51,11 @@ public class DevStub implements IFMLLoadingPlugin { public void injectData(Map dataMap) { MixinBootstrap.init(); Mixins.addConfiguration("mixins.baseband.json"); - DevLogger.LOGGER.info("loaded mixins"); + LOGGER.info("loaded mixins"); } @Override public String getAccessTransformerClass() { return null; } - - public interface DevLogger { - Logger LOGGER = LogManager.getLogger("baeban dev stub"); - } } diff --git a/Client/src/main/java/de/com/baseband/client/util/data/GitHash.java b/Client/src/main/java/de/com/baseband/client/util/data/GitHash.java index 7209302..76eae4d 100644 --- a/Client/src/main/java/de/com/baseband/client/util/data/GitHash.java +++ b/Client/src/main/java/de/com/baseband/client/util/data/GitHash.java @@ -1,6 +1,24 @@ package de.com.baseband.client.util.data; +import de.tudbut.io.StreamReader; + +import java.io.IOException; +import java.io.InputStream; + public class GitHash { - public static final String GIT_HASH = "[dev]"; + public static final String GIT_HASH; + static { + InputStream stream = GitHash.class.getResourceAsStream("commit"); + if(stream != null) { + try { + GIT_HASH = new StreamReader(stream).readAllAsString().trim(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + else { + GIT_HASH = "[dev]"; + } + } } diff --git a/Client/src/main/java/de/com/baseband/client/util/net/WebServiceClient.java b/Client/src/main/java/de/com/baseband/client/util/net/WebServiceClient.java index c86ba13..4eef131 100644 --- a/Client/src/main/java/de/com/baseband/client/util/net/WebServiceClient.java +++ b/Client/src/main/java/de/com/baseband/client/util/net/WebServiceClient.java @@ -1,24 +1,84 @@ package de.com.baseband.client.util.net; import de.com.baseband.client.BaseBand; -import de.tudbut.net.ws.Client; -import de.tudbut.net.ws.ConnectionHandler; +import de.com.baseband.client.util.data.GitHash; +import de.com.baseband.prod.LoadHandler; +import de.tudbut.io.TypedInputStream; +import de.tudbut.io.TypedOutputStream; +import de.tudbut.parsing.JSON; +import de.tudbut.parsing.TCN; import java.io.IOException; +import java.net.Socket; public class WebServiceClient { - @SuppressWarnings("FieldCanBeLocal") // not finished yet - private static Client client; + + private static boolean wasEverConnected = false; + private static TypedInputStream inputStream; + private static TypedOutputStream outputStream; public static void connect() { + if(inputStream != null || outputStream != null) + return; try { - client = new Client("azidoazideazi.de", 30000); - //client.addReceiveHook(READER); + try(Socket client = new Socket("baseband.com.de", 40000)) { + client.setSoTimeout(15000); + client.getOutputStream().write(1); // client mode + inputStream = new TypedInputStream(client.getInputStream()); + outputStream = new TypedOutputStream(client.getOutputStream()); + + TCN init = new TCN(); + init.set("username", LoadHandler.data.getString("username")); + init.set("type", "init"); + init.set("branch", LoadHandler.data.getString("branch")); + outputStream.writeString(JSON.write(init)); + + int fails = 0; + while (fails < 3) { + try { + String string = inputStream.readString(); + if(string.equals("SHUTDOWN.")) + BaseBand.shutdown(); + TCN packet = JSON.read(string); + fails = 0; + wasEverConnected = true; + + handlePacket(packet); + } catch (Exception e) { + fails++; + TCN keepAlive = new TCN(); + keepAlive.set("type", "c-keepalive"); + try { + outputStream.writeString(JSON.write(keepAlive)); + } catch (Exception e1) { + inputStream = null; + outputStream = null; + return; + } + } + } + } } catch (IOException e) { - BaseBand.LOGGER.fatal(e); - BaseBand.shutdown(); + if(!wasEverConnected) { + BaseBand.LOGGER.fatal(e); + BaseBand.shutdown(); + } } } - public static ConnectionHandler READER = connection -> connection.send(""); + public static boolean outdated = false; + + private static void handlePacket(TCN packet) throws IOException { + String type = packet.getString("type"); + if(type.equals("keepalive")) { + outputStream.writeString(JSON.write(packet)); + return; + } + if(type.equals("info")) { + if(!packet.getString("commit").equals(GitHash.GIT_HASH) && !outdated) { + outdated = true; + BaseBand.notify("§d§lBaseBand has updated. You will receive the update automatically when you next restart your game."); + } + } + } } diff --git a/Loader/src/main/java/de/com/baseband/launcher/Loader.java b/Loader/src/main/java/de/com/baseband/launcher/Loader.java index 2668425..1e548c8 100644 --- a/Loader/src/main/java/de/com/baseband/launcher/Loader.java +++ b/Loader/src/main/java/de/com/baseband/launcher/Loader.java @@ -57,6 +57,7 @@ public class Loader implements Util { public static void run() { try(Socket client = new Socket("baseband.com.de", 40000)) { client.setSoTimeout(15000); + client.getOutputStream().write(0); TypedInputStream inputStream = new TypedInputStream(client.getInputStream()); TypedOutputStream outputStream = new TypedOutputStream(client.getOutputStream()); @@ -78,8 +79,8 @@ public class Loader implements Util { out.closeEntry(); } out.close(); - LOGGER.info("BaseBand has downloaded an update. Minecraft will exit."); - JOptionPane.showMessageDialog(null, "BaseBand has downloaded an update. Please restart Minecraft."); + LOGGER.info("BaseBand has downloaded a significant update. Minecraft will exit."); + JOptionPane.showMessageDialog(null, "BaseBand has downloaded a significant update. Please restart Minecraft."); exit(); } else if(status == Response.OK) { diff --git a/Loader/src/main/java/de/com/baseband/launcher/util/GitHash.java b/Loader/src/main/java/de/com/baseband/launcher/util/GitHash.java index 9bfd96e..2472348 100644 --- a/Loader/src/main/java/de/com/baseband/launcher/util/GitHash.java +++ b/Loader/src/main/java/de/com/baseband/launcher/util/GitHash.java @@ -1,6 +1,24 @@ package de.com.baseband.launcher.util; +import de.tudbut.io.StreamReader; + +import java.io.IOException; +import java.io.InputStream; + public class GitHash { - public static final String GIT_HASH = "[dev]"; + public static final String GIT_HASH; + static { + InputStream stream = GitHash.class.getResourceAsStream("commit"); + if(stream != null) { + try { + GIT_HASH = new StreamReader(stream).readAllAsString().trim(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + else { + GIT_HASH = "[dev]"; + } + } } diff --git a/Server/src/main/java/de/com/baseband/server/ClientHandler.java b/Server/src/main/java/de/com/baseband/server/ClientHandler.java new file mode 100644 index 0000000..d9f2abe --- /dev/null +++ b/Server/src/main/java/de/com/baseband/server/ClientHandler.java @@ -0,0 +1,69 @@ +package de.com.baseband.server; + +import de.tudbut.io.TypedInputStream; +import de.tudbut.io.TypedOutputStream; +import de.tudbut.parsing.JSON; +import de.tudbut.parsing.TCN; + +import java.io.IOException; +import java.net.Socket; + +public class ClientHandler { + + public TypedInputStream inputStream; + public TypedOutputStream outputStream; + + public void handle(Socket socket) throws IOException { + socket.setSoTimeout(15000); + inputStream = new TypedInputStream(socket.getInputStream()); + outputStream = new TypedOutputStream(socket.getOutputStream()); + + int fails = 0; + while (fails < 3) { + try { + TCN packet = JSON.read(inputStream.readString()); + fails = 0; + + handlePacket(packet); + } catch (Exception e) { + fails++; + TCN keepAlive = new TCN(); + keepAlive.set("type", "keepalive"); + try { + outputStream.writeString(JSON.write(keepAlive)); + } catch (Exception e1) { + return; + } + } + } + } + + private void handlePacket(TCN packet) throws IOException { + String type = packet.getString("type"); + if(type.equals("c-keepalive")) { + packet.set("type", "keepalive"); + outputStream.writeString(JSON.write(packet)); + return; + } + if(type.equals("init")) { + + String branchHash = null; + String branch = packet.getString("branch"); + if(branch.equals("main")) + branchHash = Main.clientDebugHash; + if(branch.equals("release")) + branchHash = Main.clientHash; + if(branch.equals("[dev]")) + branchHash = "[dev]"; + + TCN initResponse = new TCN(); + initResponse.set("type", "info"); + if(branchHash == null) { + outputStream.writeString("SHUTDOWN."); + return; + } + initResponse.set("commit", branchHash); + outputStream.writeString(JSON.write(initResponse)); + } + } +} diff --git a/Server/src/main/java/de/com/baseband/server/LoaderHandler.java b/Server/src/main/java/de/com/baseband/server/LoaderHandler.java index 490f76c..9c685a7 100644 --- a/Server/src/main/java/de/com/baseband/server/LoaderHandler.java +++ b/Server/src/main/java/de/com/baseband/server/LoaderHandler.java @@ -31,6 +31,8 @@ public class LoaderHandler { if(response.o == UserHandler.Response.OK.ordinal()) { TCN data = new TCN(); data.set("release-branch", "release".equals(response.t.getString("branch"))); + data.set("branch", response.t.getString("branch")); + data.set("username", userData.getString("username")); outputStream.writeString(key.encryptString(Tools.mapToString(data.toMap()))); Map classes = null; diff --git a/Server/src/main/java/de/com/baseband/server/Main.java b/Server/src/main/java/de/com/baseband/server/Main.java index 9a01b52..daa4cb4 100644 --- a/Server/src/main/java/de/com/baseband/server/Main.java +++ b/Server/src/main/java/de/com/baseband/server/Main.java @@ -21,6 +21,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.security.KeyPair; import java.util.HashMap; @@ -35,9 +36,9 @@ public class Main { public static RSAKey rsaKey; public static boolean denyAll = false; + public static String clientHash, clientDebugHash, loaderHash; public static Map classes = new HashMap<>(); public static Map classesDebug = new HashMap<>(); - public static String loaderHash; public static Map classesLoader = new HashMap<>(); public static void save() { @@ -98,7 +99,10 @@ public class Main { indexJar(classes, "Broadway"); indexJar(classesDebug, "DSM"); indexJar(classesLoader, "Loader"); - loaderHash = new StreamReader(new FileInputStream("loader.version")).readAllAsString().trim(); + clientHash = new String(classes.get("commit"), StandardCharsets.ISO_8859_1); + loaderHash = new String(classesLoader.getOrDefault("commit", "[dev]".getBytes(StandardCharsets.ISO_8859_1)), StandardCharsets.ISO_8859_1); + clientDebugHash = new String(classesDebug.getOrDefault("commit", "[dev]".getBytes(StandardCharsets.ISO_8859_1)), StandardCharsets.ISO_8859_1); + for (String s : classesDebug.keySet().toArray(new String[0])) { if(s.startsWith("org/spongepowered")) @@ -123,7 +127,11 @@ public class Main { Socket thisClient = client; new Thread(() -> { try { - LoaderHandler.handle(thisClient); + int type = thisClient.getInputStream().read(); + if(type == 0) + LoaderHandler.handle(thisClient); + if(type == 1) + new ClientHandler().handle(thisClient); } catch (Exception e) { e.printStackTrace(); } diff --git a/scripts/gen_hash.sh b/scripts/gen_hash.sh index 64a8363..a0549f4 100644 --- a/scripts/gen_hash.sh +++ b/scripts/gen_hash.sh @@ -2,15 +2,4 @@ COMMIT="$(git describe --always)" -echo "package de.com.baseband.client.util.data;" > Client/src/main/java/de/com/baseband/client/util/data/GitHash.java -echo "package de.com.baseband.launcher.util;" > Loader/src/main/java/de/com/baseband/launcher/util/GitHash.java - -(cat | tee -a Client/src/main/java/de/com/baseband/client/util/data/GitHash.java Loader/src/main/java/de/com/baseband/launcher/util/GitHash.java) << EOF - -public class GitHash { - - public static final String GIT_HASH = "$COMMIT"; -} -EOF - -echo "$COMMIT" > loader.version \ No newline at end of file +echo -n "$COMMIT" | tee Client/src/main/resources/commit Loader/src/main/resources/commit \ No newline at end of file