Implement System properties to set the udp port and address #3597 (#3689)

* System property stuff

* Add geyserUdpPort/Address system properties as overrides for pluginUdpPort/Address

* Fix formatting for if-else statements

---------

Co-authored-by: Camotoy <20743703+Camotoy@users.noreply.github.com>
This commit is contained in:
Julian Vennen 2023-04-23 03:33:23 +02:00 committed by GitHub
parent cb440b65ad
commit f39e689b83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 160 additions and 129 deletions

View file

@ -158,4 +158,15 @@ public interface GeyserBootstrap {
}
return stream;
}
/**
* @return the bind address being used by the Java server.
*/
@Nonnull
String getServerBindAddress();
/**
* @return the listening port being used by the Java server. -1 if can't be found
*/
int getServerPort();
}

View file

@ -261,16 +261,68 @@ public class GeyserImpl implements GeyserApi {
ResourcePack.loadPacks();
if (platformType != PlatformType.STANDALONE && config.getRemote().address().equals("auto")) {
// Set the remote address to localhost since that is where we are always connecting
try {
config.getRemote().setAddress(InetAddress.getLocalHost().getHostAddress());
} catch (UnknownHostException ex) {
logger.debug("Unknown host when trying to find localhost.");
if (config.isDebugMode()) {
ex.printStackTrace();
String geyserUdpPort = System.getProperty("geyserUdpPort", "");
String pluginUdpPort = geyserUdpPort.isEmpty() ? System.getProperty("pluginUdpPort", "") : geyserUdpPort;
if ("-1".equals(pluginUdpPort)) {
throw new UnsupportedOperationException("This hosting/service provider does not support applications running on the UDP port");
}
boolean portPropertyApplied = false;
String pluginUdpAddress = System.getProperty("geyserUdpAddress", System.getProperty("pluginUdpAddress", ""));
if (platformType != PlatformType.STANDALONE) {
int javaPort = bootstrap.getServerPort();
if (config.getRemote().address().equals("auto")) {
config.setAutoconfiguredRemote(true);
String serverAddress = bootstrap.getServerBindAddress();
if (!serverAddress.isEmpty() && !"0.0.0.0".equals(serverAddress)) {
config.getRemote().setAddress(serverAddress);
} else {
// Set the remote address to localhost since that is where we are always connecting
try {
config.getRemote().setAddress(InetAddress.getLocalHost().getHostAddress());
} catch (UnknownHostException ex) {
logger.debug("Unknown host when trying to find localhost.");
if (config.isDebugMode()) {
ex.printStackTrace();
}
config.getRemote().setAddress(InetAddress.getLoopbackAddress().getHostAddress());
}
}
if (javaPort != -1) {
config.getRemote().setPort(javaPort);
}
}
boolean forceMatchServerPort = "server".equals(pluginUdpPort);
if ((config.getBedrock().isCloneRemotePort() || forceMatchServerPort) && javaPort != -1) {
config.getBedrock().setPort(javaPort);
if (forceMatchServerPort) {
if (geyserUdpPort.isEmpty()) {
logger.info("Port set from system generic property to match Java server.");
} else {
logger.info("Port set from system property to match Java server.");
}
portPropertyApplied = true;
}
}
if ("server".equals(pluginUdpAddress)) {
String address = bootstrap.getServerBindAddress();
if (!address.isEmpty()) {
config.getBedrock().setAddress(address);
}
} else if (!pluginUdpAddress.isEmpty()) {
config.getBedrock().setAddress(pluginUdpAddress);
}
if (!portPropertyApplied && !pluginUdpPort.isEmpty()) {
int port = Integer.parseInt(pluginUdpPort);
config.getBedrock().setPort(port);
if (geyserUdpPort.isEmpty()) {
logger.info("Port set from generic system property: " + port);
} else {
logger.info("Port set from system property: " + port);
}
config.getRemote().setAddress(InetAddress.getLoopbackAddress().getHostAddress());
}
}
String remoteAddress = config.getRemote().address();

View file

@ -38,6 +38,10 @@ import java.util.List;
import java.util.Map;
public interface GeyserConfiguration {
/**
* If the config was originally 'auto' before the values changed
*/
void setAutoconfiguredRemote(boolean autoconfiguredRemote);
// Modify this when you introduce breaking changes into the config
int CURRENT_CONFIG_VERSION = 4;
@ -116,6 +120,9 @@ public interface GeyserConfiguration {
int getPendingAuthenticationTimeout();
interface IBedrockConfiguration extends BedrockListener {
void setAddress(String address);
void setPort(int port);
boolean isCloneRemotePort();

View file

@ -53,9 +53,6 @@ import java.util.stream.Collectors;
@SuppressWarnings("FieldMayBeFinal") // Jackson requires that the fields are not final
public abstract class GeyserJacksonConfiguration implements GeyserConfiguration {
/**
* If the config was originally 'auto' before the values changed
*/
@Setter
private boolean autoconfiguredRemote = false;
@ -163,6 +160,7 @@ public abstract class GeyserJacksonConfiguration implements GeyserConfiguration
public static class BedrockConfiguration implements IBedrockConfiguration {
@AsteriskSerializer.Asterisk(isIp = true)
@JsonProperty("address")
@Setter
private String address = "0.0.0.0";
@Override