diff options
Diffstat (limited to 'src/main/java/gq/malwarefight/tokenapp')
-rw-r--r-- | src/main/java/gq/malwarefight/tokenapp/Main.java | 75 | ||||
-rw-r--r-- | src/main/java/gq/malwarefight/tokenapp/SocketThread.java | 64 |
2 files changed, 139 insertions, 0 deletions
diff --git a/src/main/java/gq/malwarefight/tokenapp/Main.java b/src/main/java/gq/malwarefight/tokenapp/Main.java new file mode 100644 index 0000000..750ae83 --- /dev/null +++ b/src/main/java/gq/malwarefight/tokenapp/Main.java @@ -0,0 +1,75 @@ +package gq.malwarefight.tokenapp; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.mojang.authlib.GameProfile; +import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService; +import com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService; +import gq.malwarefight.nosession.mixin.Utils; +import org.apache.commons.lang3.exception.ExceptionUtils; + +import javax.net.ssl.HttpsURLConnection; +import java.io.IOException; +import java.net.*; +import java.util.UUID; + +public class Main { + public static final int BASE_PORT = 47777; + public static volatile boolean running = true; + public static YggdrasilMinecraftSessionService sessionService = null; + public static YggdrasilAuthenticationService authenticationService = null; + public static GameProfile gameProfile = null; + + public static void setup() throws IOException { + String token = Utils.readString(System.in, '\n'); + YggdrasilAuthenticationService yas = new YggdrasilAuthenticationService(Proxy.NO_PROXY, token); + authenticationService = yas; + sessionService = (YggdrasilMinecraftSessionService) yas.createMinecraftSessionService(); + HttpsURLConnection httpsURLConnection = (HttpsURLConnection) (new URL("https://api.minecraftservices.com/minecraft/profile").openConnection()); + httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); + String response = Utils.readString(httpsURLConnection.getInputStream(), null); + JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject(); + UUID id = UUID.fromString( + jsonObject.get("id").getAsString().replaceFirst( + "(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5" + ) + ); + String name = jsonObject.get("name").getAsString(); + gameProfile = new GameProfile(id, name); + System.setProperty("java.net.preferIPv4Stack", "true"); + } + + public static void main(String[] args) { + try { + setup(); + } catch (Exception e) { + System.err.println("Could not setup the server\n" + ExceptionUtils.getStackTrace(e)); + System.exit(1); + } + 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) { + } + } + if (sock == null) { + System.err.println("Could not bind to any valid port"); + System.exit(1); + } + while (running) { + try { + Socket connection = sock.accept(); + Thread t = new SocketThread(connection); + t.start(); + } catch (IOException ignored) { + running = false; + } + } + } +} +
\ No newline at end of file diff --git a/src/main/java/gq/malwarefight/tokenapp/SocketThread.java b/src/main/java/gq/malwarefight/tokenapp/SocketThread.java new file mode 100644 index 0000000..572ca57 --- /dev/null +++ b/src/main/java/gq/malwarefight/tokenapp/SocketThread.java @@ -0,0 +1,64 @@ +package gq.malwarefight.tokenapp; + +import com.mojang.authlib.exceptions.AuthenticationException; + +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; + public SocketThread(Socket sock) { + this.sock = sock; + } + + public void writeResponse(String output, OutputStream outputStream) throws IOException { + outputStream.write( + (output + "\n").getBytes(StandardCharsets.UTF_8) + ); + outputStream.flush(); + } + + @Override + public void run() { + 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; + } + 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); + } + } + } + } catch (IOException ignored) {} + } +} |