Skip to content

Commit

Permalink
Improved local address resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
jakovrr authored and akuhtz committed Dec 5, 2022
1 parent f4307cc commit 56a27e7
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
Expand Down Expand Up @@ -228,6 +229,7 @@ private String getLocalAddressOfType(Class<? extends InetAddress> cl) {
return overrideIP.trim();
}
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
List<InetAddress> localAddresses = new ArrayList<>();
while (interfaces.hasMoreElements()) {
NetworkInterface current = interfaces.nextElement();
if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
Expand All @@ -237,12 +239,31 @@ private String getLocalAddressOfType(Class<? extends InetAddress> cl) {
while (addresses.hasMoreElements()) {
InetAddress adr = addresses.nextElement();
if (cl.isInstance(adr)) {
Logger.log(JavaxPacScriptParser.class, LogLevel.TRACE, "Local address resolved to {}", adr);
return adr.getHostAddress();
localAddresses.add(adr);
}
}
}
return "";
if (localAddresses.isEmpty()) {
return "";
} else {
InetAddress localAddress = localAddresses.get(0);
if (localAddresses.size() > 1) {
// if we have multiple addresses we prefer the localHost address
if (Inet4Address.class.equals(cl)) {
InetAddress localHost = Inet4Address.getLocalHost();
if (localAddresses.contains(localHost)) {
localAddress = localHost;
}
} else if (Inet6Address.class.equals(cl)) {
InetAddress localHost = Inet6Address.getLocalHost();
if (localAddresses.contains(localHost)) {
localAddress = localHost;
}
}
}
Logger.log(JavaxPacScriptParser.class, LogLevel.TRACE, "Local address resolved to {}", localAddress);
return localAddress.getHostAddress();
}
} catch (IOException e) {
Logger.log(JavaxPacScriptParser.class, LogLevel.DEBUG, "Local address not resolvable.");
return "";
Expand Down

0 comments on commit 56a27e7

Please sign in to comment.