mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 03:38:01 +00:00
Show external IP when hosting, if possible
Fixes: core-developers/forge#396 Signed-off-by: Jamin W. Collins <jamin.collins@gmail.com>
This commit is contained in:
@@ -106,10 +106,24 @@ public class NetConnectUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void copyHostedServerUrl() {
|
public static void copyHostedServerUrl() {
|
||||||
String hostname = FServerManager.getInstance().getLocalAddress();
|
String internalAddress = FServerManager.getInstance().getLocalAddress();
|
||||||
String url = hostname + ":" + ForgeProfileProperties.getServerPort();
|
String externalAddress = FServerManager.getInstance().getExternalAddress();
|
||||||
GuiBase.getInterface().copyToClipboard(url);
|
String internalUrl = internalAddress + ":" + ForgeProfileProperties.getServerPort();
|
||||||
SOptionPane.showMessageDialog("Share the following URL with anyone who wishes to join your server. It has been copied to your clipboard for convenience.\n\n" + url, "Server URL", SOptionPane.INFORMATION_ICON);
|
String externalUrl = null;
|
||||||
|
if (externalAddress != null) {
|
||||||
|
externalUrl = externalAddress + ":" + ForgeProfileProperties.getServerPort();
|
||||||
|
GuiBase.getInterface().copyToClipboard(externalUrl);
|
||||||
|
} else {
|
||||||
|
GuiBase.getInterface().copyToClipboard(internalAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
String message = "Share the following URL with anyone who wishes to join your server. It has been copied to your clipboard for convenience.\n\n";
|
||||||
|
if (externalUrl != null) {
|
||||||
|
message += externalUrl + "\n\nFor internal games, use the following URL: " + internalUrl;
|
||||||
|
} else {
|
||||||
|
message = "Forge was unable to determine your external IP!\n\n" + message + internalUrl;
|
||||||
|
}
|
||||||
|
SOptionPane.showMessageDialog(message, "Server URL", SOptionPane.INFORMATION_ICON);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ChatMessage join(final String url, final IOnlineLobby onlineLobby, final IOnlineChatInterface chatInterface) {
|
public static ChatMessage join(final String url, final IOnlineLobby onlineLobby, final IOnlineChatInterface chatInterface) {
|
||||||
|
|||||||
@@ -30,13 +30,10 @@ import io.netty.handler.codec.serialization.ObjectEncoder;
|
|||||||
import io.netty.handler.logging.LogLevel;
|
import io.netty.handler.logging.LogLevel;
|
||||||
import io.netty.handler.logging.LoggingHandler;
|
import io.netty.handler.logging.LoggingHandler;
|
||||||
|
|
||||||
import java.net.DatagramSocket;
|
import java.io.BufferedReader;
|
||||||
import java.net.Inet4Address;
|
import java.io.IOException;
|
||||||
import java.net.Inet6Address;
|
import java.io.InputStreamReader;
|
||||||
import java.net.InetAddress;
|
import java.net.*;
|
||||||
import java.net.NetworkInterface;
|
|
||||||
import java.net.SocketException;
|
|
||||||
import java.net.UnknownHostException;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
@@ -253,6 +250,28 @@ public final class FServerManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getExternalAddress() {
|
||||||
|
BufferedReader in = null;
|
||||||
|
try {
|
||||||
|
URL whatismyip = new URL("http://checkip.amazonaws.com");
|
||||||
|
in = new BufferedReader(new InputStreamReader(
|
||||||
|
whatismyip.openStream()));
|
||||||
|
String ip = in.readLine();
|
||||||
|
return ip;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (in != null) {
|
||||||
|
try {
|
||||||
|
in.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private void mapNatPort(final int port) {
|
private void mapNatPort(final int port) {
|
||||||
final String localAddress = getLocalAddress();
|
final String localAddress = getLocalAddress();
|
||||||
final PortMapping portMapping = new PortMapping(port, localAddress, PortMapping.Protocol.TCP, "Forge");
|
final PortMapping portMapping = new PortMapping(port, localAddress, PortMapping.Protocol.TCP, "Forge");
|
||||||
|
|||||||
Reference in New Issue
Block a user