2023-10-20 18:32:52 +02:00
|
|
|
/*
|
|
|
|
* This file is part of ViaForge - https://github.com/FlorianMichael/ViaForge
|
2024-01-01 16:52:20 +01:00
|
|
|
* Copyright (C) 2021-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> and contributors
|
2023-10-20 18:32:52 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2023-12-02 01:14:12 +01:00
|
|
|
|
2023-10-20 18:32:52 +02:00
|
|
|
package de.florianmichael.viaforge.mixin;
|
|
|
|
|
2023-10-21 16:08:11 +02:00
|
|
|
import de.florianmichael.viaforge.common.ViaForgeCommon;
|
2023-10-26 01:05:26 +02:00
|
|
|
import de.florianmichael.viaforge.common.protocolhack.netty.VFNetworkManager;
|
2023-10-20 18:32:52 +02:00
|
|
|
import io.netty.channel.Channel;
|
2023-10-26 01:05:26 +02:00
|
|
|
import net.minecraft.network.NettyEncryptingDecoder;
|
|
|
|
import net.minecraft.network.NettyEncryptingEncoder;
|
2023-10-20 18:32:52 +02:00
|
|
|
import net.minecraft.network.NetworkManager;
|
2023-10-26 01:05:26 +02:00
|
|
|
import net.minecraft.util.CryptManager;
|
|
|
|
import net.minecraft.util.LazyValue;
|
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
|
import net.raphimc.vialoader.netty.VLLegacyPipeline;
|
|
|
|
import net.raphimc.vialoader.util.VersionEnum;
|
2023-10-20 18:32:52 +02:00
|
|
|
import org.spongepowered.asm.mixin.Mixin;
|
|
|
|
import org.spongepowered.asm.mixin.Shadow;
|
2023-10-26 01:05:26 +02:00
|
|
|
import org.spongepowered.asm.mixin.Unique;
|
2023-10-20 18:32:52 +02:00
|
|
|
import org.spongepowered.asm.mixin.injection.At;
|
|
|
|
import org.spongepowered.asm.mixin.injection.Inject;
|
|
|
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
2023-10-26 01:05:26 +02:00
|
|
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
|
|
|
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
|
|
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
import javax.crypto.SecretKey;
|
|
|
|
import java.net.InetAddress;
|
2023-10-20 18:32:52 +02:00
|
|
|
|
|
|
|
@Mixin(NetworkManager.class)
|
2023-10-26 01:05:26 +02:00
|
|
|
public class MixinNetworkManager implements VFNetworkManager {
|
2023-10-20 18:32:52 +02:00
|
|
|
|
|
|
|
@Shadow private Channel channel;
|
|
|
|
|
2023-10-26 01:05:26 +02:00
|
|
|
@Unique
|
2023-12-02 01:14:12 +01:00
|
|
|
private Cipher viaForge$decryptionCipher;
|
2023-10-26 01:05:26 +02:00
|
|
|
|
|
|
|
@Unique
|
2023-12-02 01:14:12 +01:00
|
|
|
private VersionEnum viaForge$targetVersion;
|
2023-10-26 01:05:26 +02:00
|
|
|
|
|
|
|
@Inject(method = "connectToServer", at = @At(value = "INVOKE", target = "Lio/netty/bootstrap/Bootstrap;group(Lio/netty/channel/EventLoopGroup;)Lio/netty/bootstrap/AbstractBootstrap;"), locals = LocalCapture.CAPTURE_FAILHARD)
|
|
|
|
private static void trackSelfTarget(InetAddress p_181124_0_, int p_181124_1_, boolean p_181124_2_, CallbackInfoReturnable<NetworkManager> cir, NetworkManager networkmanager, Class oclass, LazyValue lazyvalue) {
|
|
|
|
// The connecting screen and server pinger are setting the main target version when a specific version for a server is set,
|
|
|
|
// This works for joining perfect since we can simply restore the version when the server doesn't have a specific one set,
|
|
|
|
// but for the server pinger we need to store the target version and force the pinging to use the target version.
|
|
|
|
// Due to the fact that the server pinger is being called multiple times.
|
2023-12-02 01:14:12 +01:00
|
|
|
((VFNetworkManager) networkmanager).viaForge$setTrackedVersion(ViaForgeCommon.getManager().getTargetVersion());
|
2023-10-26 01:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Inject(method = "setEncryptionKey", at = @At("HEAD"), cancellable = true)
|
|
|
|
private void storeEncryptionCiphers(Cipher p_244777_1_, Cipher p_244777_2_, CallbackInfo ci) {
|
|
|
|
if (ViaForgeCommon.getManager().getTargetVersion().isOlderThanOrEqualTo(VersionEnum.r1_6_4)) {
|
|
|
|
// Minecraft's encryption code is bad for us, we need to reorder the pipeline
|
|
|
|
ci.cancel();
|
|
|
|
|
|
|
|
// Minecraft 1.6.4 supports tile encryption which means the server can only disable one side of the encryption
|
|
|
|
// So we only enable the encryption side and later enable the decryption side if the 1.7 -> 1.6 protocol
|
|
|
|
// tells us to do, therefore we need to store the cipher instance.
|
2023-12-02 01:14:12 +01:00
|
|
|
this.viaForge$decryptionCipher = p_244777_1_;
|
2023-10-26 01:05:26 +02:00
|
|
|
|
|
|
|
// Enabling the encryption side
|
|
|
|
this.channel.pipeline().addBefore(VLLegacyPipeline.VIALEGACY_PRE_NETTY_LENGTH_REMOVER_NAME, "encrypt", new NettyEncryptingEncoder(p_244777_2_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Inject(method = "disconnect", at = @At("HEAD"))
|
|
|
|
public void restoreTargetVersion(ITextComponent p_150718_1_, CallbackInfo ci) {
|
|
|
|
// If the previous server forced a version, we need to restore the version to the default one.
|
|
|
|
ViaForgeCommon.getManager().restoreVersion();
|
|
|
|
}
|
|
|
|
|
2023-10-20 18:32:52 +02:00
|
|
|
@Inject(method = "setupCompression", at = @At("RETURN"))
|
|
|
|
public void reorderPipeline(int p_setCompressionTreshold_1_, CallbackInfo ci) {
|
2023-10-21 16:08:11 +02:00
|
|
|
ViaForgeCommon.getManager().reorderCompression(channel);
|
2023-10-20 18:32:52 +02:00
|
|
|
}
|
2023-10-26 01:05:26 +02:00
|
|
|
|
|
|
|
@Override
|
2023-12-02 01:14:12 +01:00
|
|
|
public void viaForge$setupPreNettyDecryption() {
|
2023-10-26 01:05:26 +02:00
|
|
|
// Enabling the decryption side for 1.6.4 if the 1.7 -> 1.6 protocol tells us to do
|
2023-12-02 01:14:12 +01:00
|
|
|
this.channel.pipeline().addBefore(VLLegacyPipeline.VIALEGACY_PRE_NETTY_LENGTH_REMOVER_NAME, "decrypt", new NettyEncryptingDecoder(this.viaForge$decryptionCipher));
|
2023-10-26 01:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-12-02 01:14:12 +01:00
|
|
|
public VersionEnum viaForge$getTrackedVersion() {
|
|
|
|
return viaForge$targetVersion;
|
2023-10-26 01:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-12-02 01:14:12 +01:00
|
|
|
public void viaForge$setTrackedVersion(VersionEnum version) {
|
|
|
|
viaForge$targetVersion = version;
|
2023-10-26 01:05:26 +02:00
|
|
|
}
|
2023-12-02 01:14:12 +01:00
|
|
|
|
2023-10-20 18:32:52 +02:00
|
|
|
}
|