Skip to content

Commit 1b4b35c

Browse files
committed
ms login + http client
1 parent 70706f2 commit 1b4b35c

File tree

2 files changed

+41
-43
lines changed

2 files changed

+41
-43
lines changed

src/main/java/meteordevelopment/meteorclient/systems/accounts/MicrosoftLogin.java

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
package meteordevelopment.meteorclient.systems.accounts;
77

88
import com.sun.net.httpserver.HttpExchange;
9-
import com.sun.net.httpserver.HttpHandler;
109
import com.sun.net.httpserver.HttpServer;
10+
import meteordevelopment.meteorclient.MeteorClient;
1111
import meteordevelopment.meteorclient.utils.network.Http;
12-
import meteordevelopment.meteorclient.utils.network.MeteorExecutor;
1312
import net.minecraft.util.Util;
1413
import org.apache.http.NameValuePair;
1514
import org.apache.http.client.utils.URLEncodedUtils;
1615

1716
import java.io.IOException;
18-
import java.io.OutputStream;
1917
import java.net.InetSocketAddress;
2018
import java.nio.charset.StandardCharsets;
2119
import java.util.List;
20+
import java.util.concurrent.Executors;
2221
import java.util.function.Consumer;
2322

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

50-
private static HttpServer server;
51-
private static Consumer<String> callback;
49+
private static volatile HttpServer server;
50+
private static volatile Consumer<String> callback;
5251

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

118-
server.createContext("/", new Handler());
119-
server.setExecutor(MeteorExecutor.executor);
117+
server.createContext("/", MicrosoftLogin::handleRequest);
118+
server.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
120119
server.start();
121120
} catch (IOException e) {
122-
e.printStackTrace();
121+
MeteorClient.LOG.error("Error starting Microsoft login server", e);
122+
stopServer();
123123
}
124124
}
125125

@@ -132,51 +132,46 @@ public static void stopServer() {
132132
callback = null;
133133
}
134134

135-
private static class Handler implements HttpHandler {
136-
@Override
137-
public void handle(HttpExchange req) throws IOException {
138-
if (req.getRequestMethod().equals("GET")) {
139-
// Login
140-
List<NameValuePair> query = URLEncodedUtils.parse(req.getRequestURI(), StandardCharsets.UTF_8);
141-
142-
boolean ok = false;
135+
private static void handleRequest(HttpExchange req) throws IOException {
136+
if (req.getRequestMethod().equals("GET")) {
137+
// Login
138+
List<NameValuePair> query = URLEncodedUtils.parse(req.getRequestURI(), StandardCharsets.UTF_8);
143139

144-
for (NameValuePair pair : query) {
145-
if (pair.getName().equals("code")) {
146-
handleCode(pair.getValue());
140+
boolean ok = false;
147141

148-
ok = true;
149-
break;
150-
}
151-
}
142+
for (NameValuePair pair : query) {
143+
if (pair.getName().equals("code")) {
144+
handleCode(pair.getValue());
152145

153-
if (!ok) {
154-
writeText(req, "Cannot authenticate.");
155-
callback.accept(null);
146+
ok = true;
147+
break;
156148
}
157-
else writeText(req, "You may now close this page.");
158149
}
159150

160-
stopServer();
151+
if (!ok) {
152+
writeText(req, "Cannot authenticate.");
153+
callback.accept(null);
154+
}
155+
else writeText(req, "You may now close this page.");
161156
}
162157

163-
private void handleCode(String code) {
164-
AuthTokenResponse res = Http.post("https://login.live.com/oauth20_token.srf")
165-
.bodyForm("client_id=" + CLIENT_ID + "&code=" + code + "&grant_type=authorization_code&redirect_uri=http://127.0.0.1:" + PORT)
166-
.sendJson(AuthTokenResponse.class);
167-
168-
if (res == null) callback.accept(null);
169-
else callback.accept(res.refresh_token);
170-
}
158+
stopServer();
159+
}
171160

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

175-
req.sendResponseHeaders(200, text.length());
166+
if (res == null) callback.accept(null);
167+
else callback.accept(res.refresh_token);
168+
}
176169

177-
out.write(text.getBytes(StandardCharsets.UTF_8));
178-
out.flush();
179-
out.close();
170+
private static void writeText(HttpExchange req, String text) throws IOException {
171+
byte[] responseBody = text.getBytes(StandardCharsets.UTF_8);
172+
req.sendResponseHeaders(200, responseBody.length);
173+
try (var out = req.getResponseBody()) {
174+
out.write(responseBody);
180175
}
181176
}
182177

src/main/java/meteordevelopment/meteorclient/utils/network/Http.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.net.http.HttpRequest;
2020
import java.net.http.HttpResponse;
2121
import java.util.Date;
22+
import java.util.concurrent.Executors;
2223
import java.util.function.Consumer;
2324
import java.util.stream.Stream;
2425

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

32-
private static final HttpClient CLIENT = HttpClient.newHttpClient();
33+
private static final HttpClient CLIENT = HttpClient.newBuilder()
34+
.executor(Executors.newVirtualThreadPerTaskExecutor())
35+
.build();
3336

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

0 commit comments

Comments
 (0)