Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feature/1.21.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Oct 17, 2024
2 parents 9603c57 + fda25b5 commit f4c07f2
Show file tree
Hide file tree
Showing 34 changed files with 800 additions and 551 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void packetSent(Session session, Packet packet) {
public void connected(ConnectedEvent event) {
log.info("CLIENT Connected");

event.getSession().enableEncryption(((TestProtocol) event.getSession().getPacketProtocol()).getEncryption());
event.getSession().setEncryption(((TestProtocol) event.getSession().getPacketProtocol()).getEncryption());
event.getSession().send(new PingPacket("hello"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void serverClosed(ServerClosedEvent event) {
public void sessionAdded(SessionAddedEvent event) {
log.info("SERVER Session Added: {}:{}", event.getSession().getHost(), event.getSession().getPort());
((TestProtocol) event.getSession().getPacketProtocol()).setSecretKey(this.key);
event.getSession().enableEncryption(((TestProtocol) event.getSession().getPacketProtocol()).getEncryption());
event.getSession().setEncryption(((TestProtocol) event.getSession().getPacketProtocol()).getEncryption());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.geysermc.mcprotocollib.network.codec.PacketDefinition;
import org.geysermc.mcprotocollib.network.codec.PacketSerializer;
import org.geysermc.mcprotocollib.network.crypt.AESEncryption;
import org.geysermc.mcprotocollib.network.crypt.PacketEncryption;
import org.geysermc.mcprotocollib.network.crypt.EncryptionConfig;
import org.geysermc.mcprotocollib.network.packet.DefaultPacketHeader;
import org.geysermc.mcprotocollib.network.packet.PacketHeader;
import org.geysermc.mcprotocollib.network.packet.PacketProtocol;
Expand All @@ -23,7 +23,7 @@ public class TestProtocol extends PacketProtocol {
private static final Logger log = LoggerFactory.getLogger(TestProtocol.class);
private final PacketHeader header = new DefaultPacketHeader();
private final PacketRegistry registry = new PacketRegistry();
private AESEncryption encrypt;
private EncryptionConfig encrypt;

@SuppressWarnings("unused")
public TestProtocol() {
Expand Down Expand Up @@ -51,7 +51,7 @@ public PingPacket deserialize(ByteBuf buf, PacketCodecHelper helper, PacketDefin
});

try {
this.encrypt = new AESEncryption(key);
this.encrypt = new EncryptionConfig(new AESEncryption(key));
} catch (GeneralSecurityException e) {
log.error("Failed to create encryption", e);
}
Expand All @@ -67,7 +67,7 @@ public PacketHeader getPacketHeader() {
return this.header;
}

public PacketEncryption getEncryption() {
public EncryptionConfig getEncryption() {
return this.encrypt;
}

Expand All @@ -82,7 +82,12 @@ public void newServerSession(Server server, Session session) {
}

@Override
public PacketRegistry getPacketRegistry() {
public PacketRegistry getInboundPacketRegistry() {
return registry;
}

@Override
public PacketRegistry getOutboundPacketRegistry() {
return registry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
public class MinecraftProtocolTest {
private static final Logger log = LoggerFactory.getLogger(MinecraftProtocolTest.class);
private static final boolean SPAWN_SERVER = true;
private static final boolean VERIFY_USERS = false;
private static final boolean ENCRYPT_CONNECTION = true;
private static final boolean SHOULD_AUTHENTICATE = false;
private static final String HOST = "127.0.0.1";
private static final int PORT = 25565;
private static final ProxyInfo PROXY = null;
Expand All @@ -63,7 +64,8 @@ public static void main(String[] args) {

Server server = new TcpServer(HOST, PORT, MinecraftProtocol::new);
server.setGlobalFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
server.setGlobalFlag(MinecraftConstants.VERIFY_USERS_KEY, VERIFY_USERS);
server.setGlobalFlag(MinecraftConstants.ENCRYPT_CONNECTION, ENCRYPT_CONNECTION);
server.setGlobalFlag(MinecraftConstants.SHOULD_AUTHENTICATE, SHOULD_AUTHENTICATE);
server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, session ->
new ServerStatusInfo(
Component.text("Hello world!"),
Expand Down Expand Up @@ -101,7 +103,7 @@ public static void main(String[] args) {
))
);

server.setGlobalFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD, 100);
server.setGlobalFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD, 256);
server.addListener(new ServerAdapter() {
@Override
public void serverClosed(ServerClosedEvent event) {
Expand Down Expand Up @@ -134,7 +136,7 @@ public void packetReceived(Session session, Packet packet) {
@Override
public void sessionRemoved(SessionRemovedEvent event) {
MinecraftProtocol protocol = (MinecraftProtocol) event.getSession().getPacketProtocol();
if (protocol.getState() == ProtocolState.GAME) {
if (protocol.getOutboundState() == ProtocolState.GAME) {
log.info("Closing server.");
event.getServer().close(false);
}
Expand Down Expand Up @@ -178,7 +180,7 @@ private static void status() {

private static void login() {
MinecraftProtocol protocol;
if (VERIFY_USERS) {
if (SHOULD_AUTHENTICATE) {
StepFullJavaSession.FullJavaSession fullJavaSession;
try {
fullJavaSession = MinecraftAuth.JAVA_CREDENTIALS_LOGIN.getFromInput(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
* Built-in PacketLib session flags.
*/
public class BuiltinFlags {
public static final Flag<Boolean> ENABLE_CLIENT_PROXY_PROTOCOL = new Flag<>("enable-client-proxy-protocol", Boolean.class);

/**
* Enables HAProxy protocol support.
* When this value is not null it represents the ip and port the client claims the connection is from.
*/
public static final Flag<InetSocketAddress> CLIENT_PROXIED_ADDRESS = new Flag<>("client-proxied-address", InetSocketAddress.class);

/**
Expand All @@ -20,6 +23,24 @@ public class BuiltinFlags {
*/
public static final Flag<Boolean> TCP_FAST_OPEN = new Flag<>("tcp-fast-open", Boolean.class);

/**
* Connection timeout in seconds.
* Only used by the client.
*/
public static final Flag<Integer> CLIENT_CONNECT_TIMEOUT = new Flag<>("client-connect-timeout", Integer.class);

/**
* Read timeout in seconds.
* Used by both the server and client.
*/
public static final Flag<Integer> READ_TIMEOUT = new Flag<>("read-timeout", Integer.class);

/**
* Write timeout in seconds.
* Used by both the server and client.
*/
public static final Flag<Integer> WRITE_TIMEOUT = new Flag<>("write-timeout", Integer.class);

private BuiltinFlags() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.geysermc.mcprotocollib.network;

import io.netty.util.AttributeKey;
import org.geysermc.mcprotocollib.network.compression.CompressionConfig;
import org.geysermc.mcprotocollib.network.crypt.EncryptionConfig;

public class NetworkConstants {
public static final AttributeKey<CompressionConfig> COMPRESSION_ATTRIBUTE_KEY = AttributeKey.valueOf("compression");
public static final AttributeKey<EncryptionConfig> ENCRYPTION_ATTRIBUTE_KEY = AttributeKey.valueOf("encryption");
}
128 changes: 69 additions & 59 deletions protocol/src/main/java/org/geysermc/mcprotocollib/network/Session.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package org.geysermc.mcprotocollib.network;

import io.netty.channel.Channel;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.mcprotocollib.network.codec.PacketCodecHelper;
import org.geysermc.mcprotocollib.network.crypt.PacketEncryption;
import org.geysermc.mcprotocollib.network.compression.CompressionConfig;
import org.geysermc.mcprotocollib.network.crypt.EncryptionConfig;
import org.geysermc.mcprotocollib.network.event.session.SessionEvent;
import org.geysermc.mcprotocollib.network.event.session.SessionListener;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.network.packet.PacketProtocol;
import org.geysermc.mcprotocollib.network.tcp.FlushHandler;

import java.net.SocketAddress;
import java.util.List;
Expand Down Expand Up @@ -37,7 +40,7 @@ public interface Session {
* @param wait Whether to wait for the connection to be established before returning.
* @param transferring Whether the session is a client being transferred.
*/
public void connect(boolean wait, boolean transferring);
void connect(boolean wait, boolean transferring);

/**
* Gets the host the session is connected to.
Expand Down Expand Up @@ -138,7 +141,7 @@ public interface Session {
*
* @param flags Collection of flags
*/
public void setFlags(Map<String, Object> flags);
void setFlags(Map<String, Object> flags);

/**
* Gets the listeners listening on this session.
Expand Down Expand Up @@ -183,68 +186,21 @@ public interface Session {
void callPacketSent(Packet packet);

/**
* Gets the compression packet length threshold for this session (-1 = disabled).
* Sets the compression config for this session.
*
* @return This session's compression threshold.
* @param compressionConfig the compression to compress with,
* or null to disable compression
*/
int getCompressionThreshold();
void setCompression(@Nullable CompressionConfig compressionConfig);

/**
* Sets the compression packet length threshold for this session (-1 = disabled).
* Sets encryption for this session.
*
* @param threshold The new compression threshold.
* @param validateDecompression whether to validate that the decompression fits within size checks.
*/
void setCompressionThreshold(int threshold, boolean validateDecompression);

/**
* Enables encryption for this session.
*
* @param encryption the encryption to encrypt with
*/
void enableEncryption(PacketEncryption encryption);

/**
* Gets the connect timeout for this session in seconds.
*
* @return The session's connect timeout.
*/
int getConnectTimeout();

/**
* Sets the connect timeout for this session in seconds.
*
* @param timeout Connect timeout to set.
*/
void setConnectTimeout(int timeout);

/**
* Gets the read timeout for this session in seconds.
*
* @return The session's read timeout.
*/
int getReadTimeout();

/**
* Sets the read timeout for this session in seconds.
*
* @param timeout Read timeout to set.
*/
void setReadTimeout(int timeout);

/**
* Gets the write timeout for this session in seconds.
*
* @return The session's write timeout.
*/
int getWriteTimeout();

/**
* Sets the write timeout for this session in seconds.
* @param encryptionConfig the encryption to encrypt with,
* or null to disable encryption
*
* @param timeout Write timeout to set.
*/
void setWriteTimeout(int timeout);
void setEncryption(@Nullable EncryptionConfig encryptionConfig);

/**
* Returns true if the session is connected.
Expand All @@ -258,7 +214,17 @@ public interface Session {
*
* @param packet Packet to send.
*/
void send(Packet packet);
default void send(@NonNull Packet packet) {
this.send(packet, null);
}

/**
* Sends a packet and runs the specified callback when the packet has been sent.
*
* @param packet Packet to send.
* @param onSent Callback to run when the packet has been sent.
*/
void send(@NonNull Packet packet, @Nullable Runnable onSent);

/**
* Disconnects the session.
Expand Down Expand Up @@ -301,4 +267,48 @@ default void disconnect(@NonNull Component reason) {
* @param cause Throwable responsible for disconnecting.
*/
void disconnect(@NonNull Component reason, @Nullable Throwable cause);

/**
* Auto read in netty means that the server is automatically reading from the channel.
* Turning it off means that we won't get more packets being decoded until we turn it back on.
* We use this to hold off on reading packets until we are ready to process them.
* For example this is used for switching inbound states with {@link #switchInboundState(Runnable)}.
*
* @param autoRead Whether to enable auto read.
* Default is true.
*/
void setAutoRead(boolean autoRead);

/**
* Returns the underlying netty channel of this session.
*
* @return The netty channel
*/
Channel getChannel();

/**
* Changes the inbound state of the session and then re-enables auto read.
* This is used after a terminal packet was handled and the session is ready to receive more packets in the new state.
*
* @param switcher The runnable that switches the inbound state.
*/
default void switchInboundState(Runnable switcher) {
switcher.run();

// We switched to the new inbound state
// we can start reading again
setAutoRead(true);
}

/**
* Flushes all packets that are due to be sent and changes the outbound state of the session.
* This makes sure no other threads have scheduled packets to be sent.
*
* @param switcher The runnable that switches the outbound state.
*/
default void switchOutboundState(Runnable switcher) {
getChannel().writeAndFlush(FlushHandler.FLUSH_PACKET).syncUninterruptibly();

switcher.run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.geysermc.mcprotocollib.network.compression;

public record CompressionConfig(int threshold, PacketCompression compression, boolean validateDecompression) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.geysermc.mcprotocollib.network.crypt;

public record EncryptionConfig(PacketEncryption encryption) {
}
Loading

0 comments on commit f4c07f2

Please sign in to comment.