From f07237d1d8546793b24bddd8964a1c80e23a2f10 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Wed, 11 Dec 2024 15:48:41 +0100 Subject: [PATCH] u --- .../autopeerer/client/MyWebsocketClient.java | 4 +- .../m724/autopeerer/client/PacketHandler.java | 18 ++++-- .../client/wireguard/WireGuardLive.java | 28 ++++++++ .../client/wireguard/WireGuardSession.java | 26 +++++++- .../eu/m724/autopeerer/packet/Packets.java | 8 +-- .../packet/client/BgpRequestPacket.java | 43 ------------- ...tPacket.java => SessionRequestPacket.java} | 14 ++-- .../packet/server/BgpResponsePacket.java | 37 ----------- .../packet/server/SessionResponsePacket.java | 64 +++++++++++++++++++ .../packet/server/VpnResponsePacket.java | 48 -------------- 10 files changed, 143 insertions(+), 147 deletions(-) create mode 100644 src/main/java/eu/m724/autopeerer/client/wireguard/WireGuardLive.java delete mode 100644 src/main/java/eu/m724/autopeerer/packet/client/BgpRequestPacket.java rename src/main/java/eu/m724/autopeerer/packet/client/{VpnRequestPacket.java => SessionRequestPacket.java} (80%) delete mode 100644 src/main/java/eu/m724/autopeerer/packet/server/BgpResponsePacket.java create mode 100644 src/main/java/eu/m724/autopeerer/packet/server/SessionResponsePacket.java delete mode 100644 src/main/java/eu/m724/autopeerer/packet/server/VpnResponsePacket.java diff --git a/src/main/java/eu/m724/autopeerer/client/MyWebsocketClient.java b/src/main/java/eu/m724/autopeerer/client/MyWebsocketClient.java index bc94612..a917f6e 100644 --- a/src/main/java/eu/m724/autopeerer/client/MyWebsocketClient.java +++ b/src/main/java/eu/m724/autopeerer/client/MyWebsocketClient.java @@ -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); } diff --git a/src/main/java/eu/m724/autopeerer/client/PacketHandler.java b/src/main/java/eu/m724/autopeerer/client/PacketHandler.java index 6ed08cd..c8ea039 100644 --- a/src/main/java/eu/m724/autopeerer/client/PacketHandler.java +++ b/src/main/java/eu/m724/autopeerer/client/PacketHandler.java @@ -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 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); + } } } diff --git a/src/main/java/eu/m724/autopeerer/client/wireguard/WireGuardLive.java b/src/main/java/eu/m724/autopeerer/client/wireguard/WireGuardLive.java new file mode 100644 index 0000000..2bd4227 --- /dev/null +++ b/src/main/java/eu/m724/autopeerer/client/wireguard/WireGuardLive.java @@ -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); + } +} diff --git a/src/main/java/eu/m724/autopeerer/client/wireguard/WireGuardSession.java b/src/main/java/eu/m724/autopeerer/client/wireguard/WireGuardSession.java index fb3d179..e44c734 100644 --- a/src/main/java/eu/m724/autopeerer/client/wireguard/WireGuardSession.java +++ b/src/main/java/eu/m724/autopeerer/client/wireguard/WireGuardSession.java @@ -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 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") + ); } } diff --git a/src/main/java/eu/m724/autopeerer/packet/Packets.java b/src/main/java/eu/m724/autopeerer/packet/Packets.java index 3fddb55..c5ce3fc 100644 --- a/src/main/java/eu/m724/autopeerer/packet/Packets.java +++ b/src/main/java/eu/m724/autopeerer/packet/Packets.java @@ -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; diff --git a/src/main/java/eu/m724/autopeerer/packet/client/BgpRequestPacket.java b/src/main/java/eu/m724/autopeerer/packet/client/BgpRequestPacket.java deleted file mode 100644 index 9481e09..0000000 --- a/src/main/java/eu/m724/autopeerer/packet/client/BgpRequestPacket.java +++ /dev/null @@ -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 { - 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); - } -} diff --git a/src/main/java/eu/m724/autopeerer/packet/client/VpnRequestPacket.java b/src/main/java/eu/m724/autopeerer/packet/client/SessionRequestPacket.java similarity index 80% rename from src/main/java/eu/m724/autopeerer/packet/client/VpnRequestPacket.java rename to src/main/java/eu/m724/autopeerer/packet/client/SessionRequestPacket.java index 4023668..1739cc0 100644 --- a/src/main/java/eu/m724/autopeerer/packet/client/VpnRequestPacket.java +++ b/src/main/java/eu/m724/autopeerer/packet/client/SessionRequestPacket.java @@ -11,15 +11,15 @@ import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.Base64; -public class VpnRequestPacket implements Packet { - public final short connectionId; +public class SessionRequestPacket implements Packet { + 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 { 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 { 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 diff --git a/src/main/java/eu/m724/autopeerer/packet/server/BgpResponsePacket.java b/src/main/java/eu/m724/autopeerer/packet/server/BgpResponsePacket.java deleted file mode 100644 index 5975ab2..0000000 --- a/src/main/java/eu/m724/autopeerer/packet/server/BgpResponsePacket.java +++ /dev/null @@ -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 { - 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); - } -} diff --git a/src/main/java/eu/m724/autopeerer/packet/server/SessionResponsePacket.java b/src/main/java/eu/m724/autopeerer/packet/server/SessionResponsePacket.java new file mode 100644 index 0000000..345e25e --- /dev/null +++ b/src/main/java/eu/m724/autopeerer/packet/server/SessionResponsePacket.java @@ -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 { + 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 + } +} diff --git a/src/main/java/eu/m724/autopeerer/packet/server/VpnResponsePacket.java b/src/main/java/eu/m724/autopeerer/packet/server/VpnResponsePacket.java deleted file mode 100644 index 2901db9..0000000 --- a/src/main/java/eu/m724/autopeerer/packet/server/VpnResponsePacket.java +++ /dev/null @@ -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 { - 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; - } -}