diff --git a/Mage.Common/src/mage/remote/Connection.java b/Mage.Common/src/mage/remote/Connection.java index 43b253a47b..564df9e248 100644 --- a/Mage.Common/src/mage/remote/Connection.java +++ b/Mage.Common/src/mage/remote/Connection.java @@ -28,6 +28,15 @@ package mage.remote; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.InterfaceAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; +import java.util.logging.Level; +import java.util.logging.Logger; + /** * * @author BetaSteward_at_googlemail.com @@ -64,6 +73,13 @@ public class Connection { } public String getURI() { + if (host.equals("localhost")) { + try { + return "bisocket://" + getLocalAddress().getHostAddress() + ":" + port; + } catch (SocketException ex) { + // just use localhost if can't find local ip + } + } return "bisocket://" + host + ":" + port; } @@ -160,5 +176,21 @@ public class Connection { public void setProxyPassword(String proxyPassword) { this.proxyPassword = proxyPassword; } + + public static InetAddress getLocalAddress() throws SocketException { + for (Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) { + NetworkInterface iface = interfaces.nextElement( ); + if (iface.isLoopback()) + continue; + for (InterfaceAddress addr: iface.getInterfaceAddresses()) + { + InetAddress iaddr = addr.getAddress(); + if (iaddr instanceof Inet4Address) { + return iaddr; + } + } + } + return null; + } } diff --git a/Mage.Common/src/mage/remote/Session.java b/Mage.Common/src/mage/remote/Session.java index 288f745c78..c93eb9ba5e 100644 --- a/Mage.Common/src/mage/remote/Session.java +++ b/Mage.Common/src/mage/remote/Session.java @@ -101,7 +101,6 @@ public class Session { public boolean connect() { sessionState = SessionState.CONNECTING; try { -// System.setSecurityManager(null); // System.setProperty("http.nonProxyHosts", "code.google.com"); // System.setProperty("socksNonProxyHosts", "code.google.com"); // @@ -135,7 +134,7 @@ public class Session { this.sessionId = callbackClient.getSessionId(); boolean registerResult = false; - if (connection.getPassword().isEmpty()) + if (connection.getPassword() == null) registerResult = server.registerClient(connection.getUsername(), sessionId, client.getVersion()); else registerResult = server.registerAdmin(connection.getPassword(), sessionId, client.getVersion()); diff --git a/Mage.Server/src/main/java/mage/server/Main.java b/Mage.Server/src/main/java/mage/server/Main.java index 6a2f98563f..5994a39f4f 100644 --- a/Mage.Server/src/main/java/mage/server/Main.java +++ b/Mage.Server/src/main/java/mage/server/Main.java @@ -31,17 +31,13 @@ package mage.server; import mage.server.util.PluginClassLoader; import java.io.File; import java.io.FilenameFilter; -import java.net.Inet4Address; import java.net.InetAddress; -import java.net.InterfaceAddress; -import java.net.NetworkInterface; -import java.net.SocketException; -import java.util.Enumeration; import java.util.Map; import javax.management.MBeanServer; import mage.game.match.MatchType; import mage.game.tournament.TournamentType; import mage.interfaces.MageServer; +import mage.remote.Connection; import mage.server.game.DeckValidatorFactory; import mage.server.game.GameFactory; import mage.server.game.PlayerFactory; @@ -107,10 +103,12 @@ public class Main { adminPassword = arg.replace(adminPasswordArg, ""); } } - String host = getServerAddress(); + String host = config.getServerAddress(); int port = config.getPort(); - String locatorURI = "bisocket://" + host + ":" + port; try { + if (host.equals("localhost")) + host = Connection.getLocalAddress().getHostAddress(); + String locatorURI = "bisocket://" + host + ":" + port; InvokerLocator serverLocator = new InvokerLocator(locatorURI); server = new MageTransporterServer(serverLocator, new MageServerImpl(adminPassword, testMode), MageServer.class.getName(), new MageServerInvocationHandler()); server.start(); @@ -178,32 +176,6 @@ public class Main { } } - - private static String getServerAddress() { - try { - String foundIP = ""; - for (Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) { - NetworkInterface iface = interfaces.nextElement( ); - if (iface.isLoopback()) - continue; - for (InterfaceAddress addr: iface.getInterfaceAddresses()) - { - InetAddress iaddr = addr.getAddress(); - if (iaddr instanceof Inet4Address) { - foundIP = iaddr.getHostAddress(); - break; - } - } - if (foundIP.length() > 0) - break; - } - if (foundIP.length() > 0) - return foundIP; - } catch (SocketException ex) { - logger.warn("Could not get server address: ", ex); - } - return null; - } private static Class loadPlugin(Plugin plugin) { try {