diff --git a/src/main/java/com/bwssystems/HABridge/BridgeSettings.java b/src/main/java/com/bwssystems/HABridge/BridgeSettings.java index 5e9ce8d..78bbe88 100644 --- a/src/main/java/com/bwssystems/HABridge/BridgeSettings.java +++ b/src/main/java/com/bwssystems/HABridge/BridgeSettings.java @@ -24,6 +24,7 @@ import org.slf4j.LoggerFactory; import com.bwssystems.HABridge.util.BackupHandler; import com.bwssystems.HABridge.util.JsonTransformer; +import com.bwssystems.HABridge.util.ParseRoute; import com.google.gson.Gson; public class BridgeSettings extends BackupHandler { @@ -166,8 +167,9 @@ public class BridgeSettings extends BackupHandler { theBridgeSettings.setNestpwd(System.getProperty("nest.pwd")); } + ParseRoute aDefaultRoute = ParseRoute.getInstance(); if(theBridgeSettings.getUpnpConfigAddress() == null || theBridgeSettings.getUpnpConfigAddress().trim().equals("") || theBridgeSettings.getUpnpConfigAddress().trim().equals("0.0.0.0")) { - addressString = checkIpAddress(null, true); + addressString = aDefaultRoute.getLocalIPAddress(); if(addressString != null) { theBridgeSettings.setUpnpConfigAddress(addressString); log.info("Adding " + addressString + " as our default upnp config address."); @@ -177,8 +179,10 @@ public class BridgeSettings extends BackupHandler { } else { addressString = checkIpAddress(theBridgeSettings.getUpnpConfigAddress(), false); - if(addressString == null) - log.warn("The upnp config address, " + theBridgeSettings.getUpnpConfigAddress() + ", does not match any known IP's on this host."); + if(addressString == null) { + addressString = aDefaultRoute.getLocalIPAddress(); + log.warn("The upnp config address, " + theBridgeSettings.getUpnpConfigAddress() + ", does not match any known IP's on this host. Using default address: " + addressString); + } } if(theBridgeSettings.getUpnpResponsePort() == null) diff --git a/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java b/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java index f6e644f..c053ae0 100644 --- a/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java +++ b/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java @@ -119,14 +119,19 @@ public class UpnpSettingsResource { // Ruthlessly stolen from https://stackoverflow.com/questions/22045165/java-datagrampacket-receive-how-to-determine-local-ip-interface // Try to get a source IP that makes sense for the requester to contact for use in the LOCATION header in replies private InetAddress getOutboundAddress(String remoteAddress, int remotePort) throws SocketException { + InetAddress localAddress = null; + try { DatagramSocket sock = new DatagramSocket(); // connect is needed to bind the socket and retrieve the local address // later (it would return 0.0.0.0 otherwise) sock.connect(InetSocketAddress.createUnresolved(remoteAddress, remotePort)); - final InetAddress localAddress = sock.getLocalAddress(); + localAddress = sock.getLocalAddress(); sock.disconnect(); sock.close(); sock = null; + } catch(Exception e) { + + } return localAddress; } }