From 43090a9bd11c2758ef59f1b10cb4ca6586399d7e Mon Sep 17 00:00:00 2001 From: MagicBot Date: Sun, 23 Jul 2023 12:58:40 -0400 Subject: [PATCH] Support for the widest array of network configurations. Auto-configure as best we can. --- src/engine/gameManager/ConfigManager.java | 2 +- .../msg/login/GameServerIPResponseMsg.java | 2 +- src/engine/server/login/LoginServer.java | 16 ++++++++++++++ src/engine/server/world/WorldServer.java | 21 ++++++++++++++++--- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/engine/gameManager/ConfigManager.java b/src/engine/gameManager/ConfigManager.java index 75fffdf4..6cf0eeda 100644 --- a/src/engine/gameManager/ConfigManager.java +++ b/src/engine/gameManager/ConfigManager.java @@ -25,7 +25,7 @@ import java.util.regex.Pattern; public enum ConfigManager { MB_BIND_ADDR, - + MB_EXTERNAL_ADDR, // Database connection config MB_DATABASE_ADDRESS, diff --git a/src/engine/net/client/msg/login/GameServerIPResponseMsg.java b/src/engine/net/client/msg/login/GameServerIPResponseMsg.java index b14acdf0..fe258fc9 100644 --- a/src/engine/net/client/msg/login/GameServerIPResponseMsg.java +++ b/src/engine/net/client/msg/login/GameServerIPResponseMsg.java @@ -36,7 +36,7 @@ public class GameServerIPResponseMsg extends ClientNetMsg { */ public GameServerIPResponseMsg() { super(Protocol.GAMESERVERIPRESPONSE); - this.ip = ConfigManager.MB_BIND_ADDR.getValue(); + this.ip = ConfigManager.MB_EXTERNAL_ADDR.getValue(); this.port = Integer.parseInt(ConfigManager.MB_WORLD_PORT.getValue()); } diff --git a/src/engine/server/login/LoginServer.java b/src/engine/server/login/LoginServer.java index 06e8f71c..3b97f412 100644 --- a/src/engine/server/login/LoginServer.java +++ b/src/engine/server/login/LoginServer.java @@ -34,6 +34,7 @@ import org.pmw.tinylog.writers.RollingFileWriter; import java.io.*; import java.net.DatagramSocket; import java.net.InetAddress; +import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; import java.sql.Connection; @@ -335,6 +336,18 @@ public class LoginServer { String name = ConfigManager.MB_WORLD_NAME.getValue(); + if (ConfigManager.MB_EXTERNAL_ADDR.getValue().equals("0.0.0.0")) { + + // Autoconfigure External address for use in worldserver response + + Logger.info("AUTOCONFIG EXTERNAL IP ADDRESS"); + URL whatismyip = new URL("http://checkip.amazonaws.com"); + + BufferedReader in = new BufferedReader(new InputStreamReader( + whatismyip.openStream())); + ConfigManager.MB_EXTERNAL_ADDR.setValue(in.readLine()); + } + if (ConfigManager.MB_BIND_ADDR.getValue().equals("0.0.0.0")) { try (final DatagramSocket socket = new DatagramSocket()) { @@ -344,6 +357,9 @@ public class LoginServer { } + Logger.info("External address: " + ConfigManager.MB_EXTERNAL_ADDR.getValue() + ":" + ConfigManager.MB_WORLD_PORT.getValue()); + Logger.info("Internal address: " + ConfigManager.MB_BIND_ADDR.getValue() + ":" + ConfigManager.MB_LOGIN_PORT.getValue()); + InetAddress addy = InetAddress.getByName(ConfigManager.MB_BIND_ADDR.getValue()); int port = Integer.parseInt(ConfigManager.MB_LOGIN_PORT.getValue()); diff --git a/src/engine/server/world/WorldServer.java b/src/engine/server/world/WorldServer.java index 257a7f78..48c2edfd 100644 --- a/src/engine/server/world/WorldServer.java +++ b/src/engine/server/world/WorldServer.java @@ -51,11 +51,10 @@ import org.pmw.tinylog.labelers.TimestampLabeler; import org.pmw.tinylog.policies.StartupPolicy; import org.pmw.tinylog.writers.RollingFileWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; +import java.io.*; import java.net.DatagramSocket; import java.net.InetAddress; +import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; import java.time.LocalDateTime; @@ -249,6 +248,19 @@ public class WorldServer { String name = ConfigManager.MB_WORLD_NAME.getValue(); + if (ConfigManager.MB_EXTERNAL_ADDR.getValue().equals("0.0.0.0")) { + + // Autoconfigure External IP address. Only used in loginserver but useful + // here for bootstrap display + + Logger.info("AUTOCONFIG EXTERNAL IP ADDRESS"); + URL whatismyip = new URL("http://checkip.amazonaws.com"); + + BufferedReader in = new BufferedReader(new InputStreamReader( + whatismyip.openStream())); + ConfigManager.MB_EXTERNAL_ADDR.setValue(in.readLine()); + } + if (ConfigManager.MB_BIND_ADDR.getValue().equals("0.0.0.0")) { try (final DatagramSocket socket = new DatagramSocket()) { @@ -258,6 +270,9 @@ public class WorldServer { } + Logger.info("External address: " + ConfigManager.MB_EXTERNAL_ADDR.getValue() + ":" + ConfigManager.MB_WORLD_PORT.getValue()); + Logger.info("Internal address: " + ConfigManager.MB_BIND_ADDR.getValue() + ":" + ConfigManager.MB_LOGIN_PORT.getValue()); + InetAddress addy = InetAddress.getByName(ConfigManager.MB_BIND_ADDR.getValue()); int port = Integer.parseInt(ConfigManager.MB_WORLD_PORT.getValue());