Skip to content

Commit

Permalink
Add early send hack
Browse files Browse the repository at this point in the history
fix #1
  • Loading branch information
james58899 committed Feb 8, 2021
1 parent fa600a8 commit 73ce497
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# FabricProxy-Lite

Same as [FabricProxy](https://github.com/OKTW-Network/FabricProxy) but only support velocity and using Fabric-API handle velocity packet.
Same as [FabricProxy](https://github.com/OKTW-Network/FabricProxy) but only support velocity and using Fabric-API handle
velocity packet.

This will have the better compatibility with other mods.

## Important

**LuckPerms need enable `hackEarlySend` in config.**

Because Fabric-API can't send packet before QUERY_START event, so player info(UUID) will not ready at QUERY_START event.

Enable `hackEarlySend` will use mixin for early send packet to velocity.

## Setup

* Download mod
* Start server to generate config
* Setting velocity `player-info-forwarding-mode` to `modern` and `forwarding-secret`
Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.16.5
yarn_mappings=1.16.5+build.1
loader_version=0.11.0
yarn_mappings=1.16.5+build.4
loader_version=0.11.1
# Mod Properties
mod_version=1.0.1
mod_version=1.1.0
maven_group=one.oktw
archives_base_name=FabricProxy-Lite
# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.29.3+1.16
fabric_version=0.30.0+1.16
auto_config_version=3.3.1
17 changes: 14 additions & 3 deletions src/main/java/one/oktw/FabricProxyLite.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package one.oktw;

import com.mojang.authlib.GameProfile;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import me.sargunvohra.mcmods.autoconfig1u.serializer.Toml4jConfigSerializer;
import net.fabricmc.api.DedicatedServerModInitializer;
Expand All @@ -8,6 +9,7 @@
import net.fabricmc.fabric.api.networking.v1.ServerLoginConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerLoginNetworking;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.c2s.login.LoginHelloC2SPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerLoginNetworkHandler;
import net.minecraft.text.LiteralText;
Expand All @@ -27,7 +29,9 @@ public class FabricProxyLite implements DedicatedServerModInitializer, IMixinCon
public void onInitializeServer() {
// Packet receiver
ServerLoginNetworking.registerGlobalReceiver(VelocityLib.PLAYER_INFO_CHANNEL, this::handleVelocityPacket);
ServerLoginConnectionEvents.QUERY_START.register((handler, server, sender, synchronizer) -> sender.sendPacket(VelocityLib.PLAYER_INFO_CHANNEL, PacketByteBufs.empty()));
if (!config.getHackEarlySend()) {
ServerLoginConnectionEvents.QUERY_START.register((handler, server, sender, synchronizer) -> sender.sendPacket(VelocityLib.PLAYER_INFO_CHANNEL, PacketByteBufs.empty()));
}
}

private void handleVelocityPacket(MinecraftServer server, ServerLoginNetworkHandler handler, boolean understood, PacketByteBuf buf, ServerLoginNetworking.LoginSynchronizer synchronizer, PacketSender responseSender) {
Expand All @@ -44,7 +48,13 @@ private void handleVelocityPacket(MinecraftServer server, ServerLoginNetworkHand

((ClientConnection_AddressAccessor) handler.connection).setAddress(new java.net.InetSocketAddress(VelocityLib.readAddress(buf), ((java.net.InetSocketAddress) handler.connection.getAddress()).getPort()));

((ServerLoginNetworkHandler_ProfileAccessor) handler).setProfile(VelocityLib.createProfile(buf));
GameProfile profile = VelocityLib.createProfile(buf);

if (config.getHackEarlySend()) {
handler.onHello(new LoginHelloC2SPacket(profile));
} else {
((ServerLoginNetworkHandler_ProfileAccessor) handler).setProfile(profile);
}
}));
}

Expand All @@ -64,7 +74,8 @@ public String getRefMapperConfig() {

@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
return config.getHackOnlineMode() && !config.getSecret().isEmpty();
return mixinClassName.equals("one.oktw.mixin.hack.ServerLoginNetworkHandler_EarlySendPacket") && config.getHackEarlySend()
|| mixinClassName.equals("one.oktw.mixin.hack.ServerLoginNetworkHandler_SkipKeyPacket") && config.getHackOnlineMode();
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/one/oktw/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@Config(name = "FabricProxy-Lite")
public class ModConfig implements ConfigData {
private boolean hackOnlineMode = true;
private boolean hackEarlySend = false;

@Comment("Velocity proxy secret")
private String secret = "";
Expand All @@ -21,6 +22,15 @@ public boolean getHackOnlineMode() {
}
}

public boolean getHackEarlySend() {
String env = System.getenv("FABRIC_PROXY_HACK_FABRIC_API");
if (env == null) {
return hackEarlySend;
} else {
return Boolean.parseBoolean(env);
}
}

public String getSecret() {
String env = System.getenv("FABRIC_PROXY_SECRET");
if (env == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package one.oktw.mixin.hack;

import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.impl.networking.server.ServerLoginNetworkHandlerExtensions;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.c2s.login.LoginHelloC2SPacket;
import net.minecraft.server.network.ServerLoginNetworkHandler;
import one.oktw.VelocityLib;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerLoginNetworkHandler.class)
public class ServerLoginNetworkHandler_EarlySendPacket {
@Shadow
@Final
public ClientConnection connection;

@Inject(method = "onHello", at = @At(value = "HEAD"), cancellable = true)
private void skipKeyPacket(LoginHelloC2SPacket packet, CallbackInfo ci) {
if (packet.getProfile().isComplete()) return; // Already receive profile form velocity.

((ServerLoginNetworkHandlerExtensions) this).getAddon().sendPacket(VelocityLib.PLAYER_INFO_CHANNEL, PacketByteBufs.empty());
ci.cancel();
}
}
1 change: 1 addition & 0 deletions src/main/resources/hack.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"plugin": "one.oktw.FabricProxyLite",
"compatibilityLevel": "JAVA_8",
"mixins": [
"ServerLoginNetworkHandler_EarlySendPacket",
"ServerLoginNetworkHandler_SkipKeyPacket"
],
"injectors": {
Expand Down

0 comments on commit 73ce497

Please sign in to comment.