mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-16 10:14:36 +00:00
Auto stash before rebase of "origin/dev_branch_5.3.x"
This commit is contained in:
@@ -9,6 +9,7 @@ import com.bwssystems.HABridge.Configuration;
|
||||
import com.bwssystems.HABridge.api.hue.HueConstants;
|
||||
import com.bwssystems.HABridge.api.hue.HuePublicConfig;
|
||||
import com.bwssystems.HABridge.util.UDPDatagramSender;
|
||||
import com.bwssystems.HABridge.util.AddressUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
@@ -331,8 +332,14 @@ public class UpnpListener {
|
||||
InetAddress requester = aPacket.getAddress();
|
||||
int sourcePort = aPacket.getPort();
|
||||
String discoveryResponse = null;
|
||||
// refactored suggestion by https://github.com/pvint
|
||||
String httpLocationAddress = getOutboundAddress(requesterAddress).getHostAddress();
|
||||
String httpLocationAddress = null;
|
||||
if(useUpnpIface) {
|
||||
httpLocationAddress = upnpConfigIP;
|
||||
} else {
|
||||
// refactored suggestion by https://github.com/pvint
|
||||
httpLocationAddress = AddressUtil.getOutboundAddress(requesterAddress).getHostAddress();
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(theUpnpSendDelay);
|
||||
} catch (InterruptedException e) {
|
||||
@@ -437,21 +444,4 @@ public class UpnpListener {
|
||||
log.debug("sendUpnpNotify notifyTemplate3 is <<<{}>>>", notifyData);
|
||||
sendUDPResponse(notifyData.getBytes(), aSocketAddress, Configuration.UPNP_DISCOVERY_PORT);
|
||||
}
|
||||
|
||||
// added by https://github.com/pvint
|
||||
// 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 requestor to contact for use
|
||||
// in the LOCATION header in replies
|
||||
private InetAddress getOutboundAddress(SocketAddress remoteAddress) throws SocketException {
|
||||
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(remoteAddress);
|
||||
final InetAddress localAddress = sock.getLocalAddress();
|
||||
sock.disconnect();
|
||||
sock.close();
|
||||
sock = null;
|
||||
return localAddress;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.bwssystems.HABridge.BridgeSettings;
|
||||
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
|
||||
import com.bwssystems.HABridge.api.hue.HueConstants;
|
||||
import com.bwssystems.HABridge.api.hue.HuePublicConfig;
|
||||
import com.bwssystems.HABridge.util.AddressUtil;
|
||||
import com.bwssystems.HABridge.util.ParseRoute;
|
||||
|
||||
import static spark.Spark.get;
|
||||
@@ -98,7 +99,12 @@ public class UpnpSettingsResource {
|
||||
httpLocationAddr = theSettings.getUpnpConfigAddress();
|
||||
hueTemplate = hueTemplate_pre + hueTemplate_mid_orig + hueTemplate_post;
|
||||
} else {
|
||||
httpLocationAddr = getOutboundAddress(request.ip(), request.port()).getHostAddress();
|
||||
|
||||
if(theSettings.isUseupnpiface()) {
|
||||
httpLocationAddr = theSettings.getUpnpConfigAddress();
|
||||
} else {
|
||||
httpLocationAddr = AddressUtil.getOutboundAddress(request.ip(), request.port()).getHostAddress();
|
||||
}
|
||||
hueTemplate = hueTemplate_pre + hueTemplate_post;
|
||||
}
|
||||
|
||||
@@ -136,28 +142,4 @@ public class UpnpSettingsResource {
|
||||
return "";
|
||||
} );
|
||||
}
|
||||
// added by https://github.com/pvint
|
||||
// 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) {
|
||||
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(new InetSocketAddress(remoteAddress, remotePort));
|
||||
localAddress = sock.getLocalAddress();
|
||||
sock.disconnect();
|
||||
sock.close();
|
||||
sock = null;
|
||||
} catch(Exception e) {
|
||||
ParseRoute theRoute = ParseRoute.getInstance();
|
||||
try {
|
||||
localAddress = InetAddress.getByName(theRoute.getLocalIPAddress());
|
||||
} catch(Exception e1) {}
|
||||
log.warn("Error <" + e.getMessage() + "> on determining interface to reply for <" + remoteAddress + ">. Using default route IP Address of " + localAddress.getHostAddress());
|
||||
}
|
||||
log.debug("getOutbountAddress returning IP Address of " + localAddress.getHostAddress());
|
||||
return localAddress;
|
||||
}
|
||||
}
|
||||
|
||||
56
src/main/java/com/bwssystems/HABridge/util/AddressUtil.java
Normal file
56
src/main/java/com/bwssystems/HABridge/util/AddressUtil.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package com.bwssystems.HABridge.util;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class AddressUtil {
|
||||
final static Logger log = LoggerFactory.getLogger(AddressUtil.class);
|
||||
|
||||
// added by https://github.com/pvint
|
||||
// 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
|
||||
public static InetAddress getOutboundAddress(String remoteAddress, int remotePort) {
|
||||
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(new InetSocketAddress(remoteAddress, remotePort));
|
||||
localAddress = sock.getLocalAddress();
|
||||
sock.disconnect();
|
||||
sock.close();
|
||||
sock = null;
|
||||
} catch (Exception e) {
|
||||
ParseRoute theRoute = ParseRoute.getInstance();
|
||||
try {
|
||||
localAddress = InetAddress.getByName(theRoute.getLocalIPAddress());
|
||||
} catch (Exception e1) {
|
||||
}
|
||||
log.warn("Error <" + e.getMessage() + "> on determining interface to reply for <" + remoteAddress
|
||||
+ ">. Using default route IP Address of " + localAddress.getHostAddress());
|
||||
}
|
||||
log.debug("getOutbountAddress returning IP Address of " + localAddress.getHostAddress());
|
||||
return localAddress;
|
||||
}
|
||||
|
||||
// added by https://github.com/pvint
|
||||
// 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 requestor to contact for use
|
||||
// in the LOCATION header in replies
|
||||
public static InetAddress getOutboundAddress(SocketAddress remoteAddress) throws SocketException {
|
||||
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(remoteAddress);
|
||||
final InetAddress localAddress = sock.getLocalAddress();
|
||||
sock.disconnect();
|
||||
sock.close();
|
||||
sock = null;
|
||||
return localAddress;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user