From 1b4b35c217588353bc604548c7bda174da5a2480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E8=91=89=20Scarlet?= <93977077+mukjepscarlet@users.noreply.github.com> Date: Tue, 4 Nov 2025 01:44:56 +0800 Subject: [PATCH] ms login + http client --- .../systems/accounts/MicrosoftLogin.java | 79 +++++++++---------- .../meteorclient/utils/network/Http.java | 5 +- 2 files changed, 41 insertions(+), 43 deletions(-) diff --git a/src/main/java/meteordevelopment/meteorclient/systems/accounts/MicrosoftLogin.java b/src/main/java/meteordevelopment/meteorclient/systems/accounts/MicrosoftLogin.java index 2f246cc9c7..32818a41da 100644 --- a/src/main/java/meteordevelopment/meteorclient/systems/accounts/MicrosoftLogin.java +++ b/src/main/java/meteordevelopment/meteorclient/systems/accounts/MicrosoftLogin.java @@ -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 { @@ -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 callback; + private static volatile HttpServer server; + private static volatile Consumer callback; public static String getRefreshToken(Consumer callback) { MicrosoftLogin.callback = callback; @@ -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(); } } @@ -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 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 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); } } diff --git a/src/main/java/meteordevelopment/meteorclient/utils/network/Http.java b/src/main/java/meteordevelopment/meteorclient/utils/network/Http.java index f74b7b04f5..9245689f6f 100644 --- a/src/main/java/meteordevelopment/meteorclient/utils/network/Http.java +++ b/src/main/java/meteordevelopment/meteorclient/utils/network/Http.java @@ -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; @@ -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())