From 7a3211b5ccc127db6bb7c2032cd6937d8fb5dce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=83zvan=20Purdel?= <104128753+rpurdel@users.noreply.github.com> Date: Sat, 18 May 2024 19:19:41 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20Fixes=20leak=20and=20IllegalMonitorState?= =?UTF-8?q?Exception,=20move=20jwt=20to=20Authorization=E2=80=A6=20(#538)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix leak, fix IllegalMonitorStateException, move jwt to Authorization header * simpler connection --- .../transcription/WhisperWebsocket.java | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/jitsi/jigasi/transcription/WhisperWebsocket.java b/src/main/java/org/jitsi/jigasi/transcription/WhisperWebsocket.java index 880aa6e5..69a75801 100644 --- a/src/main/java/org/jitsi/jigasi/transcription/WhisperWebsocket.java +++ b/src/main/java/org/jitsi/jigasi/transcription/WhisperWebsocket.java @@ -107,6 +107,8 @@ public class WhisperWebsocket private final static String jwtAudience; + private WebSocketClient ws; + static { jwtAudience = JigasiBundleActivator.getConfigurationService() @@ -135,11 +137,11 @@ public class WhisperWebsocket logger.info("Websocket transcription streaming endpoint: " + websocketUrlConfig); } - private String getJWT() throws NoSuchAlgorithmException, InvalidKeySpecException + private String getJWT() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { if (privateKey.isEmpty() || privateKeyName.isEmpty()) { - return null; + throw new IOException("Failed generating JWT for Whisper. Missing private key or key name."); } long nowMillis = System.currentTimeMillis(); Date now = new Date(nowMillis); @@ -164,15 +166,7 @@ private String getJWT() throws NoSuchAlgorithmException, InvalidKeySpecException */ private void generateWebsocketUrl() { - try - { - websocketUrl = websocketUrlConfig + "/" + connectionId + "?auth_token=" + getJWT(); - } - catch (Exception e) - { - Statistics.incrementTotalTranscriberConnectionErrors(); - logger.error("Failed generating JWT for Whisper. " + e); - } + websocketUrl = websocketUrlConfig + "/" + connectionId; if (logger.isDebugEnabled()) { logger.debug("Whisper URL: " + websocketUrl); @@ -197,10 +191,11 @@ void connect() { generateWebsocketUrl(); logger.info("Connecting to " + websocketUrl); - WebSocketClient ws = new WebSocketClient(); + ClientUpgradeRequest upgradeRequest = new ClientUpgradeRequest(); + upgradeRequest.setHeader("Authorization", "Bearer " + getJWT()); + ws = new WebSocketClient(); ws.start(); - CompletableFuture connectFuture = ws.connect(this, new URI(websocketUrl)); - wsSession = connectFuture.get(); + wsSession = ws.connect(this, new URI(websocketUrl), upgradeRequest).get(); wsSession.setIdleTimeout(Duration.ofSeconds(300)); isConnected = true; logger.info("Successfully connected to " + websocketUrl); @@ -216,11 +211,14 @@ void connect() logger.error(e.toString()); } attempt++; - try + synchronized (this) { - wait(waitTime); + try + { + wait(waitTime); + } + catch (InterruptedException ignored) {} } - catch (InterruptedException ignored) {} } if (!isConnected) @@ -236,6 +234,18 @@ public void onClose(int statusCode, String reason) wsSession = null; participants = null; participantListeners = null; + try + { + if (ws != null) + { + ws.stop(); + ws = null; + } + } + catch (Exception e) + { + logger.error("Error while stopping WebSocketClient", e); + } }