A
Some checks failed
/ deploy (push) Failing after 37s

This commit is contained in:
Minecon724 2024-12-22 11:55:14 +01:00
parent 1437c720c0
commit 6e55236a7d
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
17 changed files with 104 additions and 97 deletions

View file

@ -0,0 +1,9 @@
package eu.m724.autopeerer.client;
import eu.m724.autopeerer.common.Configuration;
public class ClientConfiguration extends Configuration {
public ClientConfiguration() {
super("client");
}
}

View file

@ -1,27 +1,28 @@
package eu.m724.autopeerer.client; package eu.m724.autopeerer.client;
import eu.m724.autopeerer.client.wireguard.WireGuardLive;
import java.io.File;
import java.io.IOException;
import java.net.URI; import java.net.URI;
public class Main { public class Main {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
System.out.println("Hello world!"); System.out.println("Hello world!");
URI serverUri = URI.create("ws://127.0.0.1:8002"); var config = new ClientConfiguration();
try {
config.load();
} catch (IOException e) {
throw new RuntimeException("Failed to load configuration", e);
}
var packetHandler = new PacketHandler(); URI serverUri = URI.create(config.getString("remote"));
var wireGuardLive = new WireGuardLive(new File(config.getString("wireguard.directory")));
var packetHandler = new PacketHandler(wireGuardLive);
var client = new MyWebsocketClient(serverUri, packetHandler); var client = new MyWebsocketClient(serverUri, packetHandler);
var delay = 1000; client.connect();
while (true) {
var success = client.connectBlocking();
System.out.println("Reconnecting");
if (success) {
delay = 1000;
} else {
delay *= 2;
Thread.sleep(delay);
}
}
} }
} }

View file

@ -1,7 +1,7 @@
package eu.m724.autopeerer.client; package eu.m724.autopeerer.client;
import eu.m724.autopeerer.common.packet.Packets; import eu.m724.autopeerer.common.packet.Packets;
import eu.m724.autopeerer.common.packet.client.LoginPacket; import eu.m724.autopeerer.common.packet.c2s.LoginPacket;
import org.java_websocket.client.WebSocketClient; import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake; import org.java_websocket.handshake.ServerHandshake;

View file

@ -1,27 +1,32 @@
package eu.m724.autopeerer.client; package eu.m724.autopeerer.client;
import eu.m724.autopeerer.client.wireguard.WireGuardKeys;
import eu.m724.autopeerer.client.wireguard.WireGuardLive; import eu.m724.autopeerer.client.wireguard.WireGuardLive;
import eu.m724.autopeerer.client.wireguard.WireGuardSession; import eu.m724.autopeerer.client.wireguard.WireGuardSession;
import eu.m724.autopeerer.common.packet.Packet; import eu.m724.autopeerer.common.packet.Packet;
import eu.m724.autopeerer.common.packet.Packets; import eu.m724.autopeerer.common.packet.Packets;
import eu.m724.autopeerer.common.packet.client.PingRequestPacket; import eu.m724.autopeerer.common.packet.c2s.PingRequestPacket;
import eu.m724.autopeerer.common.packet.client.SessionRequestPacket; import eu.m724.autopeerer.common.packet.c2s.SessionRequestPacket;
import eu.m724.autopeerer.common.packet.server.PingResponsePacket; import eu.m724.autopeerer.common.packet.s2c.PingResponsePacket;
import eu.m724.autopeerer.common.packet.server.SessionResponsePacket; import eu.m724.autopeerer.common.packet.s2c.SessionResponsePacket;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.BufferUnderflowException; import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.file.FileAlreadyExistsException;
import java.util.Base64; import java.util.Base64;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer; import java.util.function.Consumer;
public class PacketHandler { public class PacketHandler {
Consumer<ByteBuffer> sender; Consumer<ByteBuffer> sender;
// TODO not here
private final WireGuardLive wireGuardLive = new WireGuardLive(new File("configs")); private final WireGuardLive wireGuardLive;
public PacketHandler(WireGuardLive wireGuardLive) {
this.wireGuardLive = wireGuardLive;
}
void handle(ByteBuffer bytes) { void handle(ByteBuffer bytes) {
Packet<?> p; Packet<?> p;
@ -84,9 +89,19 @@ public class PacketHandler {
} }
private void handleVpnRequest(SessionRequestPacket packet) { private void handleVpnRequest(SessionRequestPacket packet) {
var session = new WireGuardSession(12345, "serverpoecjteta", "fefe:fefe::fefe", packet.linkLocal.toCompressedString(), packet.endpointHost + ":" + packet.endpointPort, packet.publicKey); var privateKey = WireGuardKeys.generatePrivateKey();
var publicKey = WireGuardKeys.derivePublicKey(privateKey);
// TODO fill port and link local
var session = new WireGuardSession(12345, privateKey, "fefe:fefe::fefe", packet.linkLocal.toCompressedString(), packet.endpointHost + ":" + packet.endpointPort, packet.publicKey);
try { try {
wireGuardLive.saveSession(packet.sessionId, session); wireGuardLive.saveSession(packet.sessionId, session);
System.out.printf("Created session #%d to %s\n", packet.sessionId, packet.endpointHost);
Packets.send(new SessionResponsePacket(packet.sessionId, SessionResponsePacket.SessionResult.OK, session.listenPort(), publicKey), sender);
} catch (FileAlreadyExistsException e) {
System.err.println("Tried to create a session which already exists: #" + packet.sessionId);
Packets.send(new SessionResponsePacket(packet.sessionId, SessionResponsePacket.SessionResult.ERROR, -1, null), sender);
} catch (IOException e) { } catch (IOException e) {
Packets.send(new SessionResponsePacket(packet.sessionId, SessionResponsePacket.SessionResult.ERROR, -1, null), sender); Packets.send(new SessionResponsePacket(packet.sessionId, SessionResponsePacket.SessionResult.ERROR, -1, null), sender);
throw new RuntimeException(e); throw new RuntimeException(e);

View file

@ -2,27 +2,30 @@ package eu.m724.autopeerer.client.wireguard;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
public class WireGuardLive { public class WireGuardLive {
private final File configsPath; private final File configsPath;
public WireGuardLive(File configsPath) { public WireGuardLive(File configsPath) {
System.out.println(configsPath.getAbsolutePath());
this.configsPath = configsPath; this.configsPath = configsPath;
} }
public void saveSession(int connectionId, WireGuardSession session) throws IOException { public void saveSession(int sessionId, WireGuardSession session) throws IOException {
File file = new File(configsPath, "ap_" + connectionId + ".conf"); File file = new File(configsPath, "ap_" + sessionId + ".conf");
file.createNewFile(); Files.writeString(file.toPath(), session.config(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
try (FileWriter writer = new FileWriter(file)) {
writer.write(session.config());
}
} }
public WireGuardSession getSession(int connectionId) throws IOException { public WireGuardSession getSession(int sessionId) throws IOException {
File file = new File(configsPath, "ap_" + connectionId + ".conf"); File file = new File(configsPath, "ap_" + sessionId + ".conf");
String s = Files.readString(file.toPath()); String s = Files.readString(file.toPath());
return WireGuardSession.fromString(s); return WireGuardSession.fromString(s);
} }
public boolean existsSession(int sessionId) {
File file = new File(configsPath, "ap_" + sessionId + ".conf");
return file.exists();
}
} }

View file

@ -1,11 +1,11 @@
package eu.m724.autopeerer.common.packet; package eu.m724.autopeerer.common.packet;
import eu.m724.autopeerer.common.packet.client.LoginPacket; import eu.m724.autopeerer.common.packet.c2s.LoginPacket;
import eu.m724.autopeerer.common.packet.client.PingRequestPacket; import eu.m724.autopeerer.common.packet.c2s.PingRequestPacket;
import eu.m724.autopeerer.common.packet.client.SessionRequestPacket; import eu.m724.autopeerer.common.packet.c2s.SessionRequestPacket;
import eu.m724.autopeerer.common.packet.server.LoginResponsePacket; import eu.m724.autopeerer.common.packet.s2c.LoginResponsePacket;
import eu.m724.autopeerer.common.packet.server.PingResponsePacket; import eu.m724.autopeerer.common.packet.s2c.PingResponsePacket;
import eu.m724.autopeerer.common.packet.server.SessionResponsePacket; import eu.m724.autopeerer.common.packet.s2c.SessionResponsePacket;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.function.Consumer; import java.util.function.Consumer;

View file

@ -1,4 +1,4 @@
package eu.m724.autopeerer.common.packet.client; package eu.m724.autopeerer.common.packet.c2s;
import eu.m724.autopeerer.common.packet.Packet; import eu.m724.autopeerer.common.packet.Packet;

View file

@ -1,4 +1,4 @@
package eu.m724.autopeerer.common.packet.client; package eu.m724.autopeerer.common.packet.c2s;
import eu.m724.autopeerer.common.packet.Packet; import eu.m724.autopeerer.common.packet.Packet;

View file

@ -1,4 +1,4 @@
package eu.m724.autopeerer.common.packet.client; package eu.m724.autopeerer.common.packet.c2s;
import eu.m724.autopeerer.common.packet.Packet; import eu.m724.autopeerer.common.packet.Packet;
import inet.ipaddr.HostName; import inet.ipaddr.HostName;
@ -17,8 +17,9 @@ public class SessionRequestPacket implements Packet<SessionRequestPacket> {
public final String publicKey; public final String publicKey;
public final String endpointHost; public final String endpointHost;
public final int endpointPort; public final int endpointPort;
public final long asn;
public SessionRequestPacket(short sessionId, IPv6Address linkLocal, String publicKey, String endpointHost, int endpointPort) { public SessionRequestPacket(short sessionId, IPv6Address linkLocal, String publicKey, String endpointHost, int endpointPort, long asn) {
this.sessionId = sessionId; this.sessionId = sessionId;
this.linkLocal = linkLocal; this.linkLocal = linkLocal;
@ -36,6 +37,9 @@ public class SessionRequestPacket implements Packet<SessionRequestPacket> {
this.endpointPort = endpointPort; this.endpointPort = endpointPort;
assert endpointPort > 0 && endpointPort < 65536; assert endpointPort > 0 && endpointPort < 65536;
this.asn = asn;
assert asn < 0xFFFFFFFFL;
} }
@Override @Override
@ -54,26 +58,29 @@ public class SessionRequestPacket implements Packet<SessionRequestPacket> {
buffer.get(pk); buffer.get(pk);
var publicKey = Base64.getEncoder().encodeToString(pk); var publicKey = Base64.getEncoder().encodeToString(pk);
var endpointPort = buffer.getShort(); var endpointPort = buffer.getShort() & 0xFFFF;
var epl = buffer.get() & 0xFF; var epl = buffer.get() & 0xFF;
var ep = new byte[epl]; var ep = new byte[epl];
buffer.get(ep); buffer.get(ep);
var endpointHost = new String(ep, StandardCharsets.US_ASCII); var endpointHost = new String(ep, StandardCharsets.US_ASCII);
return new SessionRequestPacket(id, linkLocal, publicKey, endpointHost, endpointPort); var asn = Integer.toUnsignedLong(buffer.getInt());
return new SessionRequestPacket(id, linkLocal, publicKey, endpointHost, endpointPort, asn);
} }
@Override @Override
public ByteBuffer serialize() { public ByteBuffer serialize() {
var buffer = ByteBuffer.allocate(53 + endpointHost.length()); var buffer = ByteBuffer.allocate(57 + endpointHost.length());
buffer.putShort(sessionId); // 2b buffer.putShort(sessionId); // 2b
buffer.put(linkLocal.getBytes()); // 16b buffer.put(linkLocal.getBytes()); // 16b
buffer.put(Base64.getDecoder().decode(publicKey)); // 32b buffer.put(Base64.getDecoder().decode(publicKey)); // 32b
buffer.putShort((short)endpointPort); // 2b buffer.putShort((short) endpointPort); // 2b
buffer.put((byte) endpointHost.length()); // 1b buffer.put((byte) endpointHost.length()); // 1b
buffer.put(endpointHost.getBytes(StandardCharsets.US_ASCII)); buffer.put(endpointHost.getBytes(StandardCharsets.US_ASCII));
buffer.putInt((int) asn);
return buffer; return buffer;
} }

View file

@ -1,4 +1,4 @@
package eu.m724.autopeerer.common.packet.server; package eu.m724.autopeerer.common.packet.s2c;
import eu.m724.autopeerer.common.packet.Packet; import eu.m724.autopeerer.common.packet.Packet;

View file

@ -1,4 +1,4 @@
package eu.m724.autopeerer.common.packet.server; package eu.m724.autopeerer.common.packet.s2c;
import eu.m724.autopeerer.common.packet.Packet; import eu.m724.autopeerer.common.packet.Packet;

View file

@ -1,4 +1,4 @@
package eu.m724.autopeerer.common.packet.server; package eu.m724.autopeerer.common.packet.s2c;
import eu.m724.autopeerer.common.packet.Packet; import eu.m724.autopeerer.common.packet.Packet;

View file

@ -5,28 +5,28 @@ import java.net.InetSocketAddress;
import java.util.Set; import java.util.Set;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) throws InterruptedException {
System.out.println("Hello world!"); System.out.println("Hello world!");
var config = new ServerConfiguration(); var config = new ServerConfiguration();
Set<Node> nodes; Set<Node> nodes;
try { try {
config.init(); config.load();
nodes = config.loadNodes(); nodes = config.loadNodes();
if (nodes.isEmpty()) if (nodes.isEmpty())
System.err.println("Loaded 0 nodes. No client will be able to connect."); System.err.println("Loaded 0 nodes. No client will be able to connect.");
else else
System.out.printf("Loaded %d nodes.\n", nodes.size()); System.out.printf("Loaded %d nodes.\n", nodes.size());
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Failed to init configuration", e); throw new RuntimeException("Failed to load configuration", e);
} }
var packetHandler = new PacketHandler(nodes); var packetHandler = new PacketHandler(nodes);
var client = new MyWebsocketServer( var server = new MyWebsocketServer(
new InetSocketAddress(config.getString("listen.address"), config.getInt("listen.port")), new InetSocketAddress(config.getString("listen.address"), config.getInt("listen.port")),
packetHandler packetHandler
); );
client.start(); server.start();
} }
} }

View file

@ -1,7 +1,7 @@
package eu.m724.autopeerer.server; package eu.m724.autopeerer.server;
import eu.m724.autopeerer.common.packet.client.PingRequestPacket; import eu.m724.autopeerer.common.packet.c2s.PingRequestPacket;
import eu.m724.autopeerer.common.packet.client.SessionRequestPacket; import eu.m724.autopeerer.common.packet.c2s.SessionRequestPacket;
import inet.ipaddr.IPAddressString; import inet.ipaddr.IPAddressString;
import org.java_websocket.WebSocket; import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.handshake.ClientHandshake;
@ -43,9 +43,10 @@ public class MyWebsocketServer extends WebSocketServer {
state.send(new SessionRequestPacket( state.send(new SessionRequestPacket(
(short) 1, (short) 1,
new IPAddressString("fe80::dead:fed").getAddress().toIPv6(), new IPAddressString("fe80::dead:fed").getAddress().toIPv6(),
"somepk", "IBusHriGmiJaqbp0IGfClDDHXcei8+JL1MIHjueheUw=",
"end.point", "end.point",
51820 51820,
4242420000L
)); ));
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
throw new RuntimeException(e); throw new RuntimeException(e);

View file

@ -2,9 +2,9 @@ package eu.m724.autopeerer.server;
import eu.m724.autopeerer.common.packet.Packet; import eu.m724.autopeerer.common.packet.Packet;
import eu.m724.autopeerer.common.packet.Packets; import eu.m724.autopeerer.common.packet.Packets;
import eu.m724.autopeerer.common.packet.client.LoginPacket; import eu.m724.autopeerer.common.packet.c2s.LoginPacket;
import eu.m724.autopeerer.common.packet.server.PingResponsePacket; import eu.m724.autopeerer.common.packet.s2c.PingResponsePacket;
import eu.m724.autopeerer.common.packet.server.SessionResponsePacket; import eu.m724.autopeerer.common.packet.s2c.SessionResponsePacket;
import java.nio.BufferUnderflowException; import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;

View file

@ -1,33 +1,18 @@
package eu.m724.autopeerer.server; package eu.m724.autopeerer.server;
import eu.m724.autopeerer.common.Configuration;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.HashSet; import java.util.HashSet;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
public class ServerConfiguration { public class ServerConfiguration extends Configuration {
private final File directory = new File("config"); public ServerConfiguration() {
private final File configFile = new File(directory, "server.properties"); super("server");
private final Properties properties = new Properties();
public void init() throws IOException {
directory.mkdir();
if (!configFile.exists()) {
try (var is = getClass().getClassLoader().getResourceAsStream("server.properties")) {
Files.write(configFile.toPath(), is.readAllBytes());
properties.load(is);
}
}
try (var is = new FileInputStream(configFile)) {
properties.load(is);
}
} }
public Set<Node> loadNodes() throws IOException { public Set<Node> loadNodes() throws IOException {
@ -50,20 +35,4 @@ public class ServerConfiguration {
return nodes; return nodes;
} }
public String getString(String property) {
return properties.getProperty(property);
}
public String getString(String property, String def) {
return properties.getProperty(property, def);
}
public int getInt(String property) {
return Integer.parseInt(getString(property));
}
public int getInt(String property, int def) {
return Integer.parseInt(getString(property, String.valueOf(def)));
}
} }

View file

@ -0,0 +1,2 @@
remote=ws://127.0.0.1:8002
wireguard.directory=config/wg