mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-16 18:24: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.HueConstants;
|
||||||
import com.bwssystems.HABridge.api.hue.HuePublicConfig;
|
import com.bwssystems.HABridge.api.hue.HuePublicConfig;
|
||||||
import com.bwssystems.HABridge.util.UDPDatagramSender;
|
import com.bwssystems.HABridge.util.UDPDatagramSender;
|
||||||
|
import com.bwssystems.HABridge.util.AddressUtil;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
@@ -331,8 +332,14 @@ public class UpnpListener {
|
|||||||
InetAddress requester = aPacket.getAddress();
|
InetAddress requester = aPacket.getAddress();
|
||||||
int sourcePort = aPacket.getPort();
|
int sourcePort = aPacket.getPort();
|
||||||
String discoveryResponse = null;
|
String discoveryResponse = null;
|
||||||
// refactored suggestion by https://github.com/pvint
|
String httpLocationAddress = null;
|
||||||
String httpLocationAddress = getOutboundAddress(requesterAddress).getHostAddress();
|
if(useUpnpIface) {
|
||||||
|
httpLocationAddress = upnpConfigIP;
|
||||||
|
} else {
|
||||||
|
// refactored suggestion by https://github.com/pvint
|
||||||
|
httpLocationAddress = AddressUtil.getOutboundAddress(requesterAddress).getHostAddress();
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(theUpnpSendDelay);
|
Thread.sleep(theUpnpSendDelay);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@@ -437,21 +444,4 @@ public class UpnpListener {
|
|||||||
log.debug("sendUpnpNotify notifyTemplate3 is <<<{}>>>", notifyData);
|
log.debug("sendUpnpNotify notifyTemplate3 is <<<{}>>>", notifyData);
|
||||||
sendUDPResponse(notifyData.getBytes(), aSocketAddress, Configuration.UPNP_DISCOVERY_PORT);
|
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.BridgeSettingsDescriptor;
|
||||||
import com.bwssystems.HABridge.api.hue.HueConstants;
|
import com.bwssystems.HABridge.api.hue.HueConstants;
|
||||||
import com.bwssystems.HABridge.api.hue.HuePublicConfig;
|
import com.bwssystems.HABridge.api.hue.HuePublicConfig;
|
||||||
|
import com.bwssystems.HABridge.util.AddressUtil;
|
||||||
import com.bwssystems.HABridge.util.ParseRoute;
|
import com.bwssystems.HABridge.util.ParseRoute;
|
||||||
|
|
||||||
import static spark.Spark.get;
|
import static spark.Spark.get;
|
||||||
@@ -98,7 +99,12 @@ public class UpnpSettingsResource {
|
|||||||
httpLocationAddr = theSettings.getUpnpConfigAddress();
|
httpLocationAddr = theSettings.getUpnpConfigAddress();
|
||||||
hueTemplate = hueTemplate_pre + hueTemplate_mid_orig + hueTemplate_post;
|
hueTemplate = hueTemplate_pre + hueTemplate_mid_orig + hueTemplate_post;
|
||||||
} else {
|
} 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;
|
hueTemplate = hueTemplate_pre + hueTemplate_post;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,28 +142,4 @@ public class UpnpSettingsResource {
|
|||||||
return "";
|
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