Fixed HAProxy IPv6 issues and added HAProxy support to legacy passthrough

This commit is contained in:
RaphiMC 2023-09-08 12:44:28 +02:00
parent 60da5c809a
commit 305714905b
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
4 changed files with 95 additions and 17 deletions

View File

@ -19,12 +19,10 @@ package net.raphimc.viaproxy.proxy.client2proxy;
import com.google.common.collect.Lists;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.haproxy.*;
import net.raphimc.netminecraft.constants.ConnectionState;
import net.raphimc.netminecraft.packet.IPacket;
import net.raphimc.netminecraft.packet.impl.handshake.C2SHandshakePacket;
@ -46,14 +44,13 @@ import net.raphimc.viaproxy.proxy.session.DummyProxyConnection;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
import net.raphimc.viaproxy.proxy.util.CloseAndReturn;
import net.raphimc.viaproxy.proxy.util.ExceptionUtil;
import net.raphimc.viaproxy.proxy.util.HAProxyUtil;
import net.raphimc.viaproxy.util.ArrayHelper;
import net.raphimc.viaproxy.util.logging.Logger;
import java.net.ConnectException;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.nio.channels.UnresolvedAddressException;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import java.util.regex.Pattern;
@ -236,13 +233,7 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
}
if (Options.SERVER_HAPROXY_PROTOCOL) {
final InetSocketAddress sourceAddress = (InetSocketAddress) this.proxyConnection.getC2P().remoteAddress();
final InetSocketAddress targetAddress = (InetSocketAddress) this.proxyConnection.getChannel().remoteAddress();
final HAProxyProxiedProtocol protocol = sourceAddress.getAddress() instanceof Inet4Address ? HAProxyProxiedProtocol.TCP4 : HAProxyProxiedProtocol.TCP6;
final HAProxyTLV tlv = new HAProxyTLV((byte) 0xE0, Unpooled.buffer().writeInt(clientVersion.getOriginalVersion()));
final HAProxyMessage haProxyMessage = new HAProxyMessage(HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, protocol, sourceAddress.getAddress().getHostAddress(), targetAddress.getAddress().getHostAddress(), sourceAddress.getPort(), targetAddress.getPort(), Collections.singletonList(tlv));
this.proxyConnection.getChannel().writeAndFlush(haProxyMessage).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
this.proxyConnection.getChannel().writeAndFlush(HAProxyUtil.createMessage(this.proxyConnection.getC2P(), this.proxyConnection.getChannel(), clientVersion)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
handshakeParts[0] = serverAddress.getAddress();

View File

@ -20,10 +20,14 @@ package net.raphimc.viaproxy.proxy.client2proxy.passthrough;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.handler.codec.haproxy.HAProxyMessageEncoder;
import net.raphimc.netminecraft.constants.MCPipeline;
import net.raphimc.netminecraft.netty.connection.NetClient;
import net.raphimc.netminecraft.util.ServerAddress;
import net.raphimc.viaproxy.cli.options.Options;
import net.raphimc.viaproxy.proxy.proxy2server.Proxy2ServerChannelInitializer;
import net.raphimc.viaproxy.proxy.util.ExceptionUtil;
import net.raphimc.viaproxy.proxy.util.HAProxyUtil;
import net.raphimc.viaproxy.util.logging.Logger;
import java.util.function.Function;
@ -97,6 +101,10 @@ public class LegacyClientPassthroughHandler extends SimpleChannelInboundHandler<
this.p2sConnection = null;
this.c2pChannel.close();
}
if (Options.SERVER_HAPROXY_PROTOCOL) {
this.p2sConnection.getChannel().writeAndFlush(HAProxyUtil.createMessage(this.c2pChannel, this.p2sConnection.getChannel(), null)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
}
protected ServerAddress getServerAddress() {
@ -104,10 +112,13 @@ public class LegacyClientPassthroughHandler extends SimpleChannelInboundHandler<
}
protected Function<Supplier<ChannelHandler>, ChannelInitializer<Channel>> getChannelInitializerSupplier() {
return s -> new ChannelInitializer<Channel>() {
return s -> new ChannelInitializer<>() {
@Override
protected void initChannel(Channel channel) {
channel.pipeline().addLast(s.get());
if (Options.SERVER_HAPROXY_PROTOCOL) {
channel.pipeline().addLast(Proxy2ServerChannelInitializer.VIAPROXY_HAPROXY_ENCODER_NAME, HAProxyMessageEncoder.INSTANCE);
}
channel.pipeline().addLast(MCPipeline.HANDLER_HANDLER_NAME, s.get());
}
};
}

View File

@ -87,16 +87,18 @@ public class Proxy2ServerChannelInitializer extends MinecraftChannelInitializer
final String password = proxyUrl.getUserInfo() != null && proxyUrl.getUserInfo().contains(":") ? proxyUrl.getUserInfo().split(":")[1] : null;
switch (proxyUrl.getScheme().toUpperCase(Locale.ROOT)) {
case "HTTP":
case "HTTPS":
case "HTTP", "HTTPS" -> {
if (username != null && password != null) return new HttpProxyHandler(proxyAddress, username, password);
else return new HttpProxyHandler(proxyAddress);
case "SOCKS4":
}
case "SOCKS4" -> {
if (username != null) return new Socks4ProxyHandler(proxyAddress, username);
else return new Socks4ProxyHandler(proxyAddress);
case "SOCKS5":
}
case "SOCKS5" -> {
if (username != null && password != null) return new Socks5ProxyHandler(proxyAddress, username, password);
else return new Socks5ProxyHandler(proxyAddress);
}
}
throw new IllegalArgumentException("Unknown proxy type: " + proxyUrl.getScheme());

View File

@ -0,0 +1,74 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* 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/>.
*/
package net.raphimc.viaproxy.proxy.util;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.handler.codec.haproxy.*;
import net.raphimc.vialoader.util.VersionEnum;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
public class HAProxyUtil {
public static HAProxyMessage createMessage(final Channel sourceChannel, final Channel targetChannel, final VersionEnum clientVersion) {
final InetSocketAddress sourceAddress = (InetSocketAddress) sourceChannel.remoteAddress();
final InetSocketAddress targetAddress = (InetSocketAddress) targetChannel.remoteAddress();
final HAProxyProxiedProtocol protocol = sourceAddress.getAddress() instanceof Inet4Address ? HAProxyProxiedProtocol.TCP4 : HAProxyProxiedProtocol.TCP6;
final List<HAProxyTLV> tlvs = new ArrayList<>();
if (clientVersion != null) {
tlvs.add(new HAProxyTLV((byte) 0xE0, Unpooled.buffer().writeInt(clientVersion.getOriginalVersion())));
}
final String sourceAddressString = sourceAddress.getAddress().getHostAddress();
final String targetAddressString = protocol.addressFamily().equals(HAProxyProxiedProtocol.AddressFamily.AF_IPv6) ? getIPv6Address(targetAddress.getHostString()).getHostAddress() : getIPv4Address(targetAddress.getHostString()).getHostAddress();
return new HAProxyMessage(HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, protocol, sourceAddressString, targetAddressString, sourceAddress.getPort(), targetAddress.getPort(), tlvs);
}
private static Inet6Address getIPv6Address(final String host) {
try {
final InetAddress[] addresses = InetAddress.getAllByName(host);
for (InetAddress addr : addresses) {
if (addr instanceof Inet6Address) {
return (Inet6Address) addr;
}
}
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
return null;
}
private static Inet4Address getIPv4Address(final String host) {
try {
final InetAddress[] addresses = InetAddress.getAllByName(host);
for (InetAddress addr : addresses) {
if (addr instanceof Inet4Address) {
return (Inet4Address) addr;
}
}
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
return null;
}
}