Skip to content

Commit cdc3b08

Browse files
authored
novnc: Accept new novnc client and disconnect old session (#4531)
* novnc: Reject new novnc client if novnc viewer object is still alive * #4531 novnc: Accept new novnc client and disconnect old session
1 parent 0a401eb commit cdc3b08

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

services/console-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxy.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,11 +551,23 @@ public static ConsoleProxyNoVncClient getNoVncViewer(ConsoleProxyClientParam par
551551
!param.getClientHostPassword().equals(viewer.getClientHostPassword()))
552552
throw new AuthenticationException("Cannot use the existing viewer " + viewer + ": bad sid");
553553

554-
if (!viewer.isFrontEndAlive()) {
554+
try {
555555
authenticationExternally(param);
556-
viewer.initClient(param);
557-
reportLoadChange = true;
556+
} catch (Exception e) {
557+
s_logger.error("Authencation failed for param: " + param);
558+
return null;
559+
}
560+
s_logger.info("Initializing new novnc client and disconnecting existing session");
561+
try {
562+
((ConsoleProxyNoVncClient)viewer).getSession().disconnect();
563+
} catch (IOException e) {
564+
s_logger.error("Exception while disconnect session of novnc viewer object: " + viewer, e);
558565
}
566+
removeViewer(viewer);
567+
viewer = new ConsoleProxyNoVncClient(session);
568+
viewer.initClient(param);
569+
connectionMap.put(clientKey, viewer);
570+
reportLoadChange = true;
559571
}
560572

561573
if (reportLoadChange) {

services/console-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyNoVNCHandler.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
@WebSocket
3939
public class ConsoleProxyNoVNCHandler extends WebSocketHandler {
4040

41-
private ConsoleProxyNoVncClient viewer;
41+
private ConsoleProxyNoVncClient viewer = null;
4242
private static final Logger s_logger = Logger.getLogger(ConsoleProxyNoVNCHandler.class);
4343

4444
public ConsoleProxyNoVNCHandler() {
@@ -130,12 +130,18 @@ public void onConnect(final Session session) throws IOException, InterruptedExce
130130
} catch (Exception e) {
131131
s_logger.warn("Failed to create viewer due to " + e.getMessage(), e);
132132
return;
133+
} finally {
134+
if (viewer == null) {
135+
session.disconnect();
136+
}
133137
}
134138
}
135139

136140
@OnWebSocketClose
137141
public void onClose(Session session, int statusCode, String reason) throws IOException, InterruptedException {
138-
ConsoleProxy.removeViewer(viewer);
142+
if (viewer != null) {
143+
ConsoleProxy.removeViewer(viewer);
144+
}
139145
}
140146

141147
@OnWebSocketFrame

services/console-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyNoVncClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,8 @@ public String getClientTag() {
235235
return "";
236236
}
237237

238+
public Session getSession() {
239+
return session;
240+
}
241+
238242
}

0 commit comments

Comments
 (0)