66package meteordevelopment .meteorclient .systems .accounts ;
77
88import com .sun .net .httpserver .HttpExchange ;
9- import com .sun .net .httpserver .HttpHandler ;
109import com .sun .net .httpserver .HttpServer ;
10+ import meteordevelopment .meteorclient .MeteorClient ;
1111import meteordevelopment .meteorclient .utils .network .Http ;
12- import meteordevelopment .meteorclient .utils .network .MeteorExecutor ;
1312import net .minecraft .util .Util ;
1413import org .apache .http .NameValuePair ;
1514import org .apache .http .client .utils .URLEncodedUtils ;
1615
1716import java .io .IOException ;
18- import java .io .OutputStream ;
1917import java .net .InetSocketAddress ;
2018import java .nio .charset .StandardCharsets ;
2119import java .util .List ;
20+ import java .util .concurrent .Executors ;
2221import java .util .function .Consumer ;
2322
2423public 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
0 commit comments