Pre-generate web editor keypair to speed up initial session init

This commit is contained in:
Luck 2022-02-09 20:08:16 +00:00
parent 0fe85ed6ff
commit b13a74c61e
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 18 additions and 5 deletions

View File

@ -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)

View File

@ -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> 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();
}
}