diff options
Diffstat (limited to 'src/main/java/gq/malwarefight/nosession/mixin')
3 files changed, 99 insertions, 27 deletions
diff --git a/src/main/java/gq/malwarefight/nosession/mixin/InitialTweaker.java b/src/main/java/gq/malwarefight/nosession/mixin/InitialTweaker.java index 7d9c308..51425fc 100644 --- a/src/main/java/gq/malwarefight/nosession/mixin/InitialTweaker.java +++ b/src/main/java/gq/malwarefight/nosession/mixin/InitialTweaker.java @@ -21,10 +21,17 @@ import org.spongepowered.asm.mixin.Mixins; import java.io.File; import java.io.IOException; +import java.net.Socket; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; @SuppressWarnings({"unused", "unchecked"}) public class InitialTweaker implements ITweaker { @@ -56,16 +63,59 @@ public class InitialTweaker implements ITweaker { ); } + public static boolean createLockFile(long value) { + Path path = Paths.get(System.getProperty("java.io.tmpdir"), "NoSessionLock" + value); + try { + Files.createFile(path); + } catch (FileAlreadyExistsException e) { + System.out.println("You won the lottery! Two NoSession instances used the same ID. The chance that a new NoSession instance uses the same ID as an old one is (NoSession instances) / 2^64"); + return false; + } catch (IOException e) { + LogManager.getLogger().warn("Failed to create lockfile " + e.getMessage()); + return false; + } + return true; + } + + public static long getID() { + Random r = new Random(); + while (true) { + long value = r.nextLong(); + if (createLockFile(value)) { + Runtime.getRuntime().addShutdownHook( + new Thread(() -> { + try { + Files.delete(Paths.get(System.getProperty("java.io.tmpdir"), "NoSessionLock" + value)); + } catch (Exception ignored) {} + }) + ); + return value; + } + } + } public void setToken(String token) throws IOException, URISyntaxException { + long value = getID(); + Utils.ID = value; File javaExecutable = Paths.get(System.getProperty("java.home"), "bin", "java").toFile(); ProcessBuilder processBuilder = new ProcessBuilder( - javaExecutable.getAbsolutePath(), "-cp", getClasspath(), "gq.malwarefight.tokenapp.Main" + javaExecutable.getAbsolutePath(), "-cp", getClasspath(), "gq.malwarefight.tokenapp.Main", Long.toString(value) ); processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT).redirectError(ProcessBuilder.Redirect.INHERIT); Process c = processBuilder.start(); c.getOutputStream().write((token + "\n").getBytes(StandardCharsets.UTF_8)); c.getOutputStream().flush(); + Runtime.getRuntime().addShutdownHook( + new Thread(() -> { + try { + Socket socket = Utils.getProperSocket(); + socket.getOutputStream().write("fullquit\n".getBytes(StandardCharsets.UTF_8)); + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + }) + ); } /** @@ -81,6 +131,11 @@ public class InitialTweaker implements ITweaker { ArrayList<String> argsCopy = new ArrayList<>(args); for (int i = 0; i < argsCopy.size(); i++) { if (argsCopy.get(i).equals("--accessToken")) { + try { + setToken(args.get(i + 1)); + } catch (IOException | URISyntaxException e) { + throw new RuntimeException(e); + } argsCopy.set(i + 1, "<noSessionAccessToken>"); } } @@ -141,11 +196,6 @@ public class InitialTweaker implements ITweaker { ArrayList<String> launchArgs = (ArrayList<String>) Launch.blackboard.get("ArgumentList"); for (int i = 0; i < launchArgs.size(); i++) { if (launchArgs.get(i).equals("--accessToken")) { - try { - setToken(launchArgs.get(i + 1)); - } catch (Exception e) { - throw new RuntimeException(e); - } launchArgs.set(i + 1, "<noSessionAccessToken>"); } } diff --git a/src/main/java/gq/malwarefight/nosession/mixin/Utils.java b/src/main/java/gq/malwarefight/nosession/mixin/Utils.java index e67bddc..2aaec37 100644 --- a/src/main/java/gq/malwarefight/nosession/mixin/Utils.java +++ b/src/main/java/gq/malwarefight/nosession/mixin/Utils.java @@ -1,21 +1,24 @@ package gq.malwarefight.nosession.mixin; import gq.malwarefight.nosession.mixin.asm.ReplacingMethodVisitor; -import net.minecraft.launchwrapper.ITweaker; import net.minecraft.launchwrapper.Launch; import org.objectweb.asm.*; import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.AbstractList; -import java.util.ArrayList; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.Arrays; public class Utils { static int num = 0; + static int PORT = -1; + static long ID = -1; + private static final int BASE_PORT = 47777; public static byte[] read(InputStream i, Character delimiter) throws IOException { byte[] buffer = new byte[512]; @@ -39,7 +42,7 @@ public class Utils { } public static String readString(InputStream i, Character delimiter) throws IOException { - return new String(read(i, delimiter)); + return new String(read(i, delimiter), StandardCharsets.UTF_8); } public static void createClass(byte[] classArray, String name) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { @@ -86,4 +89,34 @@ public class Utils { num++; return className; } + + public static Socket getProperSocket() { + if (PORT == -1) { + Socket socket = null; + int port = 0; + for (int i = BASE_PORT; i < BASE_PORT + 10; i++) { + try { + socket = new Socket(); + socket.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), i)); + socket.getOutputStream().write("id\n".getBytes(StandardCharsets.UTF_8)); + String value = readString(socket.getInputStream(), '\n'); + if (value.equals(Long.toString(ID))) { + port = i; + break; + } + } catch (Exception ignored) {} + } + PORT = port; + return socket; + } else { + try { + Socket socket = new Socket(); + socket.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), PORT)); + return socket; + } catch (IOException e) { + PORT = -1; + return getProperSocket(); + } + } + } } diff --git a/src/main/java/gq/malwarefight/nosession/mixin/client/YggdrasilSessionMixin.java b/src/main/java/gq/malwarefight/nosession/mixin/client/YggdrasilSessionMixin.java index 440ca17..48327fe 100644 --- a/src/main/java/gq/malwarefight/nosession/mixin/client/YggdrasilSessionMixin.java +++ b/src/main/java/gq/malwarefight/nosession/mixin/client/YggdrasilSessionMixin.java @@ -3,40 +3,29 @@ package gq.malwarefight.nosession.mixin.client; import com.mojang.authlib.GameProfile; import com.mojang.authlib.exceptions.AuthenticationException; import com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService; +import gq.malwarefight.nosession.mixin.Utils; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; import java.net.Socket; import java.nio.charset.StandardCharsets; import java.util.Objects; -import java.util.Scanner; @Mixin(YggdrasilMinecraftSessionService.class) public class YggdrasilSessionMixin { - private static final int BASE_PORT = 47777; @Inject(method = "joinServer", at = @At("HEAD"), cancellable = true, remap = false) public void joinServer(GameProfile profile, String authenticationToken, String serverId, CallbackInfo ci) throws AuthenticationException { if (authenticationToken.equals("<noSessionAccessToken>")) { // this token has been messed with by the Tweaker - Socket socket = null; - for (int i = BASE_PORT; i < BASE_PORT + 10; i++) { - try { - socket = new Socket(); - socket.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), i)); - break; - } catch (Exception ignored) {} - } + Socket socket = Utils.getProperSocket(); try { assert socket != null; socket.getOutputStream().write(("login " + serverId + "\n").getBytes(StandardCharsets.UTF_8)); socket.getOutputStream().flush(); - Scanner result = new Scanner(socket.getInputStream(), StandardCharsets.UTF_8.name()); - String line = result.nextLine(); + String line = Utils.readString(socket.getInputStream(), '\n'); String[] parts = line.split(" "); if (Objects.equals(parts[0], "200")) { ci.cancel(); @@ -52,7 +41,7 @@ public class YggdrasilSessionMixin { } try { - socket.getOutputStream().write("disconnect".getBytes(StandardCharsets.UTF_8)); + socket.getOutputStream().write("disconnect\n".getBytes(StandardCharsets.UTF_8)); socket.close(); } catch (IOException ignored) {} } |