Skip to content

Commit 19814b0

Browse files
committed
console-proxy: close session if unable to connect to VNC server
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent f82ee7d commit 19814b0

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ public void run() {
109109
String tunnelSession = param.getClientTunnelSession();
110110
String websocketUrl = param.getWebsocketUrl();
111111

112-
connectClientToVNCServer(tunnelUrl, tunnelSession, websocketUrl);
112+
if (!connectClientToVNCServer(tunnelUrl, tunnelSession, websocketUrl)) {
113+
logger.error("Failed to connect to VNC server, will close connection with client [{}] [IP: {}].", clientId, clientSourceIp);
114+
connectionAlive = false;
115+
session.close();
116+
return;
117+
}
113118
authenticateToVNCServer(clientSourceIp);
114119

115120
// Track consecutive iterations with no data and sleep accordingly. Only used for NIO socket connections.
@@ -313,7 +318,7 @@ protected static byte[] rewriteServerNameInServerInit(byte[] serverInitBytes, St
313318
* - When websocketUrl is not empty -> connect to websocket
314319
* - Otherwise -> connect to TCP port on host directly
315320
*/
316-
private void connectClientToVNCServer(String tunnelUrl, String tunnelSession, String websocketUrl) {
321+
private boolean connectClientToVNCServer(String tunnelUrl, String tunnelSession, String websocketUrl) {
317322
try {
318323
if (StringUtils.isNotBlank(websocketUrl)) {
319324
logger.info(String.format("Connect to VNC over websocket URL: %s", websocketUrl));
@@ -337,7 +342,9 @@ private void connectClientToVNCServer(String tunnelUrl, String tunnelSession, St
337342
logger.info("Connection to VNC server has been established successfully.");
338343
} catch (Throwable e) {
339344
logger.error("Unexpected exception while connecting to VNC server.", e);
345+
return false;
340346
}
347+
return true;
341348
}
342349

343350
private void setClientParam(ConsoleProxyClientParam param) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,16 @@ public void connectTo(String host, int port, String path, String session, boolea
8888
setTunnelSocketStreams();
8989
}
9090

91-
public void connectTo(String host, int port) {
91+
public void connectTo(String host, int port) throws IOException {
9292
// Connect to server
9393
logger.info("Connecting to VNC server {}:{} ...", host, port);
9494
try {
9595
NioSocket nioSocket = new NioSocket(host, port);
9696
this.nioSocketConnection = new NioSocketHandlerImpl(nioSocket);
97-
} catch (Exception e) {
97+
} catch (IOException e) {
9898
logger.error(String.format("Cannot create socket to host: %s and port %s: %s", host, port,
9999
e.getMessage()), e);
100+
throw e;
100101
}
101102
}
102103

services/console-proxy/server/src/main/java/com/cloud/consoleproxy/vnc/network/NioSocket.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class NioSocket {
3636
private static final int CONNECTION_TIMEOUT_MILLIS = 3000;
3737
protected Logger logger = LogManager.getLogger(getClass());
3838

39-
private void initializeSocket() {
39+
private void initializeSocket() throws IOException {
4040
try {
4141
socketChannel = SocketChannel.open();
4242
socketChannel.configureBlocking(false);
@@ -49,42 +49,40 @@ private void initializeSocket() {
4949
socketChannel.register(readSelector, SelectionKey.OP_READ);
5050
} catch (IOException e) {
5151
logger.error("Could not initialize NioSocket: " + e.getMessage(), e);
52+
throw e;
5253
}
5354
}
5455

55-
private void waitForSocketSelectorConnected(Selector selector) {
56-
try {
57-
while (selector.select(CONNECTION_TIMEOUT_MILLIS) <= 0) {
58-
logger.debug("Waiting for ready operations to connect to the socket");
59-
}
60-
Set<SelectionKey> keys = selector.selectedKeys();
61-
for (SelectionKey selectionKey: keys) {
62-
if (selectionKey.isConnectable()) {
63-
if (socketChannel.isConnectionPending()) {
64-
socketChannel.finishConnect();
65-
}
66-
logger.debug("Connected to the socket");
67-
break;
56+
private void waitForSocketSelectorConnected(Selector selector) throws IOException {
57+
while (selector.select(CONNECTION_TIMEOUT_MILLIS) <= 0) {
58+
logger.debug("Waiting for ready operations to connect to the socket");
59+
}
60+
Set<SelectionKey> keys = selector.selectedKeys();
61+
for (SelectionKey selectionKey: keys) {
62+
if (selectionKey.isConnectable()) {
63+
if (socketChannel.isConnectionPending()) {
64+
socketChannel.finishConnect();
6865
}
66+
logger.debug("Connected to the socket");
67+
break;
6968
}
70-
} catch (IOException e) {
71-
logger.error(String.format("Error waiting for socket selector ready: %s", e.getMessage()), e);
7269
}
7370
}
7471

75-
private void connectSocket(String host, int port) {
72+
private void connectSocket(String host, int port) throws IOException {
7673
try {
7774
socketChannel.connect(new InetSocketAddress(host, port));
7875
Selector selector = Selector.open();
7976
socketChannel.register(selector, SelectionKey.OP_CONNECT);
8077

8178
waitForSocketSelectorConnected(selector);
8279
} catch (IOException e) {
83-
logger.error(String.format("Error creating NioSocket to %s:%s: %s", host, port, e.getMessage()), e);
80+
logger.error("Error connecting NioSocket to {}:{}: {}", host, port, e.getMessage(), e);
81+
throw e;
8482
}
8583
}
8684

87-
public NioSocket(String host, int port) {
85+
public NioSocket(String host, int port) throws IOException {
8886
initializeSocket();
8987
connectSocket(host, port);
9088
}

0 commit comments

Comments
 (0)