Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public interface InboundConnection {
*/
Optional<String> getRawVirtualHost();

/**
* If the connection was made through a proxy using the HAProxy protocol, this will contain the
* real address of the proxy. Otherwise, this will be empty.
*
* @return the address of the HAProxy proxy, if applicable
*/
Optional<InetSocketAddress> getHaProxyAddress();

/**
* Determine whether or not the player remains online.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter {
private final Channel channel;
public boolean pendingConfigurationSwitch = false;
private SocketAddress remoteAddress;
private @Nullable SocketAddress haProxyAddress;
private StateRegistry state;
private Map<StateRegistry, MinecraftSessionHandler> sessionHandlers;
private @Nullable MinecraftSessionHandler activeSessionHandler;
Expand Down Expand Up @@ -158,6 +159,10 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
} else if (msg instanceof HAProxyMessage proxyMessage) {
this.remoteAddress = new InetSocketAddress(proxyMessage.sourceAddress(),
proxyMessage.sourcePort());

final var proxyTransportAddress = (InetSocketAddress) ctx.channel().remoteAddress();
this.haProxyAddress = new InetSocketAddress(proxyTransportAddress.getAddress().getHostAddress(),
proxyTransportAddress.getPort());
} else if (msg instanceof ByteBuf) {
activeSessionHandler.handleUnknown((ByteBuf) msg);
}
Expand Down Expand Up @@ -327,6 +332,10 @@ public SocketAddress getRemoteAddress() {
return remoteAddress;
}

public @Nullable SocketAddress getHaProxyAddress() {
return haProxyAddress;
}

public StateRegistry getState() {
return state;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ public Optional<String> getRawVirtualHost() {
return Optional.ofNullable(rawVirtualHost);
}

@Override
public Optional<InetSocketAddress> getHaProxyAddress() {
return Optional.ofNullable((InetSocketAddress) connection.getHaProxyAddress());
}

void setPermissionFunction(PermissionFunction permissionFunction) {
this.permissionFunction = permissionFunction;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ public Optional<String> getRawVirtualHost() {
return getVirtualHost().map(InetSocketAddress::getHostName);
}

@Override
public Optional<InetSocketAddress> getHaProxyAddress() {
return Optional.ofNullable((InetSocketAddress) connection.getHaProxyAddress());
}

@Override
public boolean isActive() {
return !connection.isClosed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public Optional<String> getRawVirtualHost() {
return Optional.of(handshake.getServerAddress());
}

@Override
public Optional<InetSocketAddress> getHaProxyAddress() {
return Optional.ofNullable((InetSocketAddress) connection.getHaProxyAddress());
}

@Override
public boolean isActive() {
return connection.getChannel().isActive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public Optional<String> getRawVirtualHost() {
return delegate.getRawVirtualHost();
}

@Override
public Optional<InetSocketAddress> getHaProxyAddress() {
return delegate.getHaProxyAddress();
}

@Override
public boolean isActive() {
return delegate.isActive();
Expand Down