|
29 | 29 | import java.net.HttpURLConnection;
|
30 | 30 | import java.net.InetAddress;
|
31 | 31 | import java.net.MalformedURLException;
|
| 32 | +import java.net.NetworkInterface; |
| 33 | +import java.net.SocketException; |
32 | 34 | import java.net.URL;
|
33 | 35 | import java.nio.charset.StandardCharsets;
|
34 | 36 | import java.util.ArrayDeque;
|
35 | 37 | import java.util.Deque;
|
| 38 | +import java.util.Enumeration; |
36 | 39 |
|
37 | 40 | import javax.net.ssl.SSLHandshakeException;
|
38 | 41 |
|
@@ -344,15 +347,34 @@ public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
|
344 | 347 | });
|
345 | 348 | }
|
346 | 349 |
|
| 350 | + // Attempt to get a non-loopback address for the local server |
| 351 | + // and fallback to loopback if there is an error |
| 352 | + private String getLocalAddress(NsdServiceInfo serviceInfo){ |
| 353 | + try { |
| 354 | + for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { |
| 355 | + NetworkInterface intf = en.nextElement(); |
| 356 | + for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { |
| 357 | + InetAddress inetAddress = enumIpAddr.nextElement(); |
| 358 | + if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) { |
| 359 | + return inetAddress.getHostAddress(); |
| 360 | + } |
| 361 | + } |
| 362 | + } |
| 363 | + } catch (SocketException ex) { |
| 364 | + Log.e(OM_SUPPLY, ex.toString()); |
| 365 | + } |
| 366 | + return serviceInfo.getHost().getHostAddress(); |
| 367 | + } |
347 | 368 | private JSObject serviceInfoToObject(NsdServiceInfo serviceInfo) {
|
348 | 369 | String serverHardwareId = parseAttribute(serviceInfo, discoveryConstants.HARDWARE_ID_KEY);
|
| 370 | + Boolean isLocal = serverHardwareId.equals(discoveryConstants.hardwareId); |
349 | 371 | return new JSObject()
|
350 | 372 | .put("protocol", parseAttribute(serviceInfo, discoveryConstants.PROTOCOL_KEY))
|
351 | 373 | .put("clientVersion", parseAttribute(serviceInfo, discoveryConstants.CLIENT_VERSION_KEY))
|
352 | 374 | .put("port", serviceInfo.getPort())
|
353 |
| - .put("ip", serviceInfo.getHost().getHostAddress()) |
| 375 | + .put("ip", isLocal ? getLocalAddress(serviceInfo) : serviceInfo.getHost().getHostAddress()) |
354 | 376 | .put("hardwareId", serverHardwareId)
|
355 |
| - .put("isLocal", serverHardwareId.equals(discoveryConstants.hardwareId)); |
| 377 | + .put("isLocal", isLocal); |
356 | 378 |
|
357 | 379 | }
|
358 | 380 |
|
@@ -472,6 +494,13 @@ public class FrontEndHost {
|
472 | 494 | JSObject data;
|
473 | 495 |
|
474 | 496 | public FrontEndHost(JSObject data) {
|
| 497 | + String ip = data.getString("ip"); |
| 498 | + // attempt to translate loopback addresses to an actual IP address |
| 499 | + // so that we can display the local server IP for users to connect to the API |
| 500 | + if (data.getBool("isLocal") && (ip.equals("127.0.0.1") || ip.equals("localhost"))) { |
| 501 | + NsdServiceInfo serviceInfo = createLocalServiceInfo(); |
| 502 | + data.put("ip", getLocalAddress(serviceInfo)); |
| 503 | + } |
475 | 504 | this.data = data;
|
476 | 505 | }
|
477 | 506 |
|
|
0 commit comments