This commit is contained in:
Minecon724 2024-12-11 15:48:41 +01:00
parent 0f92f5bcbe
commit f07237d1d8
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
10 changed files with 143 additions and 147 deletions

View file

@ -2,7 +2,7 @@ package eu.m724.autopeerer.client;
import eu.m724.autopeerer.packet.Packets;
import eu.m724.autopeerer.packet.client.PingRequestPacket;
import eu.m724.autopeerer.packet.client.VpnRequestPacket;
import eu.m724.autopeerer.packet.client.SessionRequestPacket;
import inet.ipaddr.IPAddressString;
import inet.ipaddr.ipv6.IPv6Address;
import org.java_websocket.client.WebSocketClient;
@ -38,7 +38,7 @@ public class MyWebsocketClient extends WebSocketClient {
send(Packets.compose(new PingRequestPacket((short)1, InetAddress.getByName("1.1.1.3"))));
send(Packets.compose(new PingRequestPacket((short)2, InetAddress.getByName("1.1.1.1"))));
send(Packets.compose(new VpnRequestPacket((short)1, (IPv6Address) new IPAddressString("fefe::fefe").getAddress(), "sAt8JSXW4leihcAAdsghsfgFWkO5stBZJm87PGLZFXY=", "example.com", 6823)));
send(Packets.compose(new SessionRequestPacket((short)1, (IPv6Address) new IPAddressString("fefe::fefe").getAddress(), "sAt8JSXW4leihcAAdsghsfgFWkO5stBZJm87PGLZFXY=", "example.com", 6823)));
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}

View file

@ -1,12 +1,15 @@
package eu.m724.autopeerer.client;
import eu.m724.autopeerer.client.wireguard.WireGuardLive;
import eu.m724.autopeerer.client.wireguard.WireGuardSession;
import eu.m724.autopeerer.packet.*;
import eu.m724.autopeerer.packet.client.PingRequestPacket;
import eu.m724.autopeerer.packet.client.VpnRequestPacket;
import eu.m724.autopeerer.packet.client.SessionRequestPacket;
import eu.m724.autopeerer.packet.server.PingResponsePacket;
import eu.m724.autopeerer.packet.server.SessionResponsePacket;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
@ -16,6 +19,8 @@ import java.util.function.Consumer;
public class PacketHandler {
Consumer<ByteBuffer> sender;
// TODO not here
private final WireGuardLive wireGuardLive = new WireGuardLive(new File("configs"));
void handle(ByteBuffer bytes) {
Packet<?> p;
@ -37,7 +42,7 @@ public class PacketHandler {
if (p instanceof PingRequestPacket packet) {
handlePingRequest(packet);
} else if (p instanceof VpnRequestPacket packet) {
} else if (p instanceof SessionRequestPacket packet) {
handleVpnRequest(packet);
}
}
@ -79,8 +84,13 @@ public class PacketHandler {
});
}
private void handleVpnRequest(VpnRequestPacket packet) {
private void handleVpnRequest(SessionRequestPacket packet) {
var session = new WireGuardSession(12345, "serverpoecjteta", "fefe:fefe::fefe", packet.linkLocal.toCompressedString(), packet.endpointHost + ":" + packet.endpointPort, packet.publicKey);
System.err.println(session.config());
try {
wireGuardLive.saveSession(packet.sessionId, session);
} catch (IOException e) {
// sender.accept(new SessionResponsePacket(packet.sessionId, ));
throw new RuntimeException(e);
}
}
}

View file

@ -0,0 +1,28 @@
package eu.m724.autopeerer.client.wireguard;
import java.io.*;
import java.nio.file.Files;
public class WireGuardLive {
private final File configsPath;
public WireGuardLive(File configsPath) {
this.configsPath = configsPath;
}
public void saveSession(int connectionId, WireGuardSession session) throws IOException {
File file = new File(configsPath, "ap_" + connectionId + ".conf");
file.createNewFile();
try (FileWriter writer = new FileWriter(file)) {
writer.write(session.config());
}
}
public WireGuardSession getSession(int connectionId) throws IOException {
File file = new File(configsPath, "ap_" + connectionId + ".conf");
String s = Files.readString(file.toPath());
return WireGuardSession.fromString(s);
}
}

View file

@ -1,9 +1,12 @@
package eu.m724.autopeerer.client.wireguard;
import java.util.HashMap;
import java.util.Map;
public record WireGuardSession(
int listenPort,
String serverPrivateKey,
String localLinkLocal,
String serverLinkLocal,
String clientLinkLocal,
String endpoint,
String clientPublicKey
@ -20,6 +23,25 @@ public record WireGuardSession(
Endpoint = %s
PublicKey = %s
AllowedIPs = ::/0"""
.formatted(listenPort, serverPrivateKey, localLinkLocal, clientLinkLocal, endpoint, clientPublicKey);
.formatted(listenPort, serverPrivateKey, serverLinkLocal, clientLinkLocal, endpoint, clientPublicKey);
}
public static WireGuardSession fromString(String s) {
Map<String, String> values = new HashMap<>();
s.lines().forEach(l -> {
if (l.startsWith("[")) return;
String[] kv = l.substring(2).split(" = ");
values.put(kv[0], kv[1].stripLeading());
});
return new WireGuardSession(
Integer.parseInt(values.get("ListenPort")),
values.get("PrivateKey"),
values.get("PostUp").split(" ")[5],
values.get("PostUp").split(" ")[7],
values.get("Endpoint"),
values.get("PublicKey")
);
}
}

View file

@ -1,9 +1,9 @@
package eu.m724.autopeerer.packet;
import eu.m724.autopeerer.packet.client.PingRequestPacket;
import eu.m724.autopeerer.packet.client.VpnRequestPacket;
import eu.m724.autopeerer.packet.client.SessionRequestPacket;
import eu.m724.autopeerer.packet.server.PingResponsePacket;
import eu.m724.autopeerer.packet.server.VpnResponsePacket;
import eu.m724.autopeerer.packet.server.SessionResponsePacket;
import java.nio.ByteBuffer;
@ -16,7 +16,7 @@ public class Packets {
if (id == 1) {
packet = PingRequestPacket.deserialize(buffer);
} else if (id == 2) {
packet = VpnRequestPacket.deserialize(buffer);
packet = SessionRequestPacket.deserialize(buffer);
}
return packet;
@ -30,7 +30,7 @@ public class Packets {
if (id == 1) {
packet = PingResponsePacket.deserialize(buffer);
} else if (id == 2) {
packet = VpnResponsePacket.deserialize(buffer);
packet = SessionResponsePacket.deserialize(buffer);
}
return packet;

View file

@ -1,43 +0,0 @@
package eu.m724.autopeerer.packet.client;
import eu.m724.autopeerer.packet.Packet;
import inet.ipaddr.ipv6.IPv6Address;
import java.nio.ByteBuffer;
public class BgpRequestPacket implements Packet<BgpRequestPacket> {
public final short id;
public final long asn;
public final IPv6Address linkLocal;
public BgpRequestPacket(short id, long asn, IPv6Address linkLocal) {
this.id = id;
this.asn = asn;
this.linkLocal = linkLocal;
}
@Override
public byte getId() {
return 3;
}
@Override
public ByteBuffer serialize() {
ByteBuffer buffer = ByteBuffer.allocate(22);
buffer.putShort(id);
buffer.putInt((int) (asn & 0xFFFFFFFFL));
buffer.put(linkLocal.getBytes());
return buffer;
}
public static BgpRequestPacket deserialize(ByteBuffer buffer) {
var id = buffer.getShort();
var asn = Integer.toUnsignedLong(buffer.getInt());
var ip = new byte[16];
buffer.get(ip);
var linkLocal = new IPv6Address(ip);
return new BgpRequestPacket(id, asn, linkLocal);
}
}

View file

@ -11,15 +11,15 @@ import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class VpnRequestPacket implements Packet<VpnRequestPacket> {
public final short connectionId;
public class SessionRequestPacket implements Packet<SessionRequestPacket> {
public final short sessionId;
public final IPv6Address linkLocal;
public final String publicKey;
public final String endpointHost;
public final int endpointPort;
public VpnRequestPacket(short connectionId, IPv6Address linkLocal, String publicKey, String endpointHost, int endpointPort) {
this.connectionId = connectionId;
public SessionRequestPacket(short sessionId, IPv6Address linkLocal, String publicKey, String endpointHost, int endpointPort) {
this.sessionId = sessionId;
this.linkLocal = linkLocal;
assert new IPAddressString("fe80::/10").getAddress().contains(linkLocal);
@ -43,7 +43,7 @@ public class VpnRequestPacket implements Packet<VpnRequestPacket> {
return 2;
}
public static VpnRequestPacket deserialize(ByteBuffer buffer) throws Exception {
public static SessionRequestPacket deserialize(ByteBuffer buffer) throws Exception {
var id = buffer.getShort();
var ll = new byte[16];
@ -61,14 +61,14 @@ public class VpnRequestPacket implements Packet<VpnRequestPacket> {
buffer.get(ep);
var endpointHost = new String(ep, StandardCharsets.US_ASCII);
return new VpnRequestPacket(id, linkLocal, publicKey, endpointHost, endpointPort);
return new SessionRequestPacket(id, linkLocal, publicKey, endpointHost, endpointPort);
}
@Override
public ByteBuffer serialize() {
var buffer = ByteBuffer.allocate(53 + endpointHost.length());
buffer.putShort(connectionId); // 2b
buffer.putShort(sessionId); // 2b
buffer.put(linkLocal.getBytes()); // 16b
buffer.put(Base64.getDecoder().decode(publicKey)); // 32b
buffer.putShort((short)endpointPort); // 2b

View file

@ -1,37 +0,0 @@
package eu.m724.autopeerer.packet.server;
import eu.m724.autopeerer.packet.Packet;
import java.nio.ByteBuffer;
public class BgpResponsePacket implements Packet<BgpResponsePacket> {
public final short id;
public final boolean success;
public BgpResponsePacket(short id, boolean success) {
this.id = id;
this.success = success;
}
@Override
public byte getId() {
return 3;
}
@Override
public ByteBuffer serialize() {
ByteBuffer buffer = ByteBuffer.allocate(3);
buffer.putShort(id);
buffer.put((byte) (success ? 1 : 0));
return buffer;
}
public static BgpResponsePacket deserialize(ByteBuffer buffer) {
var id = buffer.getShort();
var success = buffer.get() == 1;
return new BgpResponsePacket(id, success);
}
}

View file

@ -0,0 +1,64 @@
package eu.m724.autopeerer.packet.server;
import eu.m724.autopeerer.packet.Packet;
import java.nio.ByteBuffer;
import java.util.Base64;
public class SessionResponsePacket implements Packet<SessionResponsePacket> {
public final short sessionId;
public final SessionResult result;
public final int port;
public final String publicKey;
public SessionResponsePacket(short connectionId, SessionResult result, int port, String publicKey) {
this.sessionId = connectionId;
this.result = result;
this.port = port;
this.publicKey = publicKey;
}
@Override
public byte getId() {
return 2;
}
public static SessionResponsePacket deserialize(ByteBuffer buffer) throws Exception {
var id = buffer.getShort();
var result = SessionResult.values()[buffer.get()];
int port = -1;
String publicKey = null;
if (result == SessionResult.OK) {
port = buffer.getShort() & 0xFFFF;
var pkb = new byte[32];
buffer.get(pkb);
publicKey = Base64.getEncoder().encodeToString(pkb);
}
return new SessionResponsePacket(id, result, port, publicKey);
}
@Override
public ByteBuffer serialize() {
var buffer = ByteBuffer.allocate(result == SessionResult.OK ? 37 : 3);
buffer.putShort(sessionId);
buffer.put((byte) result.ordinal());
if (result == SessionResult.OK) {
buffer.putShort((short) port);
buffer.put(Base64.getDecoder().decode(publicKey));
}
return buffer;
}
enum SessionResult {
OK, ERROR
}
}

View file

@ -1,48 +0,0 @@
package eu.m724.autopeerer.packet.server;
import eu.m724.autopeerer.packet.Packet;
import java.nio.ByteBuffer;
import java.util.Base64;
public class VpnResponsePacket implements Packet<VpnResponsePacket> {
public final short connectionId;
public final boolean success;
public final int port;
public final String publicKey;
public VpnResponsePacket(short connectionId, boolean success, int port, String publicKey) {
this.connectionId = connectionId;
this.success = success;
this.port = port;
this.publicKey = publicKey;
}
@Override
public byte getId() {
return 2;
}
public static VpnResponsePacket deserialize(ByteBuffer buffer) throws Exception {
var id = buffer.getShort();
var port = buffer.getShort() & 0xFFFF;
var success = port != 0;
var pkb = new byte[32];
buffer.get(pkb);
var publicKey = Base64.getEncoder().encodeToString(pkb);
return new VpnResponsePacket(id, success, port, publicKey);
}
@Override
public ByteBuffer serialize() {
var buffer = ByteBuffer.allocate(35);
buffer.putShort(connectionId); // 2b
buffer.put((byte) (success ? 1 : 0));
buffer.put(Base64.getDecoder().decode(publicKey));
return buffer;
}
}