Skip to content

Commit

Permalink
Tidy up logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Aug 21, 2024
1 parent c60ac86 commit f237102
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.rtm516.mcxboxbroadcast.core.storage.FileStorageManager;
import org.cloudburstmc.protocol.bedrock.BedrockPong;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -20,6 +21,7 @@
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.LogManager;

public class StandaloneMain {
private static StandaloneConfig config;
Expand All @@ -29,6 +31,10 @@ public class StandaloneMain {
public static SessionManager sessionManager;

public static void main(String[] args) throws Exception {
// Redirect all logging to SLF4J since ice4j uses java.util.logging
LogManager.getLogManager().reset();
SLF4JBridgeHandler.install();

logger = new StandaloneLoggerImpl(LoggerFactory.getLogger(StandaloneMain.class));

logger.info("Starting MCXboxBroadcast Standalone " + BuildData.VERSION);
Expand Down
12 changes: 10 additions & 2 deletions bootstrap/standalone/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Configuration status="WARN">
<Appenders>
<TerminalConsole name="Console">
<PatternLayout pattern="[%d{HH:mm:ss} %style{%highlight{%level}{FATAL=red, ERROR=red, WARN=yellow bright, INFO=cyan bright, DEBUG=green, TRACE=white}}] %style{%highlight{%msg}{FATAL=red, ERROR=red, WARN=yellow bright, INFO=default, DEBUG=white dim, TRACE=white dim}}%n"/>
Expand All @@ -13,9 +13,17 @@
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="DEBUG" >
<Root level="INFO" >
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>

<!-- Disable logging for ice4j and jitsi -->
<Logger name="org.ice4j" level="OFF">
</Logger>
<Logger name="org.jitsi" level="OFF">
</Logger>
<Logger name="JitsiConfig" level="OFF">
</Logger>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id("com.rtm516.mcxboxbroadcast.java-conventions")
id("com.github.johnrengelman.shadow")
id("com.gradleup.shadow")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.rtm516.mcxboxbroadcast.core.webrtc;

import com.rtm516.mcxboxbroadcast.core.Logger;
import com.rtm516.mcxboxbroadcast.core.SessionInfo;
import com.rtm516.mcxboxbroadcast.core.webrtc.bedrock.RedirectPacketHandler;
import com.rtm516.mcxboxbroadcast.core.webrtc.encryption.BedrockEncryptionEncoder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import javax.crypto.SecretKey;
import org.bouncycastle.util.encoders.Hex;

import org.cloudburstmc.protocol.bedrock.codec.BedrockCodec;
import org.cloudburstmc.protocol.bedrock.codec.BedrockCodecHelper;
import org.cloudburstmc.protocol.bedrock.data.DisconnectFailReason;
import org.cloudburstmc.protocol.bedrock.netty.BedrockPacketWrapper;
import org.cloudburstmc.protocol.bedrock.netty.codec.packet.BedrockPacketCodec;
import org.cloudburstmc.protocol.bedrock.netty.codec.packet.BedrockPacketCodec_v3;
import org.cloudburstmc.protocol.bedrock.packet.BedrockPacket;
import org.cloudburstmc.protocol.bedrock.packet.DisconnectPacket;
import org.cloudburstmc.protocol.bedrock.packet.LoginPacket;
import org.cloudburstmc.protocol.bedrock.util.EncryptionUtils;
import org.cloudburstmc.protocol.common.util.VarInts;
Expand All @@ -27,17 +26,19 @@ public class MinecraftDataHandler implements SCTPByteStreamListener {
private final BedrockCodec codec;
private final BedrockCodecHelper helper;
private final RedirectPacketHandler redirectPacketHandler;
private final Logger logger;

private boolean compressionEnabled;
private BedrockEncryptionEncoder encryptionEncoder;

private ByteBuf concat;
private int expectedLength;

public MinecraftDataHandler(SCTPStream sctpStream, BedrockCodec codec, SessionInfo sessionInfo) {
public MinecraftDataHandler(SCTPStream sctpStream, BedrockCodec codec, SessionInfo sessionInfo, Logger logger) {
this.sctpStream = sctpStream;
this.codec = codec;
this.helper = codec.createHelper();
this.logger = logger.prefixed("MinecraftDataHandler");

this.redirectPacketHandler = new RedirectPacketHandler(this, sessionInfo);
}
Expand All @@ -54,12 +55,12 @@ public void onMessage(SCTPStream sctpStream, byte[] bytes) {
buf.writeBytes(bytes);

byte remainingSegments = buf.readByte();
System.out.println("first 100 bytes: " + Hex.toHexString(bytes, 0, Math.min(100, bytes.length)));
// System.out.println("first 100 bytes: " + Hex.toHexString(bytes, 0, Math.min(100, bytes.length)));

if (concat == null) {
if (compressionEnabled) {
if (0xff != buf.readUnsignedByte()) {
throw new IllegalStateException("Expected none compression!");
throw new IllegalStateException("Expected no compression!");
}
}
expectedLength = VarInts.readUnsignedInt(buf);
Expand Down Expand Up @@ -92,27 +93,28 @@ public void onMessage(SCTPStream sctpStream, byte[] bytes) {
var packet = readPacket(buf);

if (!(packet instanceof LoginPacket)) {
System.out.println("C -> S: " + packet);
logger.debug("C -> S: " + packet);
} else {
// Don't log the contents of the login packet
logger.debug("C -> S: LoginPacket");
}

packet.handle(redirectPacketHandler);
} catch (Exception e) {
e.printStackTrace();
logger.error("Failed to handle packet from NetherNet", e);
}
}

@Override
public void onMessage(SCTPStream sctpStream, String s) {
System.out.println("string message (" + sctpStream.getLabel() + "): " + s);
}

@Override
public void close(SCTPStream sctpStream) {
System.out.println("stream closed: " + sctpStream.getLabel());
}

public void sendPacket(BedrockPacket packet) {
System.out.println("S -> C: " + packet);
logger.debug("S -> C: " + packet);
try {
ByteBuf dataBuf = Unpooled.buffer(128);
var shiftedBytes = (compressionEnabled ? 1 : 0) + 5; // leave enough room for compression byte & data length
Expand All @@ -128,7 +130,7 @@ public void sendPacket(BedrockPacket packet) {

var lastPacketByte = dataBuf.writerIndex();
dataBuf.readerIndex(shiftedBytes);
System.out.println("packet: " + Hex.toHexString(encode(dataBuf)));
// System.out.println("packet: " + Hex.toHexString(encode(dataBuf)));

var packetLength = lastPacketByte - shiftedBytes;
// read from the first actual byte
Expand All @@ -142,14 +144,14 @@ public void sendPacket(BedrockPacket packet) {
dataBuf.writerIndex(lastPacketByte);

var ri = dataBuf.readerIndex();
System.out.println("encoding: " + Hex.toHexString(encode(dataBuf)));
// System.out.println("encoding: " + Hex.toHexString(encode(dataBuf)));
dataBuf.readerIndex(ri);

if (encryptionEncoder != null) {
dataBuf = encryptionEncoder.encode(dataBuf);

ri = dataBuf.readerIndex();
System.out.println("encrypted: " + Hex.toHexString(encode(dataBuf)));
// System.out.println("encrypted: " + Hex.toHexString(encode(dataBuf)));
dataBuf.readerIndex(ri);
}

Expand All @@ -161,11 +163,11 @@ public void sendPacket(BedrockPacket packet) {
sendBuf.writeBytes(dataBuf, segmentLength);

var data = encode(sendBuf);
System.out.println("final: " + Hex.toHexString(data));
// System.out.println("final: " + Hex.toHexString(data));
sctpStream.send(data);
}
} catch (Exception e) {
e.printStackTrace();
logger.error("Failed to send packet to NetherNet", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ public void receiveOffer(BigInteger from, String sessionId, String message) {
// Log.setLevel(Log.DEBUG);

// Log the remote public IP
component.getRemoteCandidates().forEach(remoteCandidate -> {
if (remoteCandidate.getType() == CandidateType.SERVER_REFLEXIVE_CANDIDATE) {
System.out.println("Remote public IP: " + remoteCandidate.getTransportAddress().getHostAddress());
}
});
// component.getRemoteCandidates().forEach(remoteCandidate -> {
// if (remoteCandidate.getType() == CandidateType.SERVER_REFLEXIVE_CANDIDATE) {
// System.out.println("Remote public IP: " + remoteCandidate.getTransportAddress().getHostAddress());
// }
// });

// TODO Pass some form of close handler to the association so we can clean up properly in the RtcWebsocketClient
new ThreadedAssociation(dtlsTransport, new SctpAssociationListener(rtcWebsocket.sessionInfo(), () -> {
new ThreadedAssociation(dtlsTransport, new SctpAssociationListener(rtcWebsocket.sessionInfo(), rtcWebsocket.logger(), () -> {
try {
dtlsTransport.close();
agent.free();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private void handleCandidateAdd(String sessionId, String message) {
}

public void handleDisconnect(String sessionId) {
System.out.println("Disconnecting session: " + sessionId);
logger.debug("Disconnecting session: " + sessionId);
activeSessions.remove(sessionId);
}

Expand Down Expand Up @@ -175,4 +175,8 @@ public void onError(Exception ex) {
public SessionInfo sessionInfo() {
return sessionInfo;
}

public Logger logger() {
return logger;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rtm516.mcxboxbroadcast.core.webrtc;

import com.rtm516.mcxboxbroadcast.core.Logger;
import com.rtm516.mcxboxbroadcast.core.SessionInfo;
import org.cloudburstmc.protocol.bedrock.codec.v712.Bedrock_v712;
import pe.pi.sctp4j.sctp.Association;
Expand All @@ -9,20 +10,22 @@
public class SctpAssociationListener implements AssociationListener {
private final SessionInfo sessionInfo;
private final Runnable onDisconnect;
private final Logger logger;

public SctpAssociationListener(SessionInfo sessionInfo, Runnable onDisconnect) {
public SctpAssociationListener(SessionInfo sessionInfo, Logger logger, Runnable onDisconnect) {
this.sessionInfo = sessionInfo;
this.logger = logger.prefixed("SctpAssociationListener");
this.onDisconnect = onDisconnect;
}

@Override
public void onAssociated(Association association) {
System.out.println("Association associated: " + association.toString());
// System.out.println("Association associated: " + association.toString());
}

@Override
public void onDisAssociated(Association association) {
System.out.println("Association disassociated: " + association.toString());
// System.out.println("Association disassociated: " + association.toString());
onDisconnect.run();
}

Expand All @@ -31,15 +34,15 @@ public void onDCEPStream(SCTPStream sctpStream, String label, int i) throws Exce
if (label == null) {
return;
}
System.out.println("Received DCEP SCTP stream: " + sctpStream.toString());
logger.debug("Received DCEP SCTP stream: " + sctpStream.toString());

if ("ReliableDataChannel".equals(label)) {
sctpStream.setSCTPStreamListener(new MinecraftDataHandler(sctpStream, Bedrock_v712.CODEC, sessionInfo));
sctpStream.setSCTPStreamListener(new MinecraftDataHandler(sctpStream, Bedrock_v712.CODEC, sessionInfo, logger));
}
}

@Override
public void onRawStream(SCTPStream sctpStream) {
System.out.println("Received raw SCTP stream: " + sctpStream.toString());
// System.out.println("Received raw SCTP stream: " + sctpStream.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public void sendStartGame() {

startGamePacket.setServerEngine("");
startGamePacket.setLevelId("");
startGamePacket.setLevelName("GlobalLinkServer");
startGamePacket.setLevelName("MCXboxBroadcast");
startGamePacket.setPremiumWorldTemplateId("");
startGamePacket.setWorldTemplateId(new UUID(0, 0));
startGamePacket.setCurrentTick(0);
Expand Down
11 changes: 7 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ junit = "1.10.2"
spring-dependency = "1.1.5" # Spring Dependency Management Plugin
guava = "33.2.1-jre"
bouncycastle = "1.78.1"
slf4j-bridge = "2.0.16"

shadow = "8.1.1"
shadow = "8.3.0"
indra = "3.1.3"
blossom = "2.1.0"

Expand All @@ -38,7 +39,7 @@ java-websocket = { group = "org.java-websocket", name = "Java-WebSocket", versio
nimbus-jose-jwt = { group = "com.nimbusds", name = "nimbus-jose-jwt", version.ref = "nimbus-jose-jwt" }

# Plugins
shadow = { group = "com.github.johnrengelman", name = "shadow", version.ref = "shadow" }
shadow = { group = "com.gradleup.shadow", name = "shadow-gradle-plugin", version.ref = "shadow" }
indra-git = { group = "net.kyori", name = "indra-git", version.ref = "indra" }
blossom = { group = "net.kyori", name = "blossom", version.ref = "blossom" }

Expand All @@ -47,6 +48,7 @@ terminalconsoleappender = { group = "net.minecrell", name = "terminalconsoleappe
log4j-api = { group = "org.apache.logging.log4j", name = "log4j-api", version.ref = "log4j" }
log4j-core = { group = "org.apache.logging.log4j", name = "log4j-core", version.ref = "log4j" }
log4j-slf4j2-impl = { group = "org.apache.logging.log4j", name = "log4j-slf4j2-impl", version.ref = "log4j" }
slf4j-bridge = { group = "org.slf4j", name = "jul-to-slf4j", version.ref = "slf4j-bridge" }

jline-terminal = { group = "org.jline", name = "jline-terminal", version.ref = "jline" }
jline-terminal-jna = { group = "org.jline", name = "jline-terminal-jna", version.ref = "jline" }
Expand Down Expand Up @@ -85,7 +87,8 @@ geyser = [
log4j = [
"log4j-api",
"log4j-core",
"log4j-slf4j2-impl"
"log4j-slf4j2-impl",
"slf4j-bridge"
]
jline = [
"jline-terminal",
Expand Down Expand Up @@ -115,7 +118,7 @@ bouncycastle = [
]

[plugins]
shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" }
shadow = { id = "com.gradleup.shadow", version.ref = "shadow" }
indra-git = { id = "net.kyori.indra.git", version.ref = "indra" }
blossom = { id = "net.kyori.blossom", version.ref = "blossom" }
spring-boot = { id = "org.springframework.boot", version.ref = "spring" }
Expand Down

0 comments on commit f237102

Please sign in to comment.