Explicitly rewrite legacy plugin message channels for 1.13+ clients.

This commit is contained in:
Andrew Steinborn
2019-05-15 17:04:25 -04:00
parent d805f79d9b
commit 6d42a3c37c
6 changed files with 164 additions and 16 deletions

View File

@@ -671,8 +671,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
// Otherwise, we need to see if the player already knows this channel or it's known by the
// proxy.
return minecraftOrFmlMessage || knownChannels.contains(message.getChannel())
|| server.getChannelRegistrar().registered(message.getChannel());
return minecraftOrFmlMessage || knownChannels.contains(message.getChannel());
}
private class ConnectionRequestBuilderImpl implements ConnectionRequestBuilder {

View File

@@ -1,11 +1,13 @@
package com.velocitypowered.proxy.protocol.packet;
import static com.velocitypowered.proxy.connection.VelocityConstants.EMPTY_BYTE_ARRAY;
import static com.velocitypowered.proxy.protocol.util.PluginMessageUtil.transformLegacyToModernChannel;
import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -38,13 +40,16 @@ public class PluginMessage implements MinecraftPacket {
public String toString() {
return "PluginMessage{"
+ "channel='" + channel + '\''
+ ", data=<removed>" +
+ ", data=<removed>"
+ '}';
}
@Override
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
this.channel = ProtocolUtils.readString(buf);
if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) {
this.channel = transformLegacyToModernChannel(this.channel);
}
this.data = new byte[buf.readableBytes()];
buf.readBytes(data);
}
@@ -54,7 +59,8 @@ public class PluginMessage implements MinecraftPacket {
if (channel == null) {
throw new IllegalStateException("Channel is not specified.");
}
ProtocolUtils.writeString(buf, channel);
ProtocolUtils.writeString(buf, version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0
? channel : transformLegacyToModernChannel(this.channel));
buf.writeBytes(data);
}

View File

@@ -14,6 +14,8 @@ import io.netty.buffer.Unpooled;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
public class PluginMessageUtil {
@@ -143,4 +145,40 @@ public class PluginMessageUtil {
return newMsg;
}
private static final Pattern INVALID_IDENTIFIER_REGEX = Pattern.compile("[^a-z0-9\\-_]*");
/**
* Transform a plugin message channel from a "legacy" (<1.13) form to a modern one.
* @param name the existing name
* @return the new name
*/
public static String transformLegacyToModernChannel(String name) {
checkNotNull(name, "name");
if (name.indexOf(':') != -1) {
// Probably valid. We won't check this for now and go on faith.
return name;
}
// Before falling into the fallback, explicitly rewrite certain messages.
switch (name) {
case REGISTER_CHANNEL_LEGACY:
return REGISTER_CHANNEL;
case UNREGISTER_CHANNEL_LEGACY:
return UNREGISTER_CHANNEL;
case BRAND_CHANNEL_LEGACY:
return BRAND_CHANNEL;
case "BungeeCord":
// This is a special historical case we are compelled to support for the benefit of
// BungeeQuack.
return "bungeecord:main";
default:
// This is very likely a legacy name, so transform it. Velocity uses the same scheme as
// BungeeCord does to transform channels, but also removes clearly invalid characters as
// well.
String lower = name.toLowerCase(Locale.ROOT);
return "legacy:" + INVALID_IDENTIFIER_REGEX.matcher(lower).replaceAll("");
}
}
}

View File

@@ -1,15 +1,15 @@
package com.velocitypowered.proxy.util;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.api.proxy.messages.ChannelRegistrar;
import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import java.util.ArrayList;
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -28,7 +28,13 @@ public class VelocityChannelRegistrar implements ChannelRegistrar {
}
for (ChannelIdentifier identifier : identifiers) {
identifierMap.put(identifier.getId(), identifier);
if (identifier instanceof MinecraftChannelIdentifier) {
identifierMap.put(identifier.getId(), identifier);
} else {
String rewritten = PluginMessageUtil.transformLegacyToModernChannel(identifier.getId());
identifierMap.put(identifier.getId(), identifier);
identifierMap.put(rewritten, identifier);
}
}
}
@@ -41,12 +47,22 @@ public class VelocityChannelRegistrar implements ChannelRegistrar {
}
for (ChannelIdentifier identifier : identifiers) {
identifierMap.remove(identifier.getId());
if (identifier instanceof MinecraftChannelIdentifier) {
identifierMap.remove(identifier.getId());
} else {
String rewritten = PluginMessageUtil.transformLegacyToModernChannel(identifier.getId());
identifierMap.remove(identifier.getId());
identifierMap.remove(rewritten);
}
}
}
public Collection<String> getIdsForLegacyConnections() {
return ImmutableList.copyOf(identifierMap.keySet());
public Collection<String> getLegacyChannelIds() {
Collection<String> ids = new HashSet<>();
for (ChannelIdentifier value : identifierMap.values()) {
ids.add(value.getId());
}
return ids;
}
/**
@@ -55,19 +71,17 @@ public class VelocityChannelRegistrar implements ChannelRegistrar {
* @return the channel IDs for Minecraft 1.13 and above
*/
public Collection<String> getModernChannelIds() {
Collection<String> ids = new ArrayList<>();
Collection<String> ids = new HashSet<>();
for (ChannelIdentifier value : identifierMap.values()) {
if (value instanceof MinecraftChannelIdentifier) {
ids.add(value.getId());
} else {
ids.add(PluginMessageUtil.transformLegacyToModernChannel(value.getId()));
}
}
return ids;
}
public boolean registered(String id) {
return identifierMap.containsKey(id);
}
public @Nullable ChannelIdentifier getFromId(String id) {
return identifierMap.get(id);
}
@@ -81,6 +95,6 @@ public class VelocityChannelRegistrar implements ChannelRegistrar {
if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) {
return getModernChannelIds();
}
return getIdsForLegacyConnections();
return getLegacyChannelIds();
}
}