From 53533f823e5e0437f65934cfb31263b3697f7446 Mon Sep 17 00:00:00 2001 From: PandaNinjas Date: Sat, 24 Dec 2022 19:18:25 -0800 Subject: Improvements --- src/main/java/gq/malwarefight/tokenapp/Main.java | 12 ++--- .../gq/malwarefight/tokenapp/SocketThread.java | 54 +++++++++++----------- 2 files changed, 31 insertions(+), 35 deletions(-) (limited to 'src/main/java/gq/malwarefight/tokenapp') diff --git a/src/main/java/gq/malwarefight/tokenapp/Main.java b/src/main/java/gq/malwarefight/tokenapp/Main.java index 750ae83..66afad1 100644 --- a/src/main/java/gq/malwarefight/tokenapp/Main.java +++ b/src/main/java/gq/malwarefight/tokenapp/Main.java @@ -15,7 +15,7 @@ import java.util.UUID; public class Main { public static final int BASE_PORT = 47777; - public static volatile boolean running = true; + public static long ID; public static YggdrasilMinecraftSessionService sessionService = null; public static YggdrasilAuthenticationService authenticationService = null; public static GameProfile gameProfile = null; @@ -40,6 +40,7 @@ public class Main { } public static void main(String[] args) { + ID = Long.parseLong(args[0]); try { setup(); } catch (Exception e) { @@ -49,25 +50,22 @@ public class Main { ServerSocket sock = null; for (int i = BASE_PORT; i < BASE_PORT + 10; i++) { try { - System.out.println(i); //noinspection resource sock = new ServerSocket(i, 50, InetAddress.getLoopbackAddress()); - System.out.println(i); break; - } catch (Exception ignored) { - } + } catch (Exception ignored) {} } if (sock == null) { System.err.println("Could not bind to any valid port"); System.exit(1); } - while (running) { + while (true) { try { Socket connection = sock.accept(); Thread t = new SocketThread(connection); t.start(); } catch (IOException ignored) { - running = false; + System.exit(0); } } } diff --git a/src/main/java/gq/malwarefight/tokenapp/SocketThread.java b/src/main/java/gq/malwarefight/tokenapp/SocketThread.java index 572ca57..05db6f0 100644 --- a/src/main/java/gq/malwarefight/tokenapp/SocketThread.java +++ b/src/main/java/gq/malwarefight/tokenapp/SocketThread.java @@ -1,13 +1,13 @@ package gq.malwarefight.tokenapp; import com.mojang.authlib.exceptions.AuthenticationException; +import gq.malwarefight.nosession.mixin.Utils; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.nio.charset.StandardCharsets; -import java.util.Scanner; public class SocketThread extends Thread { private final Socket sock; @@ -27,37 +27,35 @@ public class SocketThread extends Thread { try { InputStream in = sock.getInputStream(); OutputStream out = sock.getOutputStream(); - Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.name()); while (true) { - if (scanner.hasNextLine()) { - String line = scanner.nextLine(); - String[] parts = line.split(" "); - if ("login".equals(parts[0])) { - try { - Main.sessionService.joinServer( - Main.gameProfile, Main.authenticationService.getClientToken(), parts[1] - ); - } catch (Exception e) { - if (e instanceof ArrayIndexOutOfBoundsException) { - writeResponse("400 Bad Request", out); - } else if (e instanceof AuthenticationException) { - writeResponse("401 Unauthorized", out); - } else { - writeResponse("500 Internal Server Error", out); - } - continue; + String line = Utils.readString(in, '\n'); + String[] parts = line.split(" "); + if ("login".equals(parts[0])) { + try { + Main.sessionService.joinServer( + Main.gameProfile, Main.authenticationService.getClientToken(), parts[1] + ); + } catch (Exception e) { + if (e instanceof ArrayIndexOutOfBoundsException) { + writeResponse("400 Bad Request", out); + } else if (e instanceof AuthenticationException) { + writeResponse("401 Unauthorized", out); + } else { + writeResponse("500 Internal Server Error", out); } - writeResponse("200 OK", out); - } else if ("disconnect".equals(parts[0])) { - sock.close(); - } else if ("fullquit".equals(parts[0])) { - Main.running = false; - sock.close(); - return; - } else { - writeResponse("418 I'm a teapot", out); + continue; } + writeResponse("200 OK", out); + } else if ("disconnect".equals(parts[0])) { + sock.close(); + } else if ("fullquit".equals(parts[0])) { + System.exit(0); + } else if ("id".equals(parts[0])) { + writeResponse(Long.toString(Main.ID), out); + } else { + writeResponse("418 I'm a teapot", out); } + } } catch (IOException ignored) {} } -- cgit