minor refactoring
All checks were successful
/ deploy (push) Successful in 37s

This commit is contained in:
Minecon724 2025-01-23 20:59:30 +01:00
parent 32723679e1
commit b9603b6264
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
5 changed files with 54 additions and 27 deletions

View file

@ -18,6 +18,8 @@ import eu.m724.autopeerer.common.packet.c2s.SessionResponsePacket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.BufferUnderflowException;
@ -76,10 +78,22 @@ public class PacketHandler {
private void handlePingRequest(PingRequestPacket packet) {
System.out.printf("Ping request #%d to %s\n", packet.requestId, packet.target.getHostAddress());
CompletableFuture.runAsync(() -> {
float average = -1, meanDeviation = -1;
var status = PingResponsePacket.PingResponseStatus.ERROR;
if (packet.target instanceof Inet6Address && !Connectivity.ipv6()) {
// target ipv6 but no ipv6
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() });
@ -109,6 +123,7 @@ public class PacketHandler {
} catch (IOException | InterruptedException e) {
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);
try {

View file

@ -7,7 +7,7 @@ import java.net.UnknownHostException;
import java.util.Arrays;
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;
}

View file

@ -29,8 +29,12 @@ public class PingResponsePacket implements Packet<PingResponsePacket> {
var status = PingResponseStatus.OK;
if (average == -1) {
status = meanDeviation == 0 ? PingResponseStatus.UNREACHABLE : PingResponseStatus.ERROR;
if (average == -1 && meanDeviation == 0) {
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);
@ -53,11 +57,15 @@ public class PingResponsePacket implements Packet<PingResponsePacket> {
bb.putFloat(-1);
bb.putFloat(-1);
}
case UNREACHABLE_PROTOCOL -> {
bb.putFloat(0);
bb.putFloat(-1);
}
}
return bb;
}
public enum PingResponseStatus {
OK, UNREACHABLE, ERROR
OK, UNREACHABLE, ERROR, UNREACHABLE_PROTOCOL
}
}

View file

@ -187,7 +187,9 @@ public class MyHttpHandler implements HttpHandler {
if (packet.result == SessionResponsePacket.SessionResult.OK) {
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());
@ -205,6 +207,8 @@ public class MyHttpHandler implements HttpHandler {
return null;
});
} else {
sendResponse(exchange, 404);
}
}
}

View file

@ -71,8 +71,8 @@ public final class Node {
properties.getProperty("hostname"),
linkLocal,
ipv4.equalsIgnoreCase("yes") || ipv4.equalsIgnoreCase("restricted"),
ipv4.equalsIgnoreCase("restricted"),
ipv6.equalsIgnoreCase("yes") || ipv6.equalsIgnoreCase("restricted"),
ipv4.equalsIgnoreCase("restricted"),
ipv6.equalsIgnoreCase("restricted")
)
);