Skip to content

Commit

Permalink
Merge pull request #748 from momothereal/glowstone-fix
Browse files Browse the repository at this point in the history
Fix encryption in pipeline for Glowstone
  • Loading branch information
Shevchik committed Nov 26, 2017
2 parents 021a282 + e50c223 commit ff72187
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,46 +1,33 @@
package protocolsupport.protocol.packet.handler;

import java.security.PrivateKey;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;

import org.apache.commons.lang3.Validate;
import org.bukkit.Bukkit;

import com.google.common.base.Charsets;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelPipeline;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import org.apache.commons.lang3.Validate;
import org.bukkit.Bukkit;
import protocolsupport.ProtocolSupport;
import protocolsupport.api.Connection;
import protocolsupport.api.ProtocolType;
import protocolsupport.api.ProtocolVersion;
import protocolsupport.api.events.PlayerLoginStartEvent;
import protocolsupport.api.events.PlayerPropertiesResolveEvent.ProfileProperty;
import protocolsupport.protocol.ConnectionImpl;
import protocolsupport.protocol.pipeline.ChannelHandlers;
import protocolsupport.protocol.pipeline.common.PacketDecrypter;
import protocolsupport.protocol.pipeline.common.PacketEncrypter;
import protocolsupport.protocol.utils.MinecraftEncryption;
import protocolsupport.protocol.utils.authlib.GameProfile;
import protocolsupport.utils.Utils;
import protocolsupport.zplatform.ServerPlatform;
import protocolsupport.zplatform.impl.spigot.network.SpigotChannelHandlers;
import protocolsupport.zplatform.network.NetworkManagerWrapper;

import javax.crypto.SecretKey;
import java.security.PrivateKey;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.UUID;
import java.util.concurrent.*;
import java.util.logging.Level;

public abstract class AbstractLoginListener implements IHasProfile {

private static final int loginThreadKeepAlive = Utils.getJavaPropertyValue("loginthreadskeepalive", 60, Integer::parseInt);
Expand Down Expand Up @@ -194,10 +181,7 @@ public void run() {

protected void enableEncryption(SecretKey key) {
ChannelPipeline pipeline = networkManager.getChannel().pipeline();
pipeline.addBefore(SpigotChannelHandlers.SPLITTER, ChannelHandlers.DECRYPT, new PacketDecrypter(MinecraftEncryption.getCipher(Cipher.DECRYPT_MODE, key)));
if (isFullEncryption(connection.getVersion())) {
pipeline.addBefore(SpigotChannelHandlers.PREPENDER, ChannelHandlers.ENCRYPT, new PacketEncrypter(MinecraftEncryption.getCipher(Cipher.ENCRYPT_MODE, key)));
}
ServerPlatform.get().getMiscUtils().enableEncryption(pipeline, key, isFullEncryption(connection.getVersion()));
}

@SuppressWarnings("unchecked")
Expand Down
4 changes: 4 additions & 0 deletions src/protocolsupport/zplatform/PlatformUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import protocolsupport.protocol.pipeline.IPacketSplitter;
import protocolsupport.zplatform.itemstack.NBTTagCompoundWrapper;

import javax.crypto.SecretKey;

public interface PlatformUtils {

public ItemStack createItemStackFromNBTTag(NBTTagCompoundWrapper tag);
Expand Down Expand Up @@ -53,6 +55,8 @@ public interface PlatformUtils {

public void enableCompression(ChannelPipeline pipeline, int compressionThreshold);

public void enableEncryption(ChannelPipeline pipeline, SecretKey key, boolean fullEncryption);

public void setFraming(ChannelPipeline pipeline, IPacketSplitter splitter, IPacketPrepender prepender);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
import net.glowstone.net.protocol.ProtocolType;
import net.glowstone.util.GlowServerIcon;
import protocolsupport.api.utils.NetworkState;
import protocolsupport.protocol.pipeline.ChannelHandlers;
import protocolsupport.protocol.pipeline.IPacketPrepender;
import protocolsupport.protocol.pipeline.IPacketSplitter;
import protocolsupport.protocol.pipeline.common.PacketDecrypter;
import protocolsupport.protocol.pipeline.common.PacketEncrypter;
import protocolsupport.protocol.utils.MinecraftEncryption;
import protocolsupport.protocol.utils.authlib.GameProfile;
import protocolsupport.utils.ReflectionUtils;
import protocolsupport.zplatform.PlatformUtils;
Expand All @@ -33,6 +37,9 @@
import protocolsupport.zplatform.impl.glowstone.network.pipeline.GlowStoneFramingHandler;
import protocolsupport.zplatform.itemstack.NBTTagCompoundWrapper;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;

public class GlowStoneMiscUtils implements PlatformUtils {

public static GlowServer getServer() {
Expand Down Expand Up @@ -200,6 +207,14 @@ public void enableCompression(ChannelPipeline pipeline, int compressionThreshold
pipeline.addAfter(GlowStoneChannelHandlers.FRAMING, "compression", new CompressionHandler(compressionThreshold));
}

@Override
public void enableEncryption(ChannelPipeline pipeline, SecretKey key, boolean fullEncryption) {
pipeline.addBefore(GlowStoneChannelHandlers.FRAMING, ChannelHandlers.DECRYPT, new PacketDecrypter(MinecraftEncryption.getCipher(Cipher.DECRYPT_MODE, key)));
if (fullEncryption) {
pipeline.addBefore(GlowStoneChannelHandlers.FRAMING, ChannelHandlers.ENCRYPT, new PacketEncrypter(MinecraftEncryption.getCipher(Cipher.ENCRYPT_MODE, key)));
}
}

@Override
public void setFraming(ChannelPipeline pipeline, IPacketSplitter splitter, IPacketPrepender prepender) {
((GlowStoneFramingHandler) pipeline.get(GlowStoneChannelHandlers.FRAMING)).setRealFraming(prepender, splitter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ protected void initChannel(Channel channel) throws Exception {
connection.storeInChannel(channel);
ProtocolStorage.addConnection(channel.remoteAddress(), connection);
pipeline.remove(GlowStoneChannelHandlers.READ_TIMEOUT);
pipeline.remove("legacy_ping");
pipeline.remove("encryption");
pipeline.remove("compression");
pipeline.remove(GlowStoneChannelHandlers.LEGACY_PING);
pipeline.remove(GlowStoneChannelHandlers.ENCRYPTION);
pipeline.remove(GlowStoneChannelHandlers.COMPRESSION);
pipeline.addFirst(GlowStoneChannelHandlers.READ_TIMEOUT, new SimpleReadTimeoutHandler(30));
pipeline.addAfter(GlowStoneChannelHandlers.READ_TIMEOUT, ChannelHandlers.INITIAL_DECODER, new InitialPacketDecoder());
pipeline.addBefore(GlowStoneChannelHandlers.NETWORK_MANAGER, "ps_glowstone_sync_ticker", GlowStoneSyncTickerStarter.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ public class GlowStoneChannelHandlers {

public static final String READ_TIMEOUT = "idle_timeout";
public static final String FRAMING = "framing";
public static final String DECODER_ENCODER = "codecs";
public static final String NETWORK_MANAGER = "handler";
public static final String DECRYPT = "decrypt";
public static final String LEGACY_PING = "legacy_ping";
public static final String COMPRESSION = "compression";
public static final String ENCRYPTION = "encryption";

}
15 changes: 15 additions & 0 deletions src/protocolsupport/zplatform/impl/spigot/SpigotMiscUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@
import net.minecraft.server.v1_12_R1.WorldServer;
import protocolsupport.api.events.PlayerPropertiesResolveEvent.ProfileProperty;
import protocolsupport.api.utils.NetworkState;
import protocolsupport.protocol.pipeline.ChannelHandlers;
import protocolsupport.protocol.pipeline.IPacketPrepender;
import protocolsupport.protocol.pipeline.IPacketSplitter;
import protocolsupport.protocol.pipeline.common.PacketDecrypter;
import protocolsupport.protocol.pipeline.common.PacketEncrypter;
import protocolsupport.protocol.utils.MinecraftEncryption;
import protocolsupport.protocol.utils.authlib.GameProfile;
import protocolsupport.zplatform.PlatformUtils;
import protocolsupport.zplatform.impl.spigot.itemstack.SpigotNBTTagCompoundWrapper;
Expand All @@ -42,6 +46,9 @@
import protocolsupport.zplatform.impl.spigot.network.pipeline.SpigotWrappedSplitter;
import protocolsupport.zplatform.itemstack.NBTTagCompoundWrapper;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;

public class SpigotMiscUtils implements PlatformUtils {

public static NetworkState protocolToNetState(EnumProtocol state) {
Expand Down Expand Up @@ -205,6 +212,14 @@ public void enableCompression(ChannelPipeline pipeline, int compressionThreshold
.addAfter(SpigotChannelHandlers.PREPENDER, "compress", new SpigotPacketCompressor(compressionThreshold));
}

@Override
public void enableEncryption(ChannelPipeline pipeline, SecretKey key, boolean fullEncryption) {
pipeline.addBefore(SpigotChannelHandlers.SPLITTER, ChannelHandlers.DECRYPT, new PacketDecrypter(MinecraftEncryption.getCipher(Cipher.DECRYPT_MODE, key)));
if (fullEncryption) {
pipeline.addBefore(SpigotChannelHandlers.PREPENDER, ChannelHandlers.ENCRYPT, new PacketEncrypter(MinecraftEncryption.getCipher(Cipher.ENCRYPT_MODE, key)));
}
}

@Override
public void setFraming(ChannelPipeline pipeline, IPacketSplitter splitter, IPacketPrepender prepender) {
((SpigotWrappedSplitter) pipeline.get(SpigotChannelHandlers.SPLITTER)).setRealSplitter(splitter);
Expand Down

0 comments on commit ff72187

Please sign in to comment.