3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-12-24 15:20:35 +01:00

Avoid a memory copy in creating modern-style forwarding data

Dieser Commit ist enthalten in:
Andrew Steinborn 2019-07-29 06:04:37 -04:00
Ursprung 1da51b8ffb
Commit 04efd16a83

Datei anzeigen

@ -117,32 +117,28 @@ public class LoginSessionHandler implements MinecraftSessionHandler {
private static ByteBuf createForwardingData(byte[] hmacSecret, String address, private static ByteBuf createForwardingData(byte[] hmacSecret, String address,
GameProfile profile) { GameProfile profile) {
ByteBuf dataToForward = Unpooled.buffer(); ByteBuf forwarded = Unpooled.buffer(2048);
ByteBuf finalData = Unpooled.buffer();
try { try {
ProtocolUtils.writeVarInt(dataToForward, VelocityConstants.FORWARDING_VERSION); ProtocolUtils.writeVarInt(forwarded, VelocityConstants.FORWARDING_VERSION);
ProtocolUtils.writeString(dataToForward, address); ProtocolUtils.writeString(forwarded, address);
ProtocolUtils.writeUuid(dataToForward, profile.getId()); ProtocolUtils.writeUuid(forwarded, profile.getId());
ProtocolUtils.writeString(dataToForward, profile.getName()); ProtocolUtils.writeString(forwarded, profile.getName());
ProtocolUtils.writeProperties(dataToForward, profile.getProperties()); ProtocolUtils.writeProperties(forwarded, profile.getProperties());
SecretKey key = new SecretKeySpec(hmacSecret, "HmacSHA256"); SecretKey key = new SecretKeySpec(hmacSecret, "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256");
mac.init(key); mac.init(key);
mac.update(dataToForward.array(), dataToForward.arrayOffset(), dataToForward.readableBytes()); mac.update(forwarded.array(), forwarded.arrayOffset(), forwarded.readableBytes());
byte[] sig = mac.doFinal(); byte[] sig = mac.doFinal();
finalData.writeBytes(sig);
finalData.writeBytes(dataToForward); return Unpooled.wrappedBuffer(Unpooled.wrappedBuffer(sig), forwarded);
return finalData;
} catch (InvalidKeyException e) { } catch (InvalidKeyException e) {
finalData.release(); forwarded.release();
throw new RuntimeException("Unable to authenticate data", e); throw new RuntimeException("Unable to authenticate data", e);
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
// Should never happen // Should never happen
finalData.release(); forwarded.release();
throw new AssertionError(e); throw new AssertionError(e);
} finally {
dataToForward.release();
} }
} }
} }