com
All checks were successful
/ deploy (push) Successful in 26s

This commit is contained in:
Minecon724 2024-12-25 21:26:12 +01:00
parent 2ee1ff4ca4
commit 7bd35aab87
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
6 changed files with 105 additions and 2 deletions

5
README.md Normal file
View file

@ -0,0 +1,5 @@
# Setup
1. Get the `-server.jar` or `-client.jar` and make a new directory for it
2. Run the JAR, then stop it
3. Configure in `config`
4. Now run the JAR. Done!

View file

@ -18,6 +18,11 @@ public class Main {
throw new RuntimeException("Failed to load configuration", e); throw new RuntimeException("Failed to load configuration", e);
} }
if (config.isNew) {
System.out.println("The program stopped to let you configure");
return;
}
URI serverUri = URI.create(config.getString("remote")); URI serverUri = URI.create(config.getString("remote"));
var wireGuardLive = new WireGuardLive(new File(config.getString("wireguard.directory"))); var wireGuardLive = new WireGuardLive(new File(config.getString("wireguard.directory")));

View file

@ -7,6 +7,7 @@ import java.nio.file.Files;
import java.util.Properties; import java.util.Properties;
public class Configuration { public class Configuration {
public final boolean isNew;
public final File directory = new File("config"); public final File directory = new File("config");
private final File configFile; private final File configFile;
@ -14,10 +15,11 @@ public class Configuration {
public Configuration(String profile) { public Configuration(String profile) {
this.configFile = new File(directory, profile + ".properties"); this.configFile = new File(directory, profile + ".properties");
this.isNew = !configFile.exists();
} }
public void load() throws IOException { public boolean load() throws IOException {
directory.mkdir(); directory.mkdir();
if (!configFile.exists()) { if (!configFile.exists()) {
@ -25,11 +27,15 @@ public class Configuration {
Files.write(configFile.toPath(), is.readAllBytes()); Files.write(configFile.toPath(), is.readAllBytes());
properties.load(is); properties.load(is);
} }
return true;
} }
// this if already exists
try (var is = new FileInputStream(configFile)) { try (var is = new FileInputStream(configFile)) {
properties.load(is); properties.load(is);
} }
return false;
} }
public String getString(String property) { public String getString(String property) {

View file

@ -5,6 +5,8 @@ import eu.m724.autopeerer.common.packet.Packets;
import eu.m724.autopeerer.common.packet.c2s.PingResponsePacket; import eu.m724.autopeerer.common.packet.c2s.PingResponsePacket;
import eu.m724.autopeerer.common.packet.c2s.SessionResponsePacket; import eu.m724.autopeerer.common.packet.c2s.SessionResponsePacket;
import eu.m724.autopeerer.common.packet.s2c.PingRequestPacket; import eu.m724.autopeerer.common.packet.s2c.PingRequestPacket;
import eu.m724.autopeerer.common.packet.s2c.SessionRequestPacket;
import inet.ipaddr.ipv6.IPv6Address;
import org.java_websocket.WebSocket; import org.java_websocket.WebSocket;
import java.net.InetAddress; import java.net.InetAddress;
@ -50,10 +52,22 @@ public class ClientState {
return future; return future;
} }
CompletableFuture<SessionResponsePacket> session(long asn, IPv6Address linkLocal, String publicKey, String endpointHost, int endpointPort) {
var future = new CompletableFuture<SessionResponsePacket>();
sessionConsumers.put(asn, future::complete);
send(new SessionRequestPacket(
asn, linkLocal, publicKey, endpointHost, endpointPort
));
return future;
}
/* Packet functions */ /* Packet functions */
private Map<Short, Consumer<PingResponsePacket>> pingConsumers = new HashMap<>(); private Map<Short, Consumer<PingResponsePacket>> pingConsumers = new HashMap<>();
private Map<Long, Consumer<SessionResponsePacket>> sessionConsumers = new HashMap<>();
void onPacketReceived(Packet<?> p) { void onPacketReceived(Packet<?> p) {
@ -70,6 +84,7 @@ public class ClientState {
} }
private void handleSessionResponse(SessionResponsePacket packet) { private void handleSessionResponse(SessionResponsePacket packet) {
System.out.printf("[%d] Session response #%d: %s\n", clientId, packet.sessionId, packet.result); System.out.printf("[%d] Session response for AS%d: %s\n", clientId, packet.asn, packet.result);
sessionConsumers.remove(packet.asn).accept(packet);
} }
} }

View file

@ -23,6 +23,11 @@ public class Main {
throw new RuntimeException("Failed to load configuration", e); throw new RuntimeException("Failed to load configuration", e);
} }
if (config.isNew) {
System.out.println("The program stopped to let you configure");
return;
}
var packetHandler = new PacketHandler(nodes); var packetHandler = new PacketHandler(nodes);
var server = new MyWebsocketServer( var server = new MyWebsocketServer(
new InetSocketAddress(config.getString("socket.address"), config.getInt("socket.port")), new InetSocketAddress(config.getString("socket.address"), config.getInt("socket.port")),

View file

@ -3,6 +3,10 @@ package eu.m724.autopeerer.server;
import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpHandler;
import eu.m724.autopeerer.common.packet.c2s.PingResponsePacket; import eu.m724.autopeerer.common.packet.c2s.PingResponsePacket;
import eu.m724.autopeerer.common.packet.c2s.SessionResponsePacket;
import inet.ipaddr.IPAddress;
import inet.ipaddr.IPAddressString;
import inet.ipaddr.ipv6.IPv6Address;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -136,6 +140,69 @@ public class MyHttpHandler implements HttpHandler {
return null; return null;
}); });
} else if (path[0].equals("peer")) { } else if (path[0].equals("peer")) {
requireMethod(exchange, "POST");
var json = getJsonBody(exchange);
/* */
if (!json.has("node")) {
sendResponse(exchange, 400, "node");
}
var node = nodes.stream()
.filter(n -> n.id().equals(json.getString("node")))
.findFirst().orElse(null);
if (node == null) {
sendResponse(exchange, 400, "node");
}
/* */
CompletableFuture<SessionResponsePacket> future = null;
try {
future = node.getClient().session(
json.getLong("asn"),
new IPAddressString(json.getString("linkLocal")).getAddress().toIPv6(),
json.getString("publicKey"),
json.getString("endpointHost"),
json.getInt("endpointPort")
);
} catch (Exception e) {
sendResponse(exchange, 400, "params");
}
sseStart(exchange);
future.handle((packet, ex) -> {
try {
if (ex != null) {
sseWrite(exchange, ex.getMessage());
} else {
var resp = new JSONObject()
.put("result", packet.result);
if (packet.result == SessionResponsePacket.SessionResult.OK) {
resp.put("port", packet.port)
.put("publicKey", packet.publicKey);
}
sseWrite(exchange, resp.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
try {
sseClose(exchange);
} catch (IOException e) {
e.printStackTrace();
}
return null;
});
} }
} }