This commit is contained in:
parent
32723679e1
commit
b9603b6264
5 changed files with 54 additions and 27 deletions
|
@ -18,6 +18,8 @@ import eu.m724.autopeerer.common.packet.c2s.SessionResponsePacket;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.net.Inet4Address;
|
||||||
|
import java.net.Inet6Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.nio.BufferUnderflowException;
|
import java.nio.BufferUnderflowException;
|
||||||
|
@ -76,38 +78,51 @@ public class PacketHandler {
|
||||||
|
|
||||||
private void handlePingRequest(PingRequestPacket packet) {
|
private void handlePingRequest(PingRequestPacket packet) {
|
||||||
System.out.printf("Ping request #%d to %s\n", packet.requestId, packet.target.getHostAddress());
|
System.out.printf("Ping request #%d to %s\n", packet.requestId, packet.target.getHostAddress());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
CompletableFuture.runAsync(() -> {
|
CompletableFuture.runAsync(() -> {
|
||||||
float average = -1, meanDeviation = -1;
|
float average = -1, meanDeviation = -1;
|
||||||
var status = PingResponsePacket.PingResponseStatus.ERROR;
|
var status = PingResponsePacket.PingResponseStatus.ERROR;
|
||||||
|
|
||||||
try {
|
if (packet.target instanceof Inet6Address && !Connectivity.ipv6()) {
|
||||||
// -3 was also an arumetn but not wokring on all
|
// target ipv6 but no ipv6
|
||||||
Process process = Runtime.getRuntime().exec(new String[] { "ping", "-Ac", "10", "-W", "1", packet.target.getHostAddress() });
|
status = PingResponsePacket.PingResponseStatus.UNREACHABLE_PROTOCOL;
|
||||||
|
} else if (packet.target instanceof Inet4Address && !Connectivity.ipv4()) {
|
||||||
|
// target ipv4 but no ipv4
|
||||||
|
status = PingResponsePacket.PingResponseStatus.UNREACHABLE_PROTOCOL;
|
||||||
|
} else {
|
||||||
|
// target ip version supported
|
||||||
|
try {
|
||||||
|
// -3 was also an arumetn but not wokring on all
|
||||||
|
Process process = Runtime.getRuntime().exec(new String[] { "ping", "-Ac", "10", "-W", "1", packet.target.getHostAddress() });
|
||||||
|
|
||||||
try (BufferedReader reader = process.inputReader()) {
|
try (BufferedReader reader = process.inputReader()) {
|
||||||
for (String l : reader.lines().toList()) {
|
for (String l : reader.lines().toList()) {
|
||||||
boolean end = l.startsWith("rtt");
|
boolean end = l.startsWith("rtt");
|
||||||
|
|
||||||
if (l.startsWith("PING")) {
|
if (l.startsWith("PING")) {
|
||||||
status = PingResponsePacket.PingResponseStatus.UNREACHABLE;
|
status = PingResponsePacket.PingResponseStatus.UNREACHABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (end) {
|
if (end) {
|
||||||
String[] results = l.split(" ")[3].split("/");
|
String[] results = l.split(" ")[3].split("/");
|
||||||
average = Float.parseFloat(results[1]);
|
average = Float.parseFloat(results[1]);
|
||||||
meanDeviation = Float.parseFloat(results[3]);
|
meanDeviation = Float.parseFloat(results[3]);
|
||||||
status = PingResponsePacket.PingResponseStatus.OK;
|
status = PingResponsePacket.PingResponseStatus.OK;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var code = process.waitFor();
|
var code = process.waitFor();
|
||||||
if (code != 0) {
|
if (code != 0) {
|
||||||
System.out.printf("ping returned " + code);
|
System.out.printf("ping returned " + code);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
System.err.println("Error executing ping request: " + e.getMessage());
|
System.err.println("Error executing ping request: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.printf("Ping request #%d to %s - %s avg %.3f / mdev %.3f ms\n", packet.requestId, packet.target.getHostAddress(), status, average, meanDeviation);
|
System.out.printf("Ping request #%d to %s - %s avg %.3f / mdev %.3f ms\n", packet.requestId, packet.target.getHostAddress(), status, average, meanDeviation);
|
||||||
|
|
|
@ -7,7 +7,7 @@ import java.net.UnknownHostException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class AddressTools {
|
public class AddressTools {
|
||||||
private static boolean isIp(InetAddress address, boolean ipv6) {
|
public static boolean isIp(InetAddress address, boolean ipv6) {
|
||||||
return ipv6 ? address instanceof Inet6Address : address instanceof Inet4Address;
|
return ipv6 ? address instanceof Inet6Address : address instanceof Inet4Address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,8 +29,12 @@ public class PingResponsePacket implements Packet<PingResponsePacket> {
|
||||||
|
|
||||||
var status = PingResponseStatus.OK;
|
var status = PingResponseStatus.OK;
|
||||||
|
|
||||||
if (average == -1) {
|
if (average == -1 && meanDeviation == 0) {
|
||||||
status = meanDeviation == 0 ? PingResponseStatus.UNREACHABLE : PingResponseStatus.ERROR;
|
status = PingResponseStatus.UNREACHABLE;
|
||||||
|
} else if (average == -1 && meanDeviation == -1) {
|
||||||
|
status = PingResponseStatus.ERROR;
|
||||||
|
} else if (average == 0 && meanDeviation == -1) {
|
||||||
|
status = PingResponseStatus.UNREACHABLE_PROTOCOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new PingResponsePacket(requestId, status, average, meanDeviation);
|
return new PingResponsePacket(requestId, status, average, meanDeviation);
|
||||||
|
@ -53,11 +57,15 @@ public class PingResponsePacket implements Packet<PingResponsePacket> {
|
||||||
bb.putFloat(-1);
|
bb.putFloat(-1);
|
||||||
bb.putFloat(-1);
|
bb.putFloat(-1);
|
||||||
}
|
}
|
||||||
|
case UNREACHABLE_PROTOCOL -> {
|
||||||
|
bb.putFloat(0);
|
||||||
|
bb.putFloat(-1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return bb;
|
return bb;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum PingResponseStatus {
|
public enum PingResponseStatus {
|
||||||
OK, UNREACHABLE, ERROR
|
OK, UNREACHABLE, ERROR, UNREACHABLE_PROTOCOL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -187,7 +187,9 @@ public class MyHttpHandler implements HttpHandler {
|
||||||
|
|
||||||
if (packet.result == SessionResponsePacket.SessionResult.OK) {
|
if (packet.result == SessionResponsePacket.SessionResult.OK) {
|
||||||
resp.put("port", packet.port)
|
resp.put("port", packet.port)
|
||||||
.put("publicKey", packet.publicKey);
|
.put("publicKey", packet.publicKey)
|
||||||
|
.put("endpoint", node.getNodeProfile().hostname() + ":" + packet.port)
|
||||||
|
.put("linkLocal", node.getNodeProfile().linkLocal());
|
||||||
}
|
}
|
||||||
|
|
||||||
sseWrite(exchange, resp.toString());
|
sseWrite(exchange, resp.toString());
|
||||||
|
@ -205,6 +207,8 @@ public class MyHttpHandler implements HttpHandler {
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
} else {
|
||||||
|
sendResponse(exchange, 404);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,8 +71,8 @@ public final class Node {
|
||||||
properties.getProperty("hostname"),
|
properties.getProperty("hostname"),
|
||||||
linkLocal,
|
linkLocal,
|
||||||
ipv4.equalsIgnoreCase("yes") || ipv4.equalsIgnoreCase("restricted"),
|
ipv4.equalsIgnoreCase("yes") || ipv4.equalsIgnoreCase("restricted"),
|
||||||
ipv4.equalsIgnoreCase("restricted"),
|
|
||||||
ipv6.equalsIgnoreCase("yes") || ipv6.equalsIgnoreCase("restricted"),
|
ipv6.equalsIgnoreCase("yes") || ipv6.equalsIgnoreCase("restricted"),
|
||||||
|
ipv4.equalsIgnoreCase("restricted"),
|
||||||
ipv6.equalsIgnoreCase("restricted")
|
ipv6.equalsIgnoreCase("restricted")
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Reference in a new issue