package gq.malwarefight.tokenapp; import com.mojang.authlib.exceptions.AuthenticationException; import gq.malwarefight.nosession.utils.Utils; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.nio.charset.StandardCharsets; 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(); while (true) { 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); } continue; } writeResponse("200 OK", out); } else if ("disconnect".equals(parts[0])) { sock.close(); } else if ("fullquit".equals(parts[0])) { System.exit(0); } else if ("uuid".equals(parts[0])) { writeResponse(Main.gameProfile.getId().toString(), out); } else { writeResponse("418 I'm a teapot", out); } } } catch (IOException ignored) { // if something goes wrong, don't worry! just quit immediately } } }