3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-12-23 23:00:35 +01:00

Add Query event

Dieser Commit ist enthalten in:
Mark Vainomaa 2018-10-23 22:04:15 +03:00
Ursprung 72283af867
Commit ea43b8ff60
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 1B3F9523B542D315
6 geänderte Dateien mit 524 neuen und 28 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,83 @@
package com.velocitypowered.api.event.query;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.proxy.server.QueryResponse;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.net.InetAddress;
/**
* This event is fired if proxy is getting queried over GS4 Query protocol
*/
public final class ProxyQueryEvent {
private final QueryType queryType;
private final InetAddress querierAddress;
private QueryResponse response;
public ProxyQueryEvent(QueryType queryType, InetAddress querierAddress, QueryResponse response) {
this.queryType = Preconditions.checkNotNull(queryType, "queryType");
this.querierAddress = Preconditions.checkNotNull(querierAddress, "querierAddress");
this.response = Preconditions.checkNotNull(response, "response");
}
/**
* Get query type
* @return query type
*/
@NonNull
public QueryType getQueryType() {
return queryType;
}
/**
* Get querier address
* @return querier address
*/
@NonNull
public InetAddress getQuerierAddress() {
return querierAddress;
}
/**
* Get query response
* @return query response
*/
@NonNull
public QueryResponse getResponse() {
return response;
}
/**
* Set query response
* @param response query response
*/
public void setResponse(@NonNull QueryResponse response) {
this.response = Preconditions.checkNotNull(response, "response");
}
@Override
public String toString() {
return "ProxyQueryEvent{" +
"queryType=" + queryType +
", querierAddress=" + querierAddress +
", response=" + response +
'}';
}
/**
* The type of query
*/
public enum QueryType {
/**
* Basic query asks only a subset of information, such as hostname, game type (hardcoded to <pre>MINECRAFT</pre>), map,
* current players, max players, proxy port and proxy hostname
*/
BASIC,
/**
* Full query asks pretty much everything present on this event (only hardcoded values cannot be modified here).
*/
FULL
;
}
}

Datei anzeigen

@ -0,0 +1,4 @@
/**
* Provides events for handling GS4 queries.
*/
package com.velocitypowered.api.event.query;

Datei anzeigen

@ -29,6 +29,13 @@ public interface ProxyConfig {
*/
String getQueryMap();
/**
* Whether GameSpy 4 queries should show plugins installed on
* Velocity by default
* @return show plugins in query
*/
boolean shouldQueryShowPlugins();
/**
* Get the MOTD component shown in the tab list
* @return the motd component

Datei anzeigen

@ -0,0 +1,318 @@
package com.velocitypowered.api.proxy.server;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.velocitypowered.api.proxy.config.ProxyConfig;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
* GS4 query response. This class is immutable.
*/
public final class QueryResponse {
private final String hostname;
private final String gameVersion;
private final String map;
private final int currentPlayers;
private final int maxPlayers;
private final String proxyHost;
private final int proxyPort;
private final Collection<String> players;
private final String proxyVersion;
private final Collection<PluginInformation> plugins;
private QueryResponse(String hostname, String gameVersion, String map, int currentPlayers, int maxPlayers, String proxyHost, int proxyPort, Collection<String> players, String proxyVersion, Collection<PluginInformation> plugins) {
this.hostname = hostname;
this.gameVersion = gameVersion;
this.map = map;
this.currentPlayers = currentPlayers;
this.maxPlayers = maxPlayers;
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
this.players = players;
this.proxyVersion = proxyVersion;
this.plugins = plugins;
}
/**
* Get hostname which will be used to reply to the query. By default it is {@link ProxyConfig#getMotdComponent()} in plain text without colour codes.
* @return hostname
*/
@NonNull
public String getHostname() {
return hostname;
}
/**
* Get game version which will be used to reply to the query. By default supported Minecraft versions range is sent.
* @return game version
*/
@NonNull
public String getGameVersion() {
return gameVersion;
}
/**
* Get map name which will be used to reply to the query. By default {@link ProxyConfig#getQueryMap()} is sent.
* @return map name
*/
@NonNull
public String getMap() {
return map;
}
/**
* Get current online player count which will be used to reply to the query.
* @return online player count
*/
public int getCurrentPlayers() {
return currentPlayers;
}
/**
* Get max player count which will be used to reply to the query.
* @return max player count
*/
public int getMaxPlayers() {
return maxPlayers;
}
/**
* Get proxy (public facing) hostname
* @return proxy hostname
*/
@NonNull
public String getProxyHost() {
return proxyHost;
}
/**
* Get proxy (public facing) port
* @return proxy port
*/
public int getProxyPort() {
return proxyPort;
}
/**
* Get collection of players which will be used to reply to the query.
* @return collection of players
*/
@NonNull
public Collection<String> getPlayers() {
return players;
}
/**
* Get server software (name and version) which will be used to reply to the query.
* @return server software
*/
@NonNull
public String getProxyVersion() {
return proxyVersion;
}
/**
* Get list of plugins which will be used to reply to the query.
* @return collection of plugins
*/
@NonNull
public Collection<PluginInformation> getPlugins() {
return plugins;
}
/**
* Creates a new {@link Builder} instance from data represented by this response
* @return {@link QueryResponse} builder
*/
@NonNull
public Builder toBuilder() {
return QueryResponse.builder()
.hostname(getHostname())
.gameVersion(getGameVersion())
.map(getMap())
.currentPlayers(getCurrentPlayers())
.maxPlayers(getMaxPlayers())
.proxyHost(getProxyHost())
.proxyPort(getProxyPort())
.players(getPlayers())
.proxyVersion(getProxyVersion())
.plugins(getPlugins());
}
/**
* Creates a new {@link Builder} instance
* @return {@link QueryResponse} builder
*/
@NonNull
public static Builder builder() {
return new Builder();
}
/**
* A builder for {@link QueryResponse} objects.
*/
public static final class Builder {
private String hostname;
private String gameVersion;
private String map;
private int currentPlayers;
private int maxPlayers;
private String proxyHost;
private int proxyPort;
private Collection<String> players = new ArrayList<>();
private String proxyVersion;
private List<PluginInformation> plugins = new ArrayList<>();
private Builder() {}
@NonNull
public Builder hostname(@NonNull String hostname) {
this.hostname = Preconditions.checkNotNull(hostname, "hostname");
return this;
}
@NonNull
public Builder gameVersion(@NonNull String gameVersion) {
this.gameVersion = Preconditions.checkNotNull(gameVersion, "gameVersion");
return this;
}
@NonNull
public Builder map(@NonNull String map) {
this.map = Preconditions.checkNotNull(map, "map");
return this;
}
@NonNull
public Builder currentPlayers(int currentPlayers) {
Preconditions.checkArgument(currentPlayers >= 0, "currentPlayers cannot be negative");
this.currentPlayers = currentPlayers;
return this;
}
@NonNull
public Builder maxPlayers(int maxPlayers) {
Preconditions.checkArgument(maxPlayers >= 0, "maxPlayers cannot be negative");
this.maxPlayers = maxPlayers;
return this;
}
@NonNull
public Builder proxyHost(@NonNull String proxyHost) {
this.proxyHost = Preconditions.checkNotNull(proxyHost, "proxyHost");
return this;
}
@NonNull
public Builder proxyPort(int proxyPort) {
Preconditions.checkArgument(proxyPort >= 1 && proxyPort <= 65535, "proxyPort must be between 1-65535");
this.proxyPort = proxyPort;
return this;
}
@NonNull
public Builder players(@NonNull Collection<String> players) {
this.players.addAll(Preconditions.checkNotNull(players, "players"));
return this;
}
@NonNull
public Builder players(@NonNull String... players) {
this.players.addAll(Arrays.asList(Preconditions.checkNotNull(players, "players")));
return this;
}
@NonNull
public Builder clearPlayers() {
this.players.clear();
return this;
}
@NonNull
public Builder proxyVersion(@NonNull String proxyVersion) {
this.proxyVersion = Preconditions.checkNotNull(proxyVersion, "proxyVersion");
return this;
}
@NonNull
public Builder plugins(@NonNull Collection<PluginInformation> plugins) {
this.plugins.addAll(Preconditions.checkNotNull(plugins, "plugins"));
return this;
}
@NonNull
public Builder plugins(@NonNull PluginInformation... plugins) {
this.plugins.addAll(Arrays.asList(Preconditions.checkNotNull(plugins, "plugins")));
return this;
}
@NonNull
public Builder clearPlugins() {
this.plugins.clear();
return this;
}
/**
* Builds new {@link QueryResponse} with supplied data
* @return response
*/
@NonNull
public QueryResponse build() {
return new QueryResponse(
hostname,
gameVersion,
map,
currentPlayers,
maxPlayers,
proxyHost,
proxyPort,
ImmutableList.copyOf(players),
proxyVersion,
ImmutableList.copyOf(plugins)
);
}
}
/**
* Plugin information
*/
public static class PluginInformation {
private String name;
private String version;
public PluginInformation(@NonNull String name, @Nullable String version) {
this.name = name;
this.version = version;
}
@NonNull
public String getName() {
return name;
}
public void setName(@NonNull String name) {
this.name = name;
}
public void setVersion(@Nullable String version) {
this.version = version;
}
@Nullable
public String getVersion() {
return version;
}
@NonNull
public static PluginInformation of(@NonNull String name, @Nullable String version) {
return new PluginInformation(name, version);
}
}
}

Datei anzeigen

@ -213,6 +213,11 @@ public class VelocityConfiguration extends AnnotatedConfig implements ProxyConfi
return query.getQueryMap();
}
@Override
public boolean shouldQueryShowPlugins() {
return query.shouldQueryShowPlugins();
}
public String getMotd() {
return motd;
}
@ -504,6 +509,10 @@ public class VelocityConfiguration extends AnnotatedConfig implements ProxyConfi
@ConfigKey("map")
private String queryMap = "Velocity";
@Comment("Whether plugins should be shown in query response by default or not")
@ConfigKey("show-plugins")
private boolean showPlugins = false;
private Query() {
}
@ -533,13 +542,18 @@ public class VelocityConfiguration extends AnnotatedConfig implements ProxyConfi
return queryMap;
}
public boolean shouldQueryShowPlugins() {
return showPlugins;
}
@Override
public String toString() {
return "Query{"
+ "queryEnabled=" + queryEnabled
+ ", queryPort=" + queryPort
+ ", queryMap=" + queryMap
+ '}';
return "Query{" +
"queryEnabled=" + queryEnabled +
", queryPort=" + queryPort +
", queryMap='" + queryMap + '\'' +
", showPlugins=" + showPlugins +
'}';
}
}
}

Datei anzeigen

@ -3,7 +3,10 @@ package com.velocitypowered.proxy.protocol.netty;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableSet;
import com.velocitypowered.api.event.query.ProxyQueryEvent;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.QueryResponse;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.protocol.ProtocolConstants;
import io.netty.buffer.ByteBuf;
@ -17,9 +20,16 @@ import org.apache.logging.log4j.Logger;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static com.velocitypowered.api.event.query.ProxyQueryEvent.QueryType.BASIC;
import static com.velocitypowered.api.event.query.ProxyQueryEvent.QueryType.FULL;
public class GS4QueryHandler extends SimpleChannelInboundHandler<DatagramPacket> {
private static final Logger logger = LogManager.getLogger(GS4QueryHandler.class);
@ -46,6 +56,8 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
.expireAfterWrite(30, TimeUnit.SECONDS)
.build();
private volatile List<QueryResponse.PluginInformation> pluginInformationList = null;
private final VelocityServer server;
public GS4QueryHandler(VelocityServer server) {
@ -81,6 +93,7 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
queryResponse.writeByte(QUERY_TYPE_HANDSHAKE);
queryResponse.writeInt(sessionId);
writeString(queryResponse, Integer.toString(challengeToken));
ctx.writeAndFlush(responsePacket);
break;
}
@ -97,28 +110,51 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
throw new IllegalStateException("Invalid query packet");
}
// Packet header
queryResponse.writeByte(QUERY_TYPE_STAT);
queryResponse.writeInt(sessionId);
// Build query response
QueryResponse response = QueryResponse.builder()
.hostname(ComponentSerializers.PLAIN.serialize(server.getConfiguration().getMotdComponent()))
.gameVersion(ProtocolConstants.SUPPORTED_GENERIC_VERSION_STRING)
.map(server.getConfiguration().getQueryMap())
.currentPlayers(server.getPlayerCount())
.maxPlayers(server.getConfiguration().getShowMaxPlayers())
.proxyPort(server.getConfiguration().getBind().getPort())
.proxyHost(server.getConfiguration().getBind().getHostString())
.players(server.getAllPlayers().stream().map(Player::getUsername).collect(Collectors.toList()))
.proxyVersion("Velocity")
.plugins(server.getConfiguration().shouldQueryShowPlugins() ? getRealPluginInformation() : Collections.emptyList())
.build();
// Start writing the response
ResponseWriter responseWriter = new ResponseWriter(queryResponse, queryMessage.readableBytes() == 0);
responseWriter.write("hostname", ComponentSerializers.PLAIN.serialize(server.getConfiguration().getMotdComponent()));
responseWriter.write("gametype", "SMP");
boolean isBasic = queryMessage.readableBytes() == 0;
responseWriter.write("game_id", "MINECRAFT");
responseWriter.write("version", ProtocolConstants.SUPPORTED_GENERIC_VERSION_STRING);
responseWriter.write("plugins", "");
// Call event and write response
server.getEventManager().fire(new ProxyQueryEvent(isBasic ? BASIC : FULL, senderAddress, response)).whenCompleteAsync((event, exc) -> {
// Packet header
queryResponse.writeByte(QUERY_TYPE_STAT);
queryResponse.writeInt(sessionId);
responseWriter.write("map", server.getConfiguration().getQueryMap());
responseWriter.write("numplayers", server.getPlayerCount());
responseWriter.write("maxplayers", server.getConfiguration().getShowMaxPlayers());
responseWriter.write("hostport", server.getConfiguration().getBind().getPort());
responseWriter.write("hostip", server.getConfiguration().getBind().getHostString());
// Start writing the response
ResponseWriter responseWriter = new ResponseWriter(queryResponse, isBasic);
responseWriter.write("hostname", event.getResponse().getHostname());
responseWriter.write("gametype", "SMP");
responseWriter.write("game_id", "MINECRAFT");
responseWriter.write("version", event.getResponse().getGameVersion());
responseWriter.writePlugins(event.getResponse().getProxyVersion(), event.getResponse().getPlugins());
responseWriter.write("map", event.getResponse().getMap());
responseWriter.write("numplayers", event.getResponse().getCurrentPlayers());
responseWriter.write("maxplayers", event.getResponse().getMaxPlayers());
responseWriter.write("hostport", event.getResponse().getProxyPort());
responseWriter.write("hostip", event.getResponse().getProxyHost());
if (!responseWriter.isBasic) {
responseWriter.writePlayers(event.getResponse().getPlayers());
}
// Send the response
ctx.writeAndFlush(responsePacket);
}, ctx.channel().eventLoop());
if (!responseWriter.isBasic) {
responseWriter.writePlayers(server.getAllPlayers());
}
break;
}
@ -126,9 +162,6 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
throw new IllegalStateException("Invalid query type: " + type);
}
}
// Send the response
ctx.writeAndFlush(responsePacket);
} catch (Exception e) {
logger.warn("Error while trying to handle a query packet from {}", msg.sender(), e);
// NB: Only need to explicitly release upon exception, writing the response out will decrement the reference
@ -142,6 +175,22 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
buf.writeByte(0x00);
}
private List<QueryResponse.PluginInformation> getRealPluginInformation() {
// Effective Java, Third Edition; Item 83: Use lazy initialization judiciously
List<QueryResponse.PluginInformation> res = pluginInformationList;
if (res == null) {
synchronized (this) {
if (pluginInformationList == null) {
pluginInformationList = res = server.getPluginManager().getPlugins().stream()
.map(PluginContainer::getDescription)
.map(desc -> QueryResponse.PluginInformation.of(desc.getName().orElse(desc.getId()), desc.getVersion().orElse(null)))
.collect(Collectors.toList());
}
}
}
return res;
}
private static class ResponseWriter {
private final ByteBuf buf;
private final boolean isBasic;
@ -180,7 +229,7 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
// Ends packet k/v body writing and writes stat player list to
// the packet if this writer is initialized for full stat response
void writePlayers(Collection<Player> players) {
void writePlayers(Collection<String> players) {
if (isBasic) {
return;
}
@ -189,8 +238,29 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
buf.writeByte(0x00);
buf.writeBytes(QUERY_RESPONSE_FULL_PADDING2);
players.forEach(player -> writeString(buf, player.getUsername()));
players.forEach(player -> writeString(buf, player));
buf.writeByte(0x00);
}
void writePlugins(String serverVersion, Collection<QueryResponse.PluginInformation> plugins) {
if (isBasic)
return;
StringBuilder pluginsString = new StringBuilder();
pluginsString.append(serverVersion).append(':').append(' ');
Iterator<QueryResponse.PluginInformation> iterator = plugins.iterator();
while (iterator.hasNext()) {
QueryResponse.PluginInformation info = iterator.next();
pluginsString.append(info.getName());
if (info.getVersion() != null) {
pluginsString.append(' ').append(info.getVersion());
}
if (iterator.hasNext()) {
pluginsString.append(';').append(' ');
}
}
writeString(buf, pluginsString.toString());
}
}
}