Skip to content

Commit a17daa8

Browse files
committed
Added option to send a ViaVersion connection details custom payload to the server
1 parent ec44742 commit a17daa8

File tree

4 files changed

+95
-24
lines changed

4 files changed

+95
-24
lines changed

src/main/java/net/raphimc/viaproxy/protocoltranslator/viaproxy/ViaProxyConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ public class ViaProxyConfig {
190190
})
191191
private boolean workAroundConfigStatePacketQueue = false;
192192

193+
@Option("send-connection-details")
194+
@Description({
195+
"If enabled, ViaProxy will send a connection details custom payload packet to the server.",
196+
"This includes the actual client version as well as the ViaProxy version."
197+
})
198+
private boolean sendConnectionDetails = false;
199+
193200
public static ViaProxyConfig create(final File configFile) {
194201
final ConfigLoader<ViaProxyConfig> configLoader = new ConfigLoader<>(ViaProxyConfig.class);
195202
configLoader.getConfigOptions().setResetInvalidOptions(true).setRewriteConfig(true).setCommentSpacing(1);
@@ -494,6 +501,15 @@ public void setWorkAroundConfigStatePacketQueue(final boolean workAroundConfigSt
494501
this.save();
495502
}
496503

504+
public boolean shouldSendConnectionDetails() {
505+
return this.sendConnectionDetails;
506+
}
507+
508+
public void setSendConnectionDetails(final boolean sendConnectionDetails) {
509+
this.sendConnectionDetails = sendConnectionDetails;
510+
this.save();
511+
}
512+
497513
@Validator("target-version")
498514
private ProtocolVersion validateTargetVersion(final ProtocolVersion targetVersion) {
499515
if (targetVersion == null) {

src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.viaversion.viabackwards.protocol.v1_20_5to1_20_3.storage.CookieStorage;
2323
import com.viaversion.viaversion.api.connection.UserConnection;
2424
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
25+
import com.viaversion.viaversion.api.protocol.version.VersionType;
2526
import io.netty.channel.ChannelFutureListener;
2627
import io.netty.channel.ChannelHandler;
2728
import io.netty.channel.ChannelHandlerContext;
@@ -277,7 +278,12 @@ private void connect(final SocketAddress serverAddress, final ProtocolVersion se
277278
if (clientVersion.newerThanOrEqualTo(ProtocolVersion.v1_19_3) && serverVersion.newerThanOrEqualTo(ProtocolVersion.v1_19_3)) {
278279
this.proxyConnection.getPacketHandlers().add(new ChatSignaturePacketHandler(this.proxyConnection));
279280
}
280-
this.proxyConnection.getPacketHandlers().add(new ResourcePackPacketHandler(this.proxyConnection));
281+
if (!ViaProxy.getConfig().getResourcePackUrl().isBlank()) {
282+
this.proxyConnection.getPacketHandlers().add(new ResourcePackPacketHandler(this.proxyConnection));
283+
}
284+
if (ViaProxy.getConfig().shouldSendConnectionDetails() && !serverVersion.equals(clientVersion) && serverVersion.newerThanOrEqualTo(ProtocolVersion.v1_8) && serverVersion.getVersionType() == VersionType.RELEASE) {
285+
this.proxyConnection.getPacketHandlers().add(new ViaVersionConnectionDetailsPacketHandler(this.proxyConnection));
286+
}
281287
this.proxyConnection.getPacketHandlers().add(new UnexpectedPacketHandler(this.proxyConnection));
282288

283289
Logger.u_info("connect", this.proxyConnection, "[" + clientVersion.getName() + " <-> " + serverVersion.getName() + "] Connecting to " + AddressUtil.toString(serverAddress));

src/main/java/net/raphimc/viaproxy/proxy/packethandler/ResourcePackPacketHandler.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,29 @@ public boolean handleP2S(Packet packet, List<ChannelFutureListener> listeners) {
6464
}
6565

6666
private void sendResourcePack() {
67-
if (!ViaProxy.getConfig().getResourcePackUrl().isBlank()) {
68-
this.proxyConnection.getChannel().eventLoop().schedule(() -> {
69-
try {
70-
final String url = ViaProxy.getConfig().getResourcePackUrl();
71-
final boolean required = Via.getConfig().isForcedUse1_17ResourcePack();
72-
final TextComponent message;
73-
if (Via.getConfig().get1_17ResourcePackPrompt() != null) {
74-
message = TextComponentSerializer.LATEST.deserialize(Via.getConfig().get1_17ResourcePackPrompt().toString());
75-
} else {
76-
message = null;
77-
}
67+
this.proxyConnection.getChannel().eventLoop().schedule(() -> {
68+
try {
69+
final String url = ViaProxy.getConfig().getResourcePackUrl();
70+
final boolean required = Via.getConfig().isForcedUse1_17ResourcePack();
71+
final TextComponent message;
72+
if (Via.getConfig().get1_17ResourcePackPrompt() != null) {
73+
message = TextComponentSerializer.LATEST.deserialize(Via.getConfig().get1_17ResourcePackPrompt().toString());
74+
} else {
75+
message = null;
76+
}
7877

79-
if (this.proxyConnection.getClientVersion().newerThanOrEqualTo(ProtocolVersion.v1_20_3)) {
80-
this.proxyConnection.getC2P().writeAndFlush(new S2CPlayResourcePackPushPacket(UUID.randomUUID(), url, "", required, message)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
81-
} else if (this.proxyConnection.getClientVersion().newerThanOrEqualTo(ProtocolVersion.v1_8)) {
82-
this.proxyConnection.getC2P().writeAndFlush(new S2CPlayResourcePackPacket(url, "", required, message)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
83-
} else if (this.proxyConnection.getClientVersion().newerThanOrEqualTo(ProtocolVersion.v1_7_2)) {
84-
final byte[] data = url.getBytes(StandardCharsets.UTF_8);
85-
this.proxyConnection.getC2P().writeAndFlush(new S2CPlayCustomPayloadPacket("MC|RPack", data)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
86-
}
87-
} catch (Throwable e) {
88-
Logger.LOGGER.warn("Failed to send resource pack", e);
78+
if (this.proxyConnection.getClientVersion().newerThanOrEqualTo(ProtocolVersion.v1_20_3)) {
79+
this.proxyConnection.getC2P().writeAndFlush(new S2CPlayResourcePackPushPacket(UUID.randomUUID(), url, "", required, message)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
80+
} else if (this.proxyConnection.getClientVersion().newerThanOrEqualTo(ProtocolVersion.v1_8)) {
81+
this.proxyConnection.getC2P().writeAndFlush(new S2CPlayResourcePackPacket(url, "", required, message)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
82+
} else if (this.proxyConnection.getClientVersion().newerThanOrEqualTo(ProtocolVersion.v1_7_2)) {
83+
final byte[] data = url.getBytes(StandardCharsets.UTF_8);
84+
this.proxyConnection.getC2P().writeAndFlush(new S2CPlayCustomPayloadPacket("MC|RPack", data)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
8985
}
90-
}, 250, TimeUnit.MILLISECONDS);
91-
}
86+
} catch (Throwable e) {
87+
Logger.LOGGER.warn("Failed to send resource pack", e);
88+
}
89+
}, 250, TimeUnit.MILLISECONDS);
9290
}
9391

9492
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
3+
* Copyright (C) 2021-2025 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.viaproxy.proxy.packethandler;
19+
20+
import com.viaversion.viaversion.connection.ConnectionDetails;
21+
import io.netty.channel.ChannelFutureListener;
22+
import net.raphimc.netminecraft.constants.ConnectionState;
23+
import net.raphimc.netminecraft.constants.MCPackets;
24+
import net.raphimc.netminecraft.packet.Packet;
25+
import net.raphimc.netminecraft.packet.UnknownPacket;
26+
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
27+
28+
import java.util.List;
29+
30+
public class ViaVersionConnectionDetailsPacketHandler extends PacketHandler {
31+
32+
private final int joinGameId;
33+
34+
public ViaVersionConnectionDetailsPacketHandler(ProxyConnection proxyConnection) {
35+
super(proxyConnection);
36+
37+
this.joinGameId = MCPackets.S2C_LOGIN.getId(this.proxyConnection.getClientVersion().getVersion());
38+
}
39+
40+
@Override
41+
public boolean handleP2S(Packet packet, List<ChannelFutureListener> listeners) {
42+
if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getP2sConnectionState() == ConnectionState.PLAY) {
43+
if (unknownPacket.packetId == this.joinGameId) {
44+
ConnectionDetails.sendConnectionDetails(this.proxyConnection.getUserConnection(), ConnectionDetails.APP_CHANNEL);
45+
}
46+
}
47+
48+
return true;
49+
}
50+
51+
}

0 commit comments

Comments
 (0)