Skip to content

Commit d59a197

Browse files
Merge pull request #117 from AikidoSec/fix-ips
Fix IP reporting
2 parents fa7a2b9 + 0004e9e commit d59a197

File tree

1 file changed

+11
-31
lines changed
  • agent_api/src/main/java/dev/aikido/agent_api/helpers/net

1 file changed

+11
-31
lines changed
Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,25 @@
11
package dev.aikido.agent_api.helpers.net;
22

33
import java.net.InetAddress;
4-
import java.net.NetworkInterface;
5-
import java.net.SocketException;
6-
import java.util.Enumeration;
4+
import java.net.UnknownHostException;
75

86
public final class IPAddress {
9-
private IPAddress() {}
7+
private IPAddress() {
8+
}
109

1110
public static String getIpAddress() {
1211
try {
13-
NetworkInterface bestInterface = null;
14-
15-
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
16-
while (networkInterfaces.hasMoreElements()) {
17-
NetworkInterface networkInterface = networkInterfaces.nextElement();
12+
String hostAddress = InetAddress.getLocalHost().getHostAddress();
1813

19-
// Check if the network interface is up and not a loop-back
20-
if (networkInterface.isUp() && !networkInterface.isLoopback()) {
21-
// Check if it has at least one valid IP address
22-
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
23-
while (inetAddresses.hasMoreElements()) {
24-
InetAddress inetAddress = inetAddresses.nextElement();
25-
if (!inetAddress.isLoopbackAddress()) { // Check if the address is not a loop-back address
26-
bestInterface = networkInterface;
27-
break; // Found a valid interface, no need to check further
28-
}
29-
}
30-
}
14+
// Remove the zone index if present
15+
if (hostAddress.contains("%")) {
16+
hostAddress = hostAddress.substring(0, hostAddress.indexOf('%'));
3117
}
3218

33-
if (bestInterface != null) {
34-
Enumeration<InetAddress> inetAddresses = bestInterface.getInetAddresses();
35-
while (inetAddresses.hasMoreElements()) {
36-
InetAddress inetAddress = inetAddresses.nextElement();
37-
if (!inetAddress.isLoopbackAddress()) {
38-
return inetAddress.getHostAddress();
39-
}
40-
}
41-
}
42-
} catch (SocketException ignored) {}
19+
return hostAddress;
20+
} catch (UnknownHostException ignored) {
21+
// pass-through
22+
}
4323
return "0.0.0.0";
4424
}
4525
}

0 commit comments

Comments
 (0)