mirror of
https://github.com/kuroppoi/entralinked.git
synced 2026-09-30 11:26:36 -05:00
Try get local host using a socket first
This commit is contained in:
@@ -3,11 +3,8 @@ package entralinked;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
@@ -28,6 +25,7 @@ import entralinked.network.http.dashboard.DashboardHandler;
|
||||
import entralinked.network.http.dls.DlsHandler;
|
||||
import entralinked.network.http.nas.NasHandler;
|
||||
import entralinked.network.http.pgl.PglHandler;
|
||||
import entralinked.utility.NetworkUtility;
|
||||
|
||||
public class Entralinked {
|
||||
|
||||
@@ -73,12 +71,12 @@ public class Entralinked {
|
||||
String hostName = configuration.hostName();
|
||||
|
||||
if(hostName.equals("local") || hostName.equals("localhost")) {
|
||||
hostAddress = tryGetLocalHost();
|
||||
hostAddress = NetworkUtility.getLocalHost();
|
||||
} else {
|
||||
try {
|
||||
hostAddress = InetAddress.getByName(hostName);
|
||||
} catch(UnknownHostException e) {
|
||||
hostAddress = tryGetLocalHost();
|
||||
hostAddress = NetworkUtility.getLocalHost();
|
||||
logger.error("Could not resolve host name - falling back to {} ", hostAddress, e);
|
||||
}
|
||||
}
|
||||
@@ -162,36 +160,6 @@ public class Entralinked {
|
||||
}
|
||||
}
|
||||
|
||||
private InetAddress tryGetLocalHost() {
|
||||
try {
|
||||
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
|
||||
while(networkInterfaces.hasMoreElements()) {
|
||||
NetworkInterface networkInterface = networkInterfaces.nextElement();
|
||||
|
||||
// Skip if loopback interface
|
||||
if(networkInterface.isLoopback()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
|
||||
|
||||
while(addresses.hasMoreElements()) {
|
||||
InetAddress address = addresses.nextElement();
|
||||
|
||||
// Return if IPv4
|
||||
if(address instanceof Inet4Address) {
|
||||
return address;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(IOException e) {
|
||||
logger.error("Could not determine local host", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Configuration getConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
57
src/main/java/entralinked/utility/NetworkUtility.java
Normal file
57
src/main/java/entralinked/utility/NetworkUtility.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package entralinked.utility;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.Socket;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class NetworkUtility {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger();
|
||||
|
||||
public static InetAddress getLocalHost() {
|
||||
try(Socket socket = new Socket()){
|
||||
socket.connect(new InetSocketAddress("github.com", 80));
|
||||
return socket.getLocalAddress();
|
||||
} catch(IOException e) {
|
||||
logger.error("Couldn't get local host using socket - falling back to the network interface method", e);
|
||||
return getLocalHostFromNetworkInterfaces();
|
||||
}
|
||||
}
|
||||
|
||||
private static InetAddress getLocalHostFromNetworkInterfaces() {
|
||||
try {
|
||||
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
|
||||
while(networkInterfaces.hasMoreElements()) {
|
||||
NetworkInterface networkInterface = networkInterfaces.nextElement();
|
||||
|
||||
// Skip if loopback interface
|
||||
if(networkInterface.isLoopback()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
|
||||
|
||||
while(addresses.hasMoreElements()) {
|
||||
InetAddress address = addresses.nextElement();
|
||||
|
||||
// Return if IPv4
|
||||
if(address instanceof Inet4Address) {
|
||||
return address;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(IOException e) {
|
||||
logger.error("Could not determine local host - falling back to loopback address", e);
|
||||
}
|
||||
|
||||
return InetAddress.getLoopbackAddress();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user