Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce number of interfaces scanned for UPnP gateways #4904

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/main/java/net/rptools/maptool/client/AppActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2162,8 +2162,9 @@ protected void executeAction() {
StartServerDialog dialog = new StartServerDialog();
dialog.showDialog();

if (!dialog.accepted()) // Results stored in Preferences.userRoot()
return;
if (!dialog.accepted()) { // Results stored in Preferences.userRoot()
return;
}

StartServerDialogPreferences serverProps =
new StartServerDialogPreferences(); // data retrieved from
Expand Down Expand Up @@ -2232,7 +2233,15 @@ protected void executeAction() {

// Use UPnP to open port in router
if (serverProps.getUseUPnP()) {
UPnPUtil.openPort(serverProps.getPort());
MapTool.getFrame()
.showFilledGlassPane(
new StaticMessageDialog(
I18N.getText("msg.info.server.upnp.discovering")));
try {
UPnPUtil.openPort(serverProps.getPort());
} finally {
MapTool.getFrame().hideGlassPane();
}
}
// Right now set this is set to whatever the last server settings were. If we
// wanted to turn it on and
Expand Down
66 changes: 24 additions & 42 deletions src/main/java/net/rptools/maptool/util/UPnPUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,19 @@
*/
package net.rptools.maptool.util;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import net.rptools.maptool.client.AppPreferences;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.swing.SwingUtil;
import net.sbbi.upnp.Discovery;
import net.sbbi.upnp.impls.InternetGatewayDevice;
import net.sbbi.upnp.messages.ActionResponse;
Expand All @@ -48,34 +41,6 @@ public class UPnPUtil {
private static final Logger log = LogManager.getLogger(UPnPUtil.class);
private static Map<InternetGatewayDevice, NetworkInterface> igds;
private static List<InternetGatewayDevice> mappings;
private static JDialog dialog = null;
private static JPanel panel = new JPanel(new BorderLayout());
private static Font labelFont = new Font("Dialog", Font.BOLD, 14);
private static JLabel label = new JLabel("", SwingConstants.CENTER);

private static void showMessage(String device, String msg) {
if (dialog == null) {
dialog = new JDialog(MapTool.getFrame());
dialog.setContentPane(panel);
panel.add(label, BorderLayout.CENTER);
label.setFont(labelFont);
}
if (device == null) {
dialog.setVisible(false);
} else {
dialog.setTitle("Scanning device " + device);
label.setText(msg);

Dimension d = label.getMinimumSize();
d.width += 50;
d.height += 50;
label.setPreferredSize(d);

dialog.pack();
SwingUtil.centerOver(dialog, MapTool.getFrame());
dialog.setVisible(true);
}
}

public static boolean findIGDs() {
igds = new HashMap<InternetGatewayDevice, NetworkInterface>();
Expand All @@ -84,20 +49,38 @@ public static boolean findIGDs() {
while (e.hasMoreElements()) {
NetworkInterface ni = e.nextElement();
try {
if (ni.isUp() && !ni.isLoopback() && !ni.isVirtual()) {
var addresses = Collections.list(ni.getInetAddresses());
if (addresses.isEmpty()) {
log.info("UPnP: Rejecting interface '{}' as it has no addresses", ni.getDisplayName());
} else if (ni.isLoopback()) {
log.info(
"UPnP: Rejecting interface '{}' [{}] as it is a loopback",
ni.getDisplayName(),
addresses);
} else if (ni.isVirtual()) {
log.info(
"UPnP: Rejecting interface '{}' [{}] as it is virtual",
ni.getDisplayName(),
addresses);
} else if (!ni.isUp()) {
log.info(
"UPnP: Rejecting interface '{}' [{}] as it is not up",
ni.getDisplayName(),
addresses);
} else {
int found = 0;
try {
log.info("UPnP: Trying interface {}", ni.getDisplayName());
log.info(
"UPnP: Looking for gateway devices on interface '{}' [{}]",
ni.getDisplayName(),
addresses);
InternetGatewayDevice[] thisNI;
showMessage(
ni.getDisplayName(), "Looking for gateway devices on " + ni.getDisplayName());
thisNI =
InternetGatewayDevice.getDevices(
AppPreferences.getUpnpDiscoveryTimeout(),
Discovery.DEFAULT_TTL,
Discovery.DEFAULT_MX,
ni);
showMessage(null, null);
if (thisNI != null) {
for (InternetGatewayDevice igd : thisNI) {
found++;
Expand All @@ -116,7 +99,6 @@ public static boolean findIGDs() {
}
}
} catch (IOException ex) {
showMessage(null, null);
// some IO Exception occurred during communication with device
log.warn("While searching for internet gateway devices", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2354,6 +2354,7 @@ msg.info.stopWebEndWebPoint = Stopping web end point.
# code adds that, if appropriate, based on the situation.
msg.info.versionFile = CAN'T FIND VERSION FILE
msg.info.assetsDisabled = The GM has disabled insertion of player assets.
msg.info.server.upnp.discovering = Discovering UPnP gateways...


msg.title.exportMacro = Export Macro
Expand Down
Loading