Skip to content

Commit 333756d

Browse files
committed
Added config option to disable handshake address rewriting
Fixes #482
1 parent 6f454f0 commit 333756d

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
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
@@ -155,6 +155,13 @@ public class ViaProxyConfig {
155155
})
156156
private boolean bungeecordPlayerInfoPassthrough = false;
157157

158+
@Option("rewrite-handshake-packet")
159+
@Description({
160+
"Enabling this will rewrite the address in the handshake packet to a value the vanilla client would have sent when connecting directly to the target server.",
161+
"This should be left enabled unless you are a server owner and you need the original address on the backend server."
162+
})
163+
private boolean rewriteHandshakePacket = true;
164+
158165
@Option("rewrite-transfer-packets")
159166
@Description({
160167
"Enabling this will rewrite transfer packets to point back to ViaProxy. This allows ViaProxy to perform protocol translation when forwarding the player to the actual server from the transfer packet.",
@@ -473,6 +480,15 @@ public void setPassthroughBungeecordPlayerInfo(final boolean bungeecordPlayerInf
473480
this.save();
474481
}
475482

483+
public boolean shouldRewriteHandshakePacket() {
484+
return this.rewriteHandshakePacket;
485+
}
486+
487+
public void setRewriteHandshakePacket(final boolean rewriteHandshakePacket) {
488+
this.rewriteHandshakePacket = rewriteHandshakePacket;
489+
this.save();
490+
}
491+
476492
public boolean shouldRewriteTransferPackets() {
477493
return this.rewriteTransferPackets;
478494
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -267,26 +267,27 @@ private void connect(final SocketAddress serverAddress, final ProtocolVersion se
267267
}
268268
ViaProxy.EVENT_MANAGER.call(new ConnectEvent(this.proxyConnection));
269269

270+
final int handshakePort;
271+
if (ViaProxy.getConfig().shouldRewriteHandshakePacket()) {
272+
if (serverAddress instanceof InetSocketAddress inetSocketAddress) {
273+
handshakeParts[0] = inetSocketAddress.getHostString();
274+
handshakePort = inetSocketAddress.getPort();
275+
} else {
276+
handshakeParts[0] = AddressUtil.toString(serverAddress);
277+
handshakePort = 25565;
278+
}
279+
} else {
280+
handshakePort = clientHandshakeAddress.getPort();
281+
}
282+
270283
this.proxyConnection.connectToServer(serverAddress, serverVersion).addListeners((ThrowingChannelFutureListener) f -> {
271284
if (f.isSuccess()) {
272285
f.channel().eventLoop().submit(() -> { // Reschedule so the packets get sent after the channel is fully initialized and active
273286
if (ViaProxy.getConfig().useBackendHaProxy()) {
274287
this.proxyConnection.getChannel().writeAndFlush(HAProxyUtil.createMessage(this.proxyConnection.getC2P(), this.proxyConnection.getChannel(), clientVersion)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
275288
}
276289

277-
final String address;
278-
final int port;
279-
if (serverAddress instanceof InetSocketAddress inetSocketAddress) {
280-
address = inetSocketAddress.getHostString();
281-
port = inetSocketAddress.getPort();
282-
} else {
283-
address = AddressUtil.toString(serverAddress);
284-
port = 25565;
285-
}
286-
287-
handshakeParts[0] = address;
288-
final C2SHandshakingClientIntentionPacket newHandshakePacket = new C2SHandshakingClientIntentionPacket(clientVersion.getOriginalVersion(), String.join("\0", handshakeParts), port, intendedState);
289-
290+
final C2SHandshakingClientIntentionPacket newHandshakePacket = new C2SHandshakingClientIntentionPacket(clientVersion.getOriginalVersion(), String.join("\0", handshakeParts), handshakePort, intendedState);
290291
this.proxyConnection.getChannel().writeAndFlush(newHandshakePacket).addListeners(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE, (ChannelFutureListener) f2 -> {
291292
if (f2.isSuccess()) {
292293
final UserConnection userConnection = this.proxyConnection.getUserConnection();

0 commit comments

Comments
 (0)