From e86148c7a3b10357247d4cb23335bdc37064dd04 Mon Sep 17 00:00:00 2001 From: drzamajka Date: Thu, 4 Jun 2026 16:59:41 +0200 Subject: [PATCH] feat: added direct connections option --- README.md | 1 + src/main/java/one/oktw/ModConfig.java | 12 ++++- src/main/java/one/oktw/PacketHandler.java | 60 ++++++++++++----------- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 9f9522c..46da487 100644 --- a/README.md +++ b/README.md @@ -19,5 +19,6 @@ Here are all the parameters that you can set in the `FabricProxy-Lite.toml` conf | `hackOnlineMode` | `FABRIC_PROXY_HACK_ONLINE_MODE` | Allows connection through a proxy without disabling online-mode. | `true` | | | `hackEarlySend` | `FABRIC_PROXY_HACK_EARLY_SEND` | Fabric-API can't send packet before QUERY_START event, so player info(UUID) will not ready at QUERY_START event. Setting `hackEarlySend` to `true` will use mixin for early send packet to velocity.

This is **required** for some mods, such as LuckPerms. | `false` | | | `hackMessageChain` | `FABRIC_PROXY_HACK_MESSAGE_CHAIN` | This option fixes players being kicked for `Received chat packet with missing or invalid signature.` or `Chat message validation failure`, which only happens when the player switches to another server.

See [#30](/../../issues/30) and [#121](/../../discussions/121) for more info. | `false` | | +| `allowDirectConnections` | `FABRIC_PROXY_ALLOW_DIRECT_CONNECTIONS` | Allow direct connections from clients without Velocity. When enabled, the mod will accept non-Velocity connections. | `false` | | | `disconnectMessage` | `FABRIC_PROXY_DISCONNECT_MESSAGE` | The custom disconnect/kick message for users that aren't connecting through Velocity. | `"This server requires you to connect with Velocity."` | | | `secret` | `FABRIC_PROXY_SECRET` | The Velocity forwarding secret. This should be the same random string as in Velocity's `forwarding.secret` file.

Alternatively you could set the `FABRIC_PROXY_SECRET_FILE` environment variable to read the secret from a file instead of config. | | ✓ | diff --git a/src/main/java/one/oktw/ModConfig.java b/src/main/java/one/oktw/ModConfig.java index 52f1c94..9a02d7d 100644 --- a/src/main/java/one/oktw/ModConfig.java +++ b/src/main/java/one/oktw/ModConfig.java @@ -13,6 +13,7 @@ public class ModConfig { private boolean hackOnlineMode = true; private boolean hackEarlySend = false; private boolean hackMessageChain = false; + private boolean allowDirectConnections = false; private String disconnectMessage = "This server requires you to connect with Velocity."; private String secret = ""; @@ -50,6 +51,11 @@ public static ModConfig load(Path configPath) { config.hackMessageChain = Boolean.parseBoolean(envHackMessageChain); } + String envAllowDirectConnections = System.getenv("FABRIC_PROXY_ALLOW_DIRECT_CONNECTIONS"); + if (envAllowDirectConnections != null) { + config.allowDirectConnections = Boolean.parseBoolean(envAllowDirectConnections); + } + String envDisconnectMessage = System.getenv("FABRIC_PROXY_DISCONNECT_MESSAGE"); if(envDisconnectMessage != null) { config.disconnectMessage = envDisconnectMessage; @@ -88,7 +94,11 @@ public boolean getHackMessageChain() { return hackMessageChain; } + public boolean getAllowDirectConnections() { + return allowDirectConnections; + } + public String getSecret() { return secret; } -} +} \ No newline at end of file diff --git a/src/main/java/one/oktw/PacketHandler.java b/src/main/java/one/oktw/PacketHandler.java index e8948ac..47424c6 100644 --- a/src/main/java/one/oktw/PacketHandler.java +++ b/src/main/java/one/oktw/PacketHandler.java @@ -22,40 +22,42 @@ class PacketHandler { void handleVelocityPacket(MinecraftServer server, ServerLoginNetworkHandler handler, boolean understood, PacketByteBuf buf, ServerLoginNetworking.LoginSynchronizer synchronizer, PacketSender ignored) { if (!understood) { - handler.disconnect(Text.of(config.getAbortedMessage())); - return; - } - - synchronizer.waitFor(server.submit(() -> { - try { - if (!VelocityLib.checkIntegrity(buf)) { + if (!this.config.getAllowDirectConnections()) { + handler.disconnect(Text.of(config.getAbortedMessage())); + return; + } + } else { + synchronizer.waitFor(server.submit(() -> { + try { + if (!VelocityLib.checkIntegrity(buf)) { + handler.disconnect(Text.of("Unable to verify player details")); + return; + } + VelocityLib.checkVersion(buf); + } catch (Throwable e) { + LogManager.getLogger().error("Secret check failed.", e); handler.disconnect(Text.of("Unable to verify player details")); return; } - VelocityLib.checkVersion(buf); - } catch (Throwable e) { - LogManager.getLogger().error("Secret check failed.", e); - handler.disconnect(Text.of("Unable to verify player details")); - return; - } - ClientConnection connection = ((ServerLoginNetworkHandlerAccessor) handler).getConnection(); - ((ClientConnection_AddressAccessor) connection).setAddress(new java.net.InetSocketAddress(VelocityLib.readAddress(buf), ((java.net.InetSocketAddress) (connection.getAddress())).getPort())); + ClientConnection connection = ((ServerLoginNetworkHandlerAccessor) handler).getConnection(); + ((ClientConnection_AddressAccessor) connection).setAddress(new java.net.InetSocketAddress(VelocityLib.readAddress(buf), ((java.net.InetSocketAddress) (connection.getAddress())).getPort())); - GameProfile profile; - try { - profile = VelocityLib.createProfile(buf); - } catch (Exception e) { - LogManager.getLogger().error("Profile create failed.", e); - handler.disconnect(Text.of("Unable to read player profile")); - return; - } + GameProfile profile; + try { + profile = VelocityLib.createProfile(buf); + } catch (Exception e) { + LogManager.getLogger().error("Profile create failed.", e); + handler.disconnect(Text.of("Unable to read player profile")); + return; + } - if (config.getHackEarlySend()) { - handler.onHello(new LoginHelloC2SPacket(profile.name(), profile.id())); - } + if (config.getHackEarlySend()) { + handler.onHello(new LoginHelloC2SPacket(profile.name(), profile.id())); + } - ((ServerLoginNetworkHandlerAccessor) handler).setProfile(profile); - })); + ((ServerLoginNetworkHandlerAccessor) handler).setProfile(profile); + })); + } } -} +} \ No newline at end of file