Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -83,8 +82,10 @@ public class ConsoleProxy {
static String encryptorPassword = "Dummy";
static final String[] skipProperties = new String[]{"certificate", "cacertificate", "keystore_password", "privatekey"};

static Set<String> allowedSessions = new HashSet<>();
static Set<String> allowedSessions = ConcurrentHashMap.newKeySet();
private static final Object allowedSessionsLock = new Object();

// Invoked through reflection
public static void addAllowedSession(String sessionUuid) {
allowedSessions.add(sessionUuid);
}
Expand Down Expand Up @@ -209,13 +210,15 @@ public static ConsoleProxyAuthenticationResult authenticateConsoleAccess(Console
}

String sessionUuid = param.getSessionUuid();
if (allowedSessions.contains(sessionUuid)) {
LOGGER.debug("Acquiring the session " + sessionUuid + " not available for future use");
allowedSessions.remove(sessionUuid);
} else {
LOGGER.info("Session " + sessionUuid + " has already been used, cannot connect");
authResult.setSuccess(false);
return authResult;
synchronized (allowedSessionsLock) {
if (allowedSessions.contains(sessionUuid)) {
LOGGER.debug("Acquiring the session " + sessionUuid + " not available for future use");
allowedSessions.remove(sessionUuid);
} else {
LOGGER.info("Session " + sessionUuid + " has already been used, cannot connect");
authResult.setSuccess(false);
return authResult;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
Comment on lines +213 to 222

String websocketUrl = param.getWebsocketUrl();
Expand Down Expand Up @@ -625,7 +628,7 @@ public static ConsoleProxyNoVncClient getNoVncViewer(ConsoleProxyClientParam par
} catch (IOException e) {
LOGGER.error("Exception while disconnect session of novnc viewer object: " + viewer, e);
}
removeViewer(viewer);
viewer.closeClient();
viewer = new ConsoleProxyNoVncClient(session);
viewer.initClient(param);
connectionMap.put(clientKey, viewer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,20 @@ private boolean checkSessionSourceIp(final Session session, final String sourceI

@OnWebSocketClose
public void onClose(Session session, int statusCode, String reason) throws IOException, InterruptedException {
String sessionSourceIp = session.getRemoteAddress().getAddress().getHostAddress();
logger.debug("Closing WebSocket session [source IP: {}, status code: {}].", sessionSourceIp, statusCode);
if (viewer != null) {
ConsoleProxy.removeViewer(viewer);
viewer.closeClient();
}
String sessionSourceIp = getRemoteAddressSafely(session);
logger.debug("WebSocket session [source IP: {}, status code: {}, reason: {}] closed successfully.", sessionSourceIp, statusCode, reason);
}

private String getRemoteAddressSafely(Session session) {
try {
return session.getRemoteAddress().getAddress().getHostAddress();
} catch (Exception e) {
logger.debug("Failed to get remote address from WebSocket session", e);
return "unknown";
}
logger.debug("WebSocket session [source IP: {}, status code: {}] closed successfully.", sessionSourceIp, statusCode);
}

@OnWebSocketFrame
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ public void closeClient() {
this.connectionAlive = false;
// Clear buffer reference to allow GC when client disconnects
this.readBuffer = null;
if (client != null) {
client.close();
}
ConsoleProxy.removeViewer(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@ public void proxyMsgOverWebSocketConnection(ByteBuffer msg) {
}
}

public void close() {
if (nioSocketConnection != null) {
nioSocketConnection.close();
}
if (webSocketReverseProxy != null) {
webSocketReverseProxy.close();
}
if (socket != null) {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
socket.close();
} catch (IOException e) {
logger.debug("Error closing socket: " + e.getMessage(), e);
}
}
}
Comment on lines +132 to +152

private void setTunnelSocketStreams() throws IOException {
this.is = new DataInputStream(this.socket.getInputStream());
this.os = new DataOutputStream(this.socket.getOutputStream());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,28 @@ protected int writeToSocketChannel(ByteBuffer buf, int len) {
return 0;
}
}

public void close() {
try {
if (socketChannel != null) {
socketChannel.close();
}
} catch (IOException e) {
logger.debug("Error closing socket channel: " + e.getMessage(), e);
}
try {
if (readSelector != null) {
readSelector.close();
}
} catch (IOException e) {
logger.debug("Error closing read selector: " + e.getMessage(), e);
}
try {
if (writeSelector != null) {
writeSelector.close();
}
} catch (IOException e) {
logger.debug("Error closing write selector: " + e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ public interface NioSocketHandler {
void flushWriteBuffer();
void startTLSConnection(NioSocketSSLEngineManager sslEngineManager);
boolean isTLSConnection();
void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ public class NioSocketHandlerImpl implements NioSocketHandler {
private NioSocketInputStream inputStream;
private NioSocketOutputStream outputStream;
private boolean isTLS = false;
private final NioSocket socket;

protected Logger logger = LogManager.getLogger(getClass());

public NioSocketHandlerImpl(NioSocket socket) {
this.socket = socket;
this.inputStream = new NioSocketInputStream(ConsoleProxy.defaultBufferSize, socket);
this.outputStream = new NioSocketOutputStream(ConsoleProxy.defaultBufferSize, socket);
}
Expand Down Expand Up @@ -109,4 +111,9 @@ public NioSocketInputStream getInputStream() {
public NioSocketOutputStream getOutputStream() {
return outputStream;
}

@Override
public void close() {
socket.close();
}
}
Loading