Skip to content

Commit

Permalink
Merge pull request #4208 from PeterPeet/EnableIPv6inConnectionInfoDialog
Browse files Browse the repository at this point in the history
#4207 #4206 Changed the ConnectionInfoDialog to show active interface…
  • Loading branch information
cwisniew authored Jul 25, 2023
2 parents 1b49106 + d3b06c7 commit a1ad295
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@

import java.awt.GridLayout;
import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
Expand All @@ -44,6 +49,42 @@ public class ConnectionInfoDialog extends JDialog {

private static final Logger log = LogManager.getLogger(ConnectionInfoDialog.class);

/**
* Get the IP-Address of the active NetworkInterface
*
* @param v6 Should the IPv6 Address be returnes (true) or the IPv4 Address (false)
*/
private static String getIPAddress(boolean v6) throws SocketException {
Enumeration<NetworkInterface> netInts = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netInt : Collections.list(netInts)) {
if (netInt.isUp() && !netInt.isLoopback()) {
for (InetAddress inetAddress : Collections.list(netInt.getInetAddresses())) {

if (inetAddress.isLoopbackAddress()
|| inetAddress.isLinkLocalAddress()
|| inetAddress.isMulticastAddress()) {
continue;
}

try {
InetAddress rptools = InetAddress.getByName("www.rptools.net");
} catch (UnknownHostException e) {
continue;
}

if (v6 && inetAddress instanceof Inet6Address) {
return inetAddress.getHostAddress();
}

if (!v6 && inetAddress instanceof InetAddress) {
return inetAddress.getHostAddress();
}
}
}
}
return null;
}

/**
* This is the default constructor
*
Expand All @@ -57,31 +98,36 @@ public ConnectionInfoDialog(MapToolServer server) {
AbeillePanel panel = new AbeillePanel(new ConnectionInfoDialogView().getRootComponent());

JTextField nameLabel = panel.getTextField("name");
JTextField localAddressLabel = panel.getTextField("localAddress");
JTextField localv4AddressLabel = panel.getTextField("localv4Address");
JTextField localv6AddressLabel = panel.getTextField("localv6Address");
JTextField portLabel = panel.getTextField("port");
externalAddressLabel = panel.getTextField("externalAddress");

String name = server.getConfig().getServerName();
if (name == null) {
name = "---";
}
String localAddress = "Unknown";

String localv4Address = "Unknown";
try {
InetAddress rptools = InetAddress.getByName("www.rptools.net");
try {
InetAddress localAddy = InetAddress.getLocalHost();
localAddress = localAddy.getHostAddress();
} catch (IOException e) { // Socket|UnknownHost
log.warn("Can't resolve 'www.rptools.net' or our own IP address!?", e);
}
} catch (UnknownHostException e) {
log.warn("Can't resolve 'www.rptools.net' or our own IP address!?", e);
localv4Address = getIPAddress(false);
} catch (IOException e) { // UnknownHost | Socket
log.warn("Can't resolve our own IPv4 address!?", e);
}

String localv6Address = "Unknown";
try {
localv6Address = getIPAddress(true);
} catch (IOException e) { // UnknownHost | Socket
log.warn("Can't resolve our own IPv6 address!?", e);
}

String port =
MapTool.isPersonalServer() ? "---" : Integer.toString(server.getConfig().getPort());

nameLabel.setText(name);
localAddressLabel.setText(localAddress);
localv4AddressLabel.setText(localv4Address);
localv6AddressLabel.setText(localv6Address);
externalAddressLabel.setText(I18N.getText("ConnectionInfoDialog.discovering"));
portLabel.setText(port);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="net.rptools.maptool.client.ui.connectioninfodialog.ConnectionInfoDialogView">
<grid id="f0bbd" binding="mainPanel" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="f0bbd" binding="mainPanel" layout-manager="GridLayoutManager" row-count="7" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="5" left="5" bottom="5" right="5"/>
<constraints>
<xy x="10" y="10" width="207" height="211"/>
Expand All @@ -17,7 +17,7 @@
<text resource-bundle="net/rptools/maptool/language/i18n" key="Label.name"/>
</properties>
</component>
<component id="12eb5" class="javax.swing.JLabel">
<component id="12eb6" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
Expand All @@ -26,18 +26,27 @@
<text resource-bundle="net/rptools/maptool/language/i18n" key="ConnectionInfoDialog.address.local"/>
</properties>
</component>
<component id="3c360" class="javax.swing.JLabel">
<component id="12eb5" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font name="SansSerif" size="11" style="1"/>
<text resource-bundle="net/rptools/maptool/language/i18n" key="ConnectionInfoDialog.address.local"/>
</properties>
</component>
<component id="3c360" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font name="SansSerif" size="11" style="1"/>
<text resource-bundle="net/rptools/maptool/language/i18n" key="ConnectionInfoDialog.address.external"/>
</properties>
</component>
<component id="d0010" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font name="SansSerif" size="11" style="1"/>
Expand Down Expand Up @@ -67,23 +76,36 @@
<text value="name"/>
</properties>
</component>
<component id="4188" class="javax.swing.JTextField">
<component id="4189" class="javax.swing.JTextField">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<editable value="false"/>
<name value="localAddress"/>
<name value="localv4Address"/>
<opaque value="false"/>
<selectionEnd value="12"/>
<selectionStart value="12"/>
<text value="localAddress"/>
</properties>
</component>
<component id="96cea" class="javax.swing.JTextField">
<component id="4188" class="javax.swing.JTextField">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<editable value="false"/>
<name value="localv6Address"/>
<opaque value="false"/>
<selectionEnd value="12"/>
<selectionStart value="12"/>
<text value="localAddress"/>
</properties>
</component>
<component id="96cea" class="javax.swing.JTextField">
<constraints>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<editable value="false"/>
<name value="externalAddress"/>
Expand All @@ -95,7 +117,7 @@
</component>
<component id="a928c" class="javax.swing.JTextField">
<constraints>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<editable value="false"/>
Expand All @@ -108,7 +130,7 @@
</component>
<component id="74e0b" class="javax.swing.JButton">
<constraints>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
<grid row="6" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<actionCommand value="OK"/>
Expand All @@ -118,4 +140,4 @@
</component>
</children>
</grid>
</form>
</form>

0 comments on commit a1ad295

Please sign in to comment.