From b13a74c61edba0968831f8359b1ff73d82f5c199 Mon Sep 17 00:00:00 2001 From: Luck Date: Wed, 9 Feb 2022 20:08:16 +0000 Subject: [PATCH] Pre-generate web editor keypair to speed up initial session init --- .../common/webeditor/socket/WebEditorSocket.java | 10 +++++----- .../common/webeditor/store/WebEditorStore.java | 13 +++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/webeditor/socket/WebEditorSocket.java b/common/src/main/java/me/lucko/luckperms/common/webeditor/socket/WebEditorSocket.java index aaed889e4..74c82f17a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/webeditor/socket/WebEditorSocket.java +++ b/common/src/main/java/me/lucko/luckperms/common/webeditor/socket/WebEditorSocket.java @@ -58,11 +58,11 @@ public class WebEditorSocket { private final WebEditorSession session; /** The socket listener that handles incoming messages */ private final WebEditorSocketListener listener; + /** The public and private keys used to sign messages sent by the plugin */ + private final KeyPair pluginKeyPair; /** The websocket backing the connection */ private BytesocksClient.Socket socket; - /** The public and private keys used to sign messages sent by the plugin */ - private KeyPair localKeys; /** A task to check if the socket is still active */ private SchedulerTask keepaliveTask; /** The public key used by the editor to sign messages */ @@ -75,6 +75,7 @@ public class WebEditorSocket { this.sender = sender; this.session = session; this.listener = new WebEditorSocketListener(this); + this.pluginKeyPair = plugin.getWebEditorStore().keyPair(); } /** @@ -86,7 +87,6 @@ public class WebEditorSocket { */ public void initialize(BytesocksClient client) throws UnsuccessfulRequestException, IOException { this.socket = client.createSocket(this.listener); - this.localKeys = CryptographyUtils.generateKeyPair(); } /** @@ -112,7 +112,7 @@ public class WebEditorSocket { */ public void appendDetailToRequest(WebEditorRequest request) { String channelId = this.socket.channelId(); - String publicKey = Base64.getEncoder().encodeToString(this.localKeys.getPublic().getEncoded()); + String publicKey = Base64.getEncoder().encodeToString(this.pluginKeyPair.getPublic().getEncoded()); JsonObject socket = new JsonObject(); socket.addProperty("protocolVersion", PROTOCOL_VERSION); @@ -133,7 +133,7 @@ public class WebEditorSocket { */ public void send(JsonObject msg) { String encoded = GsonProvider.normal().toJson(msg); - String signature = CryptographyUtils.sign(this.localKeys.getPrivate(), encoded); + String signature = CryptographyUtils.sign(this.pluginKeyPair.getPrivate(), encoded); JsonObject frame = new JObject() .add("msg", encoded) diff --git a/common/src/main/java/me/lucko/luckperms/common/webeditor/store/WebEditorStore.java b/common/src/main/java/me/lucko/luckperms/common/webeditor/store/WebEditorStore.java index 82a7bd47a..26d28e846 100644 --- a/common/src/main/java/me/lucko/luckperms/common/webeditor/store/WebEditorStore.java +++ b/common/src/main/java/me/lucko/luckperms/common/webeditor/store/WebEditorStore.java @@ -26,6 +26,10 @@ package me.lucko.luckperms.common.webeditor.store; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.common.webeditor.socket.CryptographyUtils; + +import java.security.KeyPair; +import java.util.concurrent.CompletableFuture; /** * Contains a store of known web editor sessions and provides a lookup function for @@ -35,11 +39,13 @@ public class WebEditorStore { private final WebEditorSessionMap sessions; private final WebEditorSocketMap sockets; private final WebEditorKeystore keystore; + private final CompletableFuture keyPair; public WebEditorStore(LuckPermsPlugin plugin) { this.sessions = new WebEditorSessionMap(); this.sockets = new WebEditorSocketMap(); this.keystore = new WebEditorKeystore(plugin.getBootstrap().getConfigDirectory().resolve("editor-keystore.json")); + this.keyPair = CompletableFuture.supplyAsync(CryptographyUtils::generateKeyPair, plugin.getBootstrap().getScheduler().async()); } public WebEditorSessionMap sessions() { @@ -54,4 +60,11 @@ public class WebEditorStore { return this.keystore; } + public KeyPair keyPair() { + if (!this.keyPair.isDone()) { + throw new IllegalStateException("Web editor keypair has not been generated yet! Has the server just started?"); + } + return this.keyPair.join(); + } + }