mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-06 13:17:41 +00:00
GUAC-1427: Do not invoke createTunnel() within WebSocket tunnels unless close() is guaranteed to eventually run.
This commit is contained in:
@@ -207,6 +207,10 @@ public abstract class GuacamoleWebSocketTunnelEndpoint extends Endpoint {
|
|||||||
@OnMessage
|
@OnMessage
|
||||||
public void onMessage(String message) {
|
public void onMessage(String message) {
|
||||||
|
|
||||||
|
// Ignore inbound messages if there is no associated tunnel
|
||||||
|
if (tunnel == null)
|
||||||
|
return;
|
||||||
|
|
||||||
GuacamoleWriter writer = tunnel.acquireWriter();
|
GuacamoleWriter writer = tunnel.acquireWriter();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@@ -31,6 +31,7 @@ import javax.websocket.server.HandshakeRequest;
|
|||||||
import javax.websocket.server.ServerEndpointConfig;
|
import javax.websocket.server.ServerEndpointConfig;
|
||||||
import org.glyptodon.guacamole.GuacamoleException;
|
import org.glyptodon.guacamole.GuacamoleException;
|
||||||
import org.glyptodon.guacamole.net.GuacamoleTunnel;
|
import org.glyptodon.guacamole.net.GuacamoleTunnel;
|
||||||
|
import org.glyptodon.guacamole.net.basic.TunnelRequest;
|
||||||
import org.glyptodon.guacamole.net.basic.TunnelRequestService;
|
import org.glyptodon.guacamole.net.basic.TunnelRequestService;
|
||||||
import org.glyptodon.guacamole.websocket.GuacamoleWebSocketTunnelEndpoint;
|
import org.glyptodon.guacamole.websocket.GuacamoleWebSocketTunnelEndpoint;
|
||||||
|
|
||||||
@@ -41,16 +42,16 @@ import org.glyptodon.guacamole.websocket.GuacamoleWebSocketTunnelEndpoint;
|
|||||||
public class BasicGuacamoleWebSocketTunnelEndpoint extends GuacamoleWebSocketTunnelEndpoint {
|
public class BasicGuacamoleWebSocketTunnelEndpoint extends GuacamoleWebSocketTunnelEndpoint {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unique string which shall be used to store the GuacamoleTunnel
|
* Unique string which shall be used to store the TunnelRequest
|
||||||
* associated with a WebSocket connection.
|
* associated with a WebSocket connection.
|
||||||
*/
|
*/
|
||||||
private static final String TUNNEL_USER_PROPERTY = "WS_GUAC_TUNNEL";
|
private static final String TUNNEL_REQUEST_PROPERTY = "WS_GUAC_TUNNEL_REQUEST";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unique string which shall be used to store any GuacamoleException that
|
* Unique string which shall be used to store the TunnelRequestService to
|
||||||
* occurs while retrieving the tunnel during the handshake.
|
* be used for processing TunnelRequests.
|
||||||
*/
|
*/
|
||||||
private static final String ERROR_USER_PROPERTY = "WS_GUAC_TUNNEL_ERROR";
|
private static final String TUNNEL_REQUEST_SERVICE_PROPERTY = "WS_GUAC_TUNNEL_REQUEST_SERVICE";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configurator implementation which stores the requested GuacamoleTunnel
|
* Configurator implementation which stores the requested GuacamoleTunnel
|
||||||
@@ -70,8 +71,8 @@ public class BasicGuacamoleWebSocketTunnelEndpoint extends GuacamoleWebSocketTun
|
|||||||
* service provider to retrieve the necessary service to handle new
|
* service provider to retrieve the necessary service to handle new
|
||||||
* connections requests.
|
* connections requests.
|
||||||
*
|
*
|
||||||
* @param tunnelRequestServiceProvider The tunnel request service
|
* @param tunnelRequestServiceProvider
|
||||||
* provider to use for all new
|
* The tunnel request service provider to use for all new
|
||||||
* connections.
|
* connections.
|
||||||
*/
|
*/
|
||||||
public Configurator(Provider<TunnelRequestService> tunnelRequestServiceProvider) {
|
public Configurator(Provider<TunnelRequestService> tunnelRequestServiceProvider) {
|
||||||
@@ -79,43 +80,40 @@ public class BasicGuacamoleWebSocketTunnelEndpoint extends GuacamoleWebSocketTun
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) {
|
public void modifyHandshake(ServerEndpointConfig config,
|
||||||
|
HandshakeRequest request, HandshakeResponse response) {
|
||||||
|
|
||||||
super.modifyHandshake(config, request, response);
|
super.modifyHandshake(config, request, response);
|
||||||
|
|
||||||
// Attempt tunnel creation
|
// Store tunnel request and tunnel request service for retrieval
|
||||||
|
// upon WebSocket open
|
||||||
Map<String, Object> userProperties = config.getUserProperties();
|
Map<String, Object> userProperties = config.getUserProperties();
|
||||||
userProperties.clear();
|
userProperties.clear();
|
||||||
try {
|
userProperties.put(TUNNEL_REQUEST_PROPERTY, new WebSocketTunnelRequest(request));
|
||||||
|
userProperties.put(TUNNEL_REQUEST_SERVICE_PROPERTY, tunnelRequestServiceProvider.get());
|
||||||
// Get tunnel request service
|
|
||||||
TunnelRequestService tunnelRequestService = tunnelRequestServiceProvider.get();
|
|
||||||
|
|
||||||
// Store new tunnel within user properties
|
|
||||||
GuacamoleTunnel tunnel = tunnelRequestService.createTunnel(new WebSocketTunnelRequest(request));
|
|
||||||
if (tunnel != null)
|
|
||||||
userProperties.put(TUNNEL_USER_PROPERTY, tunnel);
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (GuacamoleException e) {
|
|
||||||
userProperties.put(ERROR_USER_PROPERTY, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected GuacamoleTunnel createTunnel(Session session, EndpointConfig config) throws GuacamoleException {
|
protected GuacamoleTunnel createTunnel(Session session,
|
||||||
|
EndpointConfig config) throws GuacamoleException {
|
||||||
|
|
||||||
// Throw any error that occurred during tunnel creation
|
|
||||||
Map<String, Object> userProperties = config.getUserProperties();
|
Map<String, Object> userProperties = config.getUserProperties();
|
||||||
GuacamoleException tunnelError = (GuacamoleException) userProperties.get(ERROR_USER_PROPERTY);
|
|
||||||
if (tunnelError != null)
|
|
||||||
throw tunnelError;
|
|
||||||
|
|
||||||
// Return created tunnel, if any
|
// Get original tunnel request
|
||||||
return (GuacamoleTunnel) userProperties.get(TUNNEL_USER_PROPERTY);
|
TunnelRequest tunnelRequest = (TunnelRequest) userProperties.get(TUNNEL_REQUEST_PROPERTY);
|
||||||
|
if (tunnelRequest == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Get tunnel request service
|
||||||
|
TunnelRequestService tunnelRequestService = (TunnelRequestService) userProperties.get(TUNNEL_REQUEST_SERVICE_PROPERTY);
|
||||||
|
if (tunnelRequestService == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Create and return tunnel
|
||||||
|
return tunnelRequestService.createTunnel(tunnelRequest);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -70,25 +70,24 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
|
public WebSocket doWebSocketConnect(final HttpServletRequest request, String protocol) {
|
||||||
|
|
||||||
// Get tunnel
|
|
||||||
final GuacamoleTunnel tunnel;
|
|
||||||
|
|
||||||
try {
|
|
||||||
tunnel = doConnect(request);
|
|
||||||
}
|
|
||||||
catch (GuacamoleException e) {
|
|
||||||
logger.error("Creation of WebSocket tunnel to guacd failed: {}", e.getMessage());
|
|
||||||
logger.debug("Error connecting WebSocket tunnel.", e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return new WebSocket which communicates through tunnel
|
// Return new WebSocket which communicates through tunnel
|
||||||
return new WebSocket.OnTextMessage() {
|
return new WebSocket.OnTextMessage() {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The GuacamoleTunnel associated with the connected WebSocket. If
|
||||||
|
* the WebSocket has not yet been connected, this will be null.
|
||||||
|
*/
|
||||||
|
private GuacamoleTunnel tunnel = null;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMessage(String string) {
|
public void onMessage(String string) {
|
||||||
|
|
||||||
|
// Ignore inbound messages if there is no associated tunnel
|
||||||
|
if (tunnel == null)
|
||||||
|
return;
|
||||||
|
|
||||||
GuacamoleWriter writer = tunnel.acquireWriter();
|
GuacamoleWriter writer = tunnel.acquireWriter();
|
||||||
|
|
||||||
// Write message received
|
// Write message received
|
||||||
@@ -103,11 +102,22 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tunnel.releaseWriter();
|
tunnel.releaseWriter();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onOpen(final Connection connection) {
|
public void onOpen(final Connection connection) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
tunnel = doConnect(request);
|
||||||
|
}
|
||||||
|
catch (GuacamoleException e) {
|
||||||
|
logger.error("Creation of WebSocket tunnel to guacd failed: {}", e.getMessage());
|
||||||
|
logger.debug("Error connecting WebSocket tunnel.", e);
|
||||||
|
closeConnection(connection, e.getStatus());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Do not start connection if tunnel does not exist
|
// Do not start connection if tunnel does not exist
|
||||||
if (tunnel == null) {
|
if (tunnel == null) {
|
||||||
closeConnection(connection, GuacamoleStatus.RESOURCE_NOT_FOUND);
|
closeConnection(connection, GuacamoleStatus.RESOURCE_NOT_FOUND);
|
||||||
|
@@ -185,6 +185,10 @@ public abstract class GuacamoleWebSocketTunnelListener implements WebSocketListe
|
|||||||
@Override
|
@Override
|
||||||
public void onWebSocketText(String message) {
|
public void onWebSocketText(String message) {
|
||||||
|
|
||||||
|
// Ignore inbound messages if there is no associated tunnel
|
||||||
|
if (tunnel == null)
|
||||||
|
return;
|
||||||
|
|
||||||
GuacamoleWriter writer = tunnel.acquireWriter();
|
GuacamoleWriter writer = tunnel.acquireWriter();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@@ -92,26 +92,25 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StreamInbound createWebSocketInbound(String protocol, HttpServletRequest request) {
|
public StreamInbound createWebSocketInbound(String protocol,
|
||||||
|
final HttpServletRequest request) {
|
||||||
// Get tunnel
|
|
||||||
final GuacamoleTunnel tunnel;
|
|
||||||
|
|
||||||
try {
|
|
||||||
tunnel = doConnect(request);
|
|
||||||
}
|
|
||||||
catch (GuacamoleException e) {
|
|
||||||
logger.error("Creation of WebSocket tunnel to guacd failed: {}", e.getMessage());
|
|
||||||
logger.debug("Error connecting WebSocket tunnel.", e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return new WebSocket which communicates through tunnel
|
// Return new WebSocket which communicates through tunnel
|
||||||
return new StreamInbound() {
|
return new StreamInbound() {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The GuacamoleTunnel associated with the connected WebSocket. If
|
||||||
|
* the WebSocket has not yet been connected, this will be null.
|
||||||
|
*/
|
||||||
|
private GuacamoleTunnel tunnel = null;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onTextData(Reader reader) throws IOException {
|
protected void onTextData(Reader reader) throws IOException {
|
||||||
|
|
||||||
|
// Ignore inbound messages if there is no associated tunnel
|
||||||
|
if (tunnel == null)
|
||||||
|
return;
|
||||||
|
|
||||||
GuacamoleWriter writer = tunnel.acquireWriter();
|
GuacamoleWriter writer = tunnel.acquireWriter();
|
||||||
|
|
||||||
// Write all available data
|
// Write all available data
|
||||||
@@ -137,6 +136,16 @@ public abstract class GuacamoleWebSocketTunnelServlet extends WebSocketServlet {
|
|||||||
@Override
|
@Override
|
||||||
public void onOpen(final WsOutbound outbound) {
|
public void onOpen(final WsOutbound outbound) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
tunnel = doConnect(request);
|
||||||
|
}
|
||||||
|
catch (GuacamoleException e) {
|
||||||
|
logger.error("Creation of WebSocket tunnel to guacd failed: {}", e.getMessage());
|
||||||
|
logger.debug("Error connecting WebSocket tunnel.", e);
|
||||||
|
closeConnection(outbound, e.getStatus());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Do not start connection if tunnel does not exist
|
// Do not start connection if tunnel does not exist
|
||||||
if (tunnel == null) {
|
if (tunnel == null) {
|
||||||
closeConnection(outbound, GuacamoleStatus.RESOURCE_NOT_FOUND);
|
closeConnection(outbound, GuacamoleStatus.RESOURCE_NOT_FOUND);
|
||||||
|
Reference in New Issue
Block a user