Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@
package meteordevelopment.meteorclient.systems.accounts;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.utils.network.Http;
import meteordevelopment.meteorclient.utils.network.MeteorExecutor;
import net.minecraft.util.Util;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

public class MicrosoftLogin {
Expand Down Expand Up @@ -47,8 +46,8 @@ public boolean isGood() {
private static final String CLIENT_ID = "4673b348-3efa-4f6a-bbb6-34e141cdc638";
private static final int PORT = 9675;

private static HttpServer server;
private static Consumer<String> callback;
private static volatile HttpServer server;
private static volatile Consumer<String> callback;

public static String getRefreshToken(Consumer<String> callback) {
MicrosoftLogin.callback = callback;
Expand Down Expand Up @@ -115,11 +114,12 @@ private static void startServer() {
try {
server = HttpServer.create(new InetSocketAddress("127.0.0.1", PORT), 0);

server.createContext("/", new Handler());
server.setExecutor(MeteorExecutor.executor);
server.createContext("/", MicrosoftLogin::handleRequest);
server.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
server.start();
} catch (IOException e) {
e.printStackTrace();
MeteorClient.LOG.error("Error starting Microsoft login server", e);
stopServer();
}
}

Expand All @@ -132,51 +132,46 @@ public static void stopServer() {
callback = null;
}

private static class Handler implements HttpHandler {
@Override
public void handle(HttpExchange req) throws IOException {
if (req.getRequestMethod().equals("GET")) {
// Login
List<NameValuePair> query = URLEncodedUtils.parse(req.getRequestURI(), StandardCharsets.UTF_8);

boolean ok = false;
private static void handleRequest(HttpExchange req) throws IOException {
if (req.getRequestMethod().equals("GET")) {
// Login
List<NameValuePair> query = URLEncodedUtils.parse(req.getRequestURI(), StandardCharsets.UTF_8);

for (NameValuePair pair : query) {
if (pair.getName().equals("code")) {
handleCode(pair.getValue());
boolean ok = false;

ok = true;
break;
}
}
for (NameValuePair pair : query) {
if (pair.getName().equals("code")) {
handleCode(pair.getValue());

if (!ok) {
writeText(req, "Cannot authenticate.");
callback.accept(null);
ok = true;
break;
}
else writeText(req, "You may now close this page.");
}

stopServer();
if (!ok) {
writeText(req, "Cannot authenticate.");
callback.accept(null);
}
else writeText(req, "You may now close this page.");
}

private void handleCode(String code) {
AuthTokenResponse res = Http.post("https://login.live.com/oauth20_token.srf")
.bodyForm("client_id=" + CLIENT_ID + "&code=" + code + "&grant_type=authorization_code&redirect_uri=http://127.0.0.1:" + PORT)
.sendJson(AuthTokenResponse.class);

if (res == null) callback.accept(null);
else callback.accept(res.refresh_token);
}
stopServer();
}

private void writeText(HttpExchange req, String text) throws IOException {
OutputStream out = req.getResponseBody();
private static void handleCode(String code) {
AuthTokenResponse res = Http.post("https://login.live.com/oauth20_token.srf")
.bodyForm("client_id=" + CLIENT_ID + "&code=" + code + "&grant_type=authorization_code&redirect_uri=http://127.0.0.1:" + PORT)
.sendJson(AuthTokenResponse.class);

req.sendResponseHeaders(200, text.length());
if (res == null) callback.accept(null);
else callback.accept(res.refresh_token);
}

out.write(text.getBytes(StandardCharsets.UTF_8));
out.flush();
out.close();
private static void writeText(HttpExchange req, String text) throws IOException {
byte[] responseBody = text.getBytes(StandardCharsets.UTF_8);
req.sendResponseHeaders(200, responseBody.length);
try (var out = req.getResponseBody()) {
out.write(responseBody);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.stream.Stream;

Expand All @@ -29,7 +30,9 @@ public class Http {
public static final int FORBIDDEN = 403;
public static final int NOT_FOUND = 404;

private static final HttpClient CLIENT = HttpClient.newHttpClient();
private static final HttpClient CLIENT = HttpClient.newBuilder()
.executor(Executors.newVirtualThreadPerTaskExecutor())
.build();

private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(Date.class, new JsonDateDeserializer())
Expand Down