3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

Fixed even more Checkstyle issues, this time in the proxy components.

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-10-28 01:02:54 -04:00
Ursprung 6467335f74
Commit 8a9d1b1ac9
34 geänderte Dateien mit 221 neuen und 142 gelöschten Zeilen

Datei anzeigen

@ -94,7 +94,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
}
if (PluginMessageUtil.isMCBrand(packet)) {
serverConn.getPlayer().getConnection().write(PluginMessageUtil.rewriteMCBrand(packet));
serverConn.getPlayer().getConnection().write(PluginMessageUtil.rewriteMinecraftBrand(packet));
return true;
}

Datei anzeigen

@ -103,11 +103,11 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation,
private String createBungeeForwardingAddress() {
// BungeeCord IP forwarding is simply a special injection after the "address" in the handshake,
// separated by \0 (the null byte). In order, you send the original host, the player's IP, their
// UUID (undashed), and if you are in online-mode, their login properties (retrieved from Mojang).
return registeredServer.getServerInfo().getAddress().getHostString() + "\0" +
proxyPlayer.getRemoteAddress().getHostString() + "\0" +
proxyPlayer.getProfile().getId() + "\0" +
GSON.toJson(proxyPlayer.getProfile().getProperties());
// UUID (undashed), and if you are in online-mode, their login properties (from Mojang).
return registeredServer.getServerInfo().getAddress().getHostString() + "\0"
+ proxyPlayer.getRemoteAddress().getHostString() + "\0"
+ proxyPlayer.getProfile().getId() + "\0"
+ GSON.toJson(proxyPlayer.getProfile().getProperties());
}
private void startHandshake() {

Datei anzeigen

@ -182,7 +182,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
clientPluginMsgChannels.removeAll(channels);
backendConn.write(packet);
} else if (PluginMessageUtil.isMCBrand(packet)) {
backendConn.write(PluginMessageUtil.rewriteMCBrand(packet));
backendConn.write(PluginMessageUtil.rewriteMinecraftBrand(packet));
} else if (backendConn.isLegacyForge() && !serverConn.hasCompletedJoin()) {
if (packet.getChannel().equals(ForgeConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL)) {
if (!player.getModInfo().isPresent()) {
@ -195,9 +195,10 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
// Always forward the FML handshake to the remote server.
backendConn.write(packet);
} else {
// The client is trying to send messages too early. This is primarily caused by mods, but it's further
// aggravated by Velocity. To work around these issues, we will queue any non-FML handshake messages to
// be sent once the JoinGame packet has been received by the proxy.
// The client is trying to send messages too early. This is primarily caused by mods, but
// it's further aggravated by Velocity. To work around these issues, we will queue any
// non-FML handshake messages to be sent once the JoinGame packet has been received by the
// proxy.
loginPluginMessages.add(packet);
}
} else {
@ -287,12 +288,12 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
spawned = true;
player.getConnection().delayedWrite(joinGame);
// We have something special to do for legacy Forge servers - during first connection the FML handshake
// will transition to complete regardless. Thus, we need to ensure that a reset packet is ALWAYS sent on
// first switch.
// We have something special to do for legacy Forge servers - during first connection the FML
// handshake will transition to complete regardless. Thus, we need to ensure that a reset
// packet is ALWAYS sent on first switch.
//
// As we know that calling this branch only happens on first join, we set that if we are a Forge
// client that we must reset on the next switch.
// As we know that calling this branch only happens on first join, we set that if we are a
// Forge client that we must reset on the next switch.
//
// The call will handle if the player is not a Forge player appropriately.
player.getConnection().setCanSendLegacyFMLResetPacket(true);
@ -306,11 +307,12 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
// - A respawn packet with a different dimension
// - Another respawn with the correct dimension
//
// The two respawns with different dimensions are required, otherwise the client gets confused.
// The two respawns with different dimensions are required, otherwise the client gets
// confused.
//
// Most notably, by having the client accept the join game packet, we can work around the need to perform
// entity ID rewrites, eliminating potential issues from rewriting packets and improving compatibility with
// mods.
// Most notably, by having the client accept the join game packet, we can work around the need
// to perform entity ID rewrites, eliminating potential issues from rewriting packets and
// improving compatibility with mods.
player.getConnection().delayedWrite(joinGame);
int tempDim = joinGame.getDimension() == 0 ? -1 : 0;
player.getConnection().delayedWrite(
@ -360,12 +362,12 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
// We only need to indicate we can send a reset packet if we complete a handshake, that is,
// logged onto a Forge server.
//
// The special case is if we log onto a Vanilla server as our first server, FML will treat this
// as complete and **will** need a reset packet sending at some point. We will handle this
// during initial player connection if the player is detected to be forge.
// The special case is if we log onto a Vanilla server as our first server, FML will treat
// this as complete and **will** need a reset packet sending at some point. We will handle
// this during initial player connection if the player is detected to be forge.
//
// This is why we use an if statement rather than the result of VelocityServerConnection#isLegacyForge()
// because we don't want to set it false if this is a first connection to a Vanilla server.
// We do not use the result of VelocityServerConnection#isLegacyForge() directly because we
// don't want to set it false if this is a first connection to a Vanilla server.
//
// See LoginSessionHandler#handle for where the counterpart to this method is
player.getConnection().setCanSendLegacyFMLResetPacket(true);

Datei anzeigen

@ -22,6 +22,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
@ -174,8 +175,8 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
}
} catch (Exception e) {
logger.warn("Error while trying to handle a query packet from {}", msg.sender(), e);
// NB: Only need to explicitly release upon exception, writing the response out will decrement the reference
// count.
// NB: Only need to explicitly release upon exception, writing the response out will decrement
// the reference count.
responsePacket.release();
}
}
@ -265,9 +266,8 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
while (iterator.hasNext()) {
QueryResponse.PluginInformation info = iterator.next();
pluginsString.append(info.getName());
if (info.getVersion() != null) {
pluginsString.append(' ').append(info.getVersion());
}
Optional<String> version = info.getVersion();
version.ifPresent(s -> pluginsString.append(' ').append(s));
if (iterator.hasNext()) {
pluginsString.append(';').append(' ');
}

Datei anzeigen

@ -85,15 +85,15 @@ public class BossBar implements MinecraftPacket {
@Override
public String toString() {
return "BossBar{" +
"uuid=" + uuid +
", action=" + action +
", name='" + name + '\'' +
", percent=" + percent +
", color=" + color +
", overlay=" + overlay +
", flags=" + flags +
'}';
return "BossBar{"
+ "uuid=" + uuid
+ ", action=" + action
+ ", name='" + name + '\''
+ ", percent=" + percent
+ ", color=" + color
+ ", overlay=" + overlay
+ ", flags=" + flags
+ '}';
}
@Override

Datei anzeigen

@ -47,10 +47,10 @@ public class Chat implements MinecraftPacket {
@Override
public String toString() {
return "Chat{" +
"message='" + message + '\'' +
", type=" + type +
'}';
return "Chat{"
+ "message='" + message + '\''
+ ", type=" + type
+ '}';
}
@Override

Datei anzeigen

@ -34,9 +34,9 @@ public class Disconnect implements MinecraftPacket {
@Override
public String toString() {
return "Disconnect{" +
"reason='" + reason + '\'' +
'}';
return "Disconnect{"
+ "reason='" + reason + '\''
+ '}';
}
@Override

Datei anzeigen

@ -33,10 +33,10 @@ public class EncryptionRequest implements MinecraftPacket {
@Override
public String toString() {
return "EncryptionRequest{" +
"publicKey=" + Arrays.toString(publicKey) +
", verifyToken=" + Arrays.toString(verifyToken) +
'}';
return "EncryptionRequest{"
+ "publicKey=" + Arrays.toString(publicKey)
+ ", verifyToken=" + Arrays.toString(verifyToken)
+ '}';
}
@Override

Datei anzeigen

@ -32,10 +32,10 @@ public class EncryptionResponse implements MinecraftPacket {
@Override
public String toString() {
return "EncryptionResponse{" +
"sharedSecret=" + Arrays.toString(sharedSecret) +
", verifyToken=" + Arrays.toString(verifyToken) +
'}';
return "EncryptionResponse{"
+ "sharedSecret=" + Arrays.toString(sharedSecret)
+ ", verifyToken=" + Arrays.toString(verifyToken)
+ '}';
}
@Override

Datei anzeigen

@ -47,12 +47,12 @@ public class Handshake implements MinecraftPacket {
@Override
public String toString() {
return "Handshake{" +
"protocolVersion=" + protocolVersion +
", serverAddress='" + serverAddress + '\'' +
", port=" + port +
", nextStatus=" + nextStatus +
'}';
return "Handshake{"
+ "protocolVersion=" + protocolVersion
+ ", serverAddress='" + serverAddress + '\''
+ ", port=" + port
+ ", nextStatus=" + nextStatus
+ '}';
}
@Override

Datei anzeigen

@ -78,15 +78,15 @@ public class JoinGame implements MinecraftPacket {
@Override
public String toString() {
return "JoinGame{" +
"entityId=" + entityId +
", gamemode=" + gamemode +
", dimension=" + dimension +
", difficulty=" + difficulty +
", maxPlayers=" + maxPlayers +
", levelType='" + levelType + '\'' +
", reducedDebugInfo=" + reducedDebugInfo +
'}';
return "JoinGame{"
+ "entityId=" + entityId
+ ", gamemode=" + gamemode
+ ", dimension=" + dimension
+ ", difficulty=" + difficulty
+ ", maxPlayers=" + maxPlayers
+ ", levelType='" + levelType + '\''
+ ", reducedDebugInfo=" + reducedDebugInfo
+ '}';
}
@Override

Datei anzeigen

@ -22,9 +22,9 @@ public class KeepAlive implements MinecraftPacket {
@Override
public String toString() {
return "KeepAlive{" +
"randomId=" + randomId +
'}';
return "KeepAlive{"
+ "randomId=" + randomId
+ '}';
}
@Override

Datei anzeigen

@ -7,10 +7,15 @@ public class LegacyDisconnect {
private final String reason;
public LegacyDisconnect(String reason) {
private LegacyDisconnect(String reason) {
this.reason = reason;
}
/**
* Converts a legacy response into a disconnect packet.
* @param response the response to convert
* @return the disconnect packet
*/
public static LegacyDisconnect fromPingResponse(LegacyPingResponse response) {
String kickMessage = String.join("\0",
"§1",

Datei anzeigen

@ -45,15 +45,20 @@ public class LegacyPingResponse {
@Override
public String toString() {
return "LegacyPingResponse{" +
"protocolVersion=" + protocolVersion +
", serverVersion='" + serverVersion + '\'' +
", motd='" + motd + '\'' +
", playersOnline=" + playersOnline +
", playersMax=" + playersMax +
'}';
return "LegacyPingResponse{"
+ "protocolVersion=" + protocolVersion
+ ", serverVersion='" + serverVersion + '\''
+ ", motd='" + motd + '\''
+ ", playersOnline=" + playersOnline
+ ", playersMax=" + playersMax
+ '}';
}
/**
* Transforms a {@link ServerPing} into a legacy ping response.
* @param ping the response to transform
* @return the legacy ping response
*/
public static LegacyPingResponse from(ServerPing ping) {
return new LegacyPingResponse(ping.getVersion().getProtocol(),
ping.getVersion().getName(),

Datei anzeigen

@ -41,11 +41,11 @@ public class LoginPluginMessage implements MinecraftPacket {
@Override
public String toString() {
return "LoginPluginMessage{" +
"id=" + id +
", channel='" + channel + '\'' +
", data=" + data +
'}';
return "LoginPluginMessage{"
+ "id=" + id
+ ", channel='" + channel + '\''
+ ", data=" + data
+ '}';
}
@Override

Datei anzeigen

@ -39,11 +39,11 @@ public class LoginPluginResponse implements MinecraftPacket {
@Override
public String toString() {
return "LoginPluginResponse{" +
"id=" + id +
", success=" + success +
", data=" + data +
'}';
return "LoginPluginResponse{"
+ "id=" + id
+ ", success=" + success
+ ", data=" + data
+ '}';
}
@Override

Datei anzeigen

@ -36,10 +36,10 @@ public class PluginMessage implements MinecraftPacket {
@Override
public String toString() {
return "PluginMessage{" +
"channel='" + channel + '\'' +
", data=" + ByteBufUtil.hexDump(data) +
'}';
return "PluginMessage{"
+ "channel='" + channel + '\''
+ ", data=" + ByteBufUtil.hexDump(data)
+ '}';
}
@Override

Datei anzeigen

@ -57,12 +57,12 @@ public class Respawn implements MinecraftPacket {
@Override
public String toString() {
return "Respawn{" +
"dimension=" + dimension +
", difficulty=" + difficulty +
", gamemode=" + gamemode +
", levelType='" + levelType + '\'' +
'}';
return "Respawn{"
+ "dimension=" + dimension
+ ", difficulty=" + difficulty
+ ", gamemode=" + gamemode
+ ", levelType='" + levelType + '\''
+ '}';
}
@Override

Datei anzeigen

@ -28,9 +28,9 @@ public class ServerLogin implements MinecraftPacket {
@Override
public String toString() {
return "ServerLogin{" +
"username='" + username + '\'' +
'}';
return "ServerLogin{"
+ "username='" + username + '\''
+ '}';
}
@Override

Datei anzeigen

@ -37,10 +37,10 @@ public class ServerLoginSuccess implements MinecraftPacket {
@Override
public String toString() {
return "ServerLoginSuccess{" +
"uuid=" + uuid +
", username='" + username + '\'' +
'}';
return "ServerLoginSuccess{"
+ "uuid=" + uuid
+ ", username='" + username + '\''
+ '}';
}
@Override

Datei anzeigen

@ -27,9 +27,9 @@ public class SetCompression implements MinecraftPacket {
@Override
public String toString() {
return "SetCompression{" +
"threshold=" + threshold +
'}';
return "SetCompression{"
+ "threshold=" + threshold
+ '}';
}
@Override

Datei anzeigen

@ -27,9 +27,9 @@ public class StatusResponse implements MinecraftPacket {
@Override
public String toString() {
return "StatusResponse{" +
"status='" + status + '\'' +
'}';
return "StatusResponse{"
+ "status='" + status + '\''
+ '}';
}
@Override

Datei anzeigen

@ -53,12 +53,12 @@ public class TabCompleteRequest implements MinecraftPacket {
@Override
public String toString() {
return "TabCompleteRequest{" +
"command='" + command + '\'' +
", assumeCommand=" + assumeCommand +
", hasPosition=" + hasPosition +
", position=" + position +
'}';
return "TabCompleteRequest{"
+ "command='" + command + '\''
+ ", assumeCommand=" + assumeCommand
+ ", hasPosition=" + hasPosition
+ ", position=" + position
+ '}';
}
@Override

Datei anzeigen

@ -18,9 +18,9 @@ public class TabCompleteResponse implements MinecraftPacket {
@Override
public String toString() {
return "TabCompleteResponse{" +
"offers=" + offers +
'}';
return "TabCompleteResponse{"
+ "offers=" + offers
+ '}';
}
@Override

Datei anzeigen

@ -141,13 +141,13 @@ public class TitlePacket implements MinecraftPacket {
@Override
public String toString() {
return "TitlePacket{" +
"action=" + action +
", component='" + component + '\'' +
", fadeIn=" + fadeIn +
", stay=" + stay +
", fadeOut=" + fadeOut +
'}';
return "TitlePacket{"
+ "action=" + action
+ ", component='" + component + '\''
+ ", fadeIn=" + fadeIn
+ ", stay=" + stay
+ ", fadeOut=" + fadeOut
+ '}';
}
@Override

Datei anzeigen

@ -13,12 +13,12 @@ import java.util.List;
public class PluginMessageUtil {
public static final String BRAND_CHANNEL_LEGACY = "MC|Brand";
public static final String BRAND_CHANNEL = "minecraft:brand";
public static final String REGISTER_CHANNEL_LEGACY = "REGISTER";
public static final String REGISTER_CHANNEL = "minecraft:register";
public static final String UNREGISTER_CHANNEL_LEGACY = "UNREGISTER";
public static final String UNREGISTER_CHANNEL = "minecraft:unregister";
private static final String BRAND_CHANNEL_LEGACY = "MC|Brand";
private static final String BRAND_CHANNEL = "minecraft:brand";
private static final String REGISTER_CHANNEL_LEGACY = "REGISTER";
private static final String REGISTER_CHANNEL = "minecraft:register";
private static final String UNREGISTER_CHANNEL_LEGACY = "UNREGISTER";
private static final String UNREGISTER_CHANNEL = "minecraft:unregister";
private PluginMessageUtil() {
throw new AssertionError();
@ -62,7 +62,7 @@ public class PluginMessageUtil {
return message;
}
public static PluginMessage rewriteMCBrand(PluginMessage message) {
public static PluginMessage rewriteMinecraftBrand(PluginMessage message) {
Preconditions.checkNotNull(message, "message");
Preconditions.checkArgument(isMCBrand(message), "message is not a MC Brand plugin message");

Datei anzeigen

@ -1,4 +1,4 @@
package com.velocitypowered.proxy.server.ping;
package com.velocitypowered.proxy.server;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerPing;
@ -20,7 +20,7 @@ public class PingSessionHandler implements MinecraftSessionHandler {
private final MinecraftConnection connection;
private boolean completed = false;
public PingSessionHandler(CompletableFuture<ServerPing> result, RegisteredServer server,
PingSessionHandler(CompletableFuture<ServerPing> result, RegisteredServer server,
MinecraftConnection connection) {
this.result = result;
this.server = server;

Datei anzeigen

@ -21,6 +21,12 @@ public class ServerMap {
this.server = server;
}
/**
* Returns the server associated with the given name.
*
* @param name the name to look up
* @return the server, if it exists
*/
public Optional<RegisteredServer> getServer(String name) {
Preconditions.checkNotNull(name, "server");
String lowerName = name.toLowerCase(Locale.US);
@ -31,6 +37,12 @@ public class ServerMap {
return ImmutableList.copyOf(servers.values());
}
/**
* Registers a server with the proxy.
*
* @param serverInfo the server to register
* @return the registered server
*/
public RegisteredServer register(ServerInfo serverInfo) {
Preconditions.checkNotNull(serverInfo, "serverInfo");
String lowerName = serverInfo.getName().toLowerCase(Locale.US);
@ -47,6 +59,11 @@ public class ServerMap {
}
}
/**
* Unregisters the specified server from the proxy.
*
* @param serverInfo the server to unregister
*/
public void unregister(ServerInfo serverInfo) {
Preconditions.checkNotNull(serverInfo, "serverInfo");
String lowerName = serverInfo.getName().toLowerCase(Locale.US);

Datei anzeigen

@ -24,7 +24,6 @@ import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftEncoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftVarintFrameDecoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftVarintLengthEncoder;
import com.velocitypowered.proxy.server.ping.PingSessionHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;

Datei anzeigen

@ -4,9 +4,17 @@ import com.google.common.base.Preconditions;
import java.net.InetSocketAddress;
import java.net.URI;
public enum AddressUtil {
;
public class AddressUtil {
private AddressUtil() {
throw new AssertionError();
}
/**
* Attempts to parse an IP address of the form <code>127.0.0.1:25565</code>.
*
* @param ip the IP to parse
* @return the parsed address
*/
public static InetSocketAddress parseAddress(String ip) {
Preconditions.checkNotNull(ip, "ip");
URI uri = URI.create("tcp://" + ip);

Datei anzeigen

@ -12,6 +12,12 @@ import javax.crypto.Cipher;
public enum EncryptionUtils {
;
/**
* Generates an RSA key pair.
*
* @param keysize the key size (in bits) for the RSA key pair
* @return the generated key pair
*/
public static KeyPair createRsaKeyPair(final int keysize) {
try {
final KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
@ -22,16 +28,37 @@ public enum EncryptionUtils {
}
}
/**
* Generates a hex digest in two's complement form for use with the Mojang joinedServer endpoint.
*
* @param digest the bytes to digest
* @return the hex digest
*/
public static String twosComplementHexdigest(byte[] digest) {
return new BigInteger(digest).toString(16);
}
/**
* Decrypts an RSA message.
*
* @param keyPair the key pair to use
* @param bytes the bytes of the encrypted message
* @return the decrypted message
* @throws GeneralSecurityException if the message couldn't be decoded
*/
public static byte[] decryptRsa(KeyPair keyPair, byte[] bytes) throws GeneralSecurityException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
return cipher.doFinal(bytes);
}
/**
* Generates the server ID for the hasJoined endpoint.
*
* @param sharedSecret the shared secret between the client and the proxy
* @param key the RSA public key
* @return the server ID
*/
public static String generateServerId(byte[] sharedSecret, PublicKey key) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");

Datei anzeigen

@ -8,6 +8,9 @@ import java.net.InetAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
* A simple rate-limiter based on a Guava {@link Cache}.
*/
public class Ratelimiter {
private final Cache<InetAddress, Long> expiringCache;
@ -32,6 +35,12 @@ public class Ratelimiter {
}
}
/**
* Attempts to rate-limit the client.
*
* @param address the address to rate limit
* @return true if we should allow the client, false if we should rate-limit
*/
public boolean attempt(InetAddress address) {
if (timeoutNanos == 0) {
return true;

Datei anzeigen

@ -1,7 +1,9 @@
package com.velocitypowered.proxy.util;
public enum ThrowableUtils {
;
public class ThrowableUtils {
private ThrowableUtils() {
throw new AssertionError();
}
public static String briefDescription(Throwable throwable) {
return throwable.getClass().getSimpleName() + ": " + throwable.getMessage();

Datei anzeigen

@ -46,6 +46,11 @@ public class VelocityChannelRegistrar implements ChannelRegistrar {
return ImmutableList.copyOf(identifierMap.keySet());
}
/**
* Returns all channel IDs (as strings) for use with Minecraft 1.13 and above.
*
* @return the channel IDs for Minecraft 1.13 and above
*/
public Collection<String> getModernChannelIds() {
Collection<String> ids = new ArrayList<>();
for (ChannelIdentifier value : identifierMap.values()) {