Skip to content

Commit 2872efd

Browse files
committed
Add dumpsession command
1 parent 146b425 commit 2872efd

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

bootstrap/geyser/src/main/java/com/rtm516/mcxboxbroadcast/bootstrap/geyser/MCXboxBroadcastExtension.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ public void onCommandDefine(GeyserDefineCommandsEvent event) {
7474
createSession();
7575
})
7676
.build());
77+
78+
event.register(Command.builder(this)
79+
.source(CommandSource.class)
80+
.name("dumpsession")
81+
.description("Dump the current session to json files.")
82+
.executor((source, command, args) -> {
83+
if (!source.isConsole()) {
84+
source.sendMessage("This command can only be ran from the console.");
85+
return;
86+
}
87+
88+
sessionManager.dumpSession();
89+
})
90+
.build());
7791
}
7892

7993
@Subscribe

bootstrap/standalone/src/main/java/com/rtm516/mcxboxbroadcast/bootstrap/standalone/StandaloneLoggerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ protected void runCommand(String command) {
5353
switch (commandNode) {
5454
case "exit" -> System.exit(0);
5555
case "restart" -> StandaloneMain.restart();
56+
case "dumpsession" -> StandaloneMain.sessionManager.dumpSession();
5657
default -> logger.warn("Unknown command: {}", commandNode);
5758
}
5859
} catch (Exception e) {

bootstrap/standalone/src/main/java/com/rtm516/mcxboxbroadcast/bootstrap/standalone/StandaloneMain.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626
public class StandaloneMain {
2727
private static StandaloneConfig config;
2828
private static StandaloneLoggerImpl logger;
29-
private static SessionManager sessionManager;
3029
private static SessionInfo sessionInfo;
3130
private static ScheduledExecutorService scheduledThreadPool;
3231

32+
public static SessionManager sessionManager;
33+
3334
public static void main(String[] args) throws Exception {
3435
logger = new StandaloneLoggerImpl(LoggerFactory.getLogger(StandaloneMain.class));
3536

core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManager.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.rtm516.mcxboxbroadcast.core.models.XboxTokenInfo;
1111

1212
import java.io.File;
13+
import java.io.FileWriter;
1314
import java.io.IOException;
1415
import java.net.URI;
1516
import java.net.http.HttpClient;
@@ -30,10 +31,13 @@ public class SessionManager {
3031
private final XboxTokenManager xboxTokenManager;
3132
private final HttpClient httpClient;
3233
private final Logger logger;
34+
private final String cache;
3335

3436
private RtaWebsocketClient rtaWebsocket;
3537
private ExpandedSessionInfo sessionInfo;
3638

39+
private String lastSessionResponse;
40+
3741
/**
3842
* Create an instance of SessionManager using default values
3943
*/
@@ -63,6 +67,7 @@ public SessionManager(String cache, Logger logger) {
6367
.build();
6468

6569
this.logger = logger;
70+
this.cache = cache;
6671

6772
this.liveTokenManager = new LiveTokenManager(cache, httpClient, logger);
6873
this.xboxTokenManager = new XboxTokenManager(cache, httpClient, logger);
@@ -239,8 +244,10 @@ private void updateSession() throws SessionUpdateException {
239244
throw new SessionUpdateException(e.getMessage());
240245
}
241246

247+
lastSessionResponse = createSessionResponse.body();
248+
242249
if (createSessionResponse.statusCode() != 200 && createSessionResponse.statusCode() != 201) {
243-
logger.debug("Got session response: " + createSessionResponse.body());
250+
logger.debug("Got session response: " + lastSessionResponse);
244251
throw new SessionUpdateException("Unable to update session information, got status " + createSessionResponse.statusCode() + " trying to update");
245252
}
246253
}
@@ -424,4 +431,35 @@ public void stopSession() {
424431
rtaWebsocket.close();
425432
this.sessionInfo.setSessionId(null);
426433
}
434+
435+
public void dumpSession() {
436+
logger.info("Dumping current and last session responses");
437+
try {
438+
FileWriter file = new FileWriter(this.cache + "/lastSessionResponse.json");
439+
file.write(lastSessionResponse);
440+
file.close();
441+
} catch (IOException e) {
442+
logger.error("Error dumping last session: " + e.getMessage());
443+
}
444+
445+
HttpRequest createSessionRequest = HttpRequest.newBuilder()
446+
.uri(URI.create(Constants.CREATE_SESSION + this.sessionInfo.getSessionId()))
447+
.header("Content-Type", "application/json")
448+
.header("Authorization", getTokenHeader())
449+
.header("x-xbl-contract-version", "107")
450+
.GET()
451+
.build();
452+
453+
try {
454+
HttpResponse<String> createSessionResponse = httpClient.send(createSessionRequest, HttpResponse.BodyHandlers.ofString());
455+
456+
FileWriter file = new FileWriter(this.cache + "/currentSessionResponse.json");
457+
file.write(createSessionResponse.body());
458+
file.close();
459+
} catch (IOException | InterruptedException e) {
460+
logger.error("Error dumping current session: " + e.getMessage());
461+
}
462+
463+
logger.info("Dumped session responses to 'lastSessionResponse.json' and 'currentSessionResponse.json'");
464+
}
427465
}

0 commit comments

Comments
 (0)