|
26 | 26 | import java.net.InetSocketAddress; |
27 | 27 | import java.net.URISyntaxException; |
28 | 28 | import java.net.URL; |
29 | | -import java.util.HashSet; |
30 | 29 | import java.util.Hashtable; |
31 | 30 | import java.util.Map; |
32 | 31 | import java.util.Properties; |
@@ -83,12 +82,73 @@ public class ConsoleProxy { |
83 | 82 | static String encryptorPassword = "Dummy"; |
84 | 83 | static final String[] skipProperties = new String[]{"certificate", "cacertificate", "keystore_password", "privatekey"}; |
85 | 84 |
|
86 | | - static Set<String> allowedSessions = new HashSet<>(); |
| 85 | + static Set<String> allowedSessions = ConcurrentHashMap.newKeySet(); |
| 86 | + private static final Object allowedSessionsLock = new Object(); |
87 | 87 |
|
| 88 | + private static final Map<String, ReconnectGrant> sessionReconnectGrants = new ConcurrentHashMap<>(); |
| 89 | + private static long sessionReconnectionWindowMs = 0L; |
| 90 | + |
| 91 | + // Invoked through reflection |
88 | 92 | public static void addAllowedSession(String sessionUuid) { |
89 | 93 | allowedSessions.add(sessionUuid); |
90 | 94 | } |
91 | 95 |
|
| 96 | + /** |
| 97 | + * Grant the client IP a reconnection window of #{@link #sessionReconnectionWindowMs} ms to the same session UUID in case of a disconnection. |
| 98 | + * The grant is bound to the client IP that was using the session so it cannot be redeemed by another client. |
| 99 | + * @param sessionUuid session UUID to grant a reconnect window for |
| 100 | + * @param clientIp source IP of the client the session was granted to |
| 101 | + */ |
| 102 | + public static void grantReconnectWindowForSessionAndClientIp(String sessionUuid, String clientIp) { |
| 103 | + if (sessionReconnectionWindowMs > 0) { |
| 104 | + ReconnectGrant grant = new ReconnectGrant(System.currentTimeMillis() + sessionReconnectionWindowMs, clientIp); |
| 105 | + sessionReconnectGrants.put(sessionUuid, grant); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * True if the session UUID has been granted reconnection, within the reconnection window #{@link #sessionReconnectionWindowMs}. |
| 111 | + */ |
| 112 | + private static boolean isSessionReconnectionGrantedForClientIp(String sessionUuid, String clientIp) { |
| 113 | + ReconnectGrant grant = sessionReconnectGrants.remove(sessionUuid); |
| 114 | + if (grant == null) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + if (grant.isExpired(System.currentTimeMillis())) { |
| 118 | + LOGGER.warn("Rejecting reconnection for session {} as the reconnect window: {}ms is already expired", |
| 119 | + sessionUuid, sessionReconnectionWindowMs); |
| 120 | + return false; |
| 121 | + } |
| 122 | + if (grant.clientIp != null && !grant.clientIp.equals(clientIp)) { |
| 123 | + LOGGER.warn("Rejecting reconnection for session {} as it was requested from IP {} " + |
| 124 | + "but the session was granted to IP {}", sessionUuid, clientIp, grant.clientIp); |
| 125 | + return false; |
| 126 | + } |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Drops expired, unclaimed reconnect grants so sessionReconnectGrants doesn't grow unbounded |
| 132 | + * when a client never reconnects after a disconnection. Invoked periodically by {@link ConsoleProxyGCThread}. |
| 133 | + */ |
| 134 | + static void cleanupExpiredReconnectGrants() { |
| 135 | + sessionReconnectGrants.entrySet().removeIf(entry -> entry.getValue().isExpired(System.currentTimeMillis())); |
| 136 | + } |
| 137 | + |
| 138 | + private static final class ReconnectGrant { |
| 139 | + final long expiryMillis; |
| 140 | + final String clientIp; |
| 141 | + |
| 142 | + ReconnectGrant(long expiryMillis, String clientIp) { |
| 143 | + this.expiryMillis = expiryMillis; |
| 144 | + this.clientIp = clientIp; |
| 145 | + } |
| 146 | + |
| 147 | + boolean isExpired(long now) { |
| 148 | + return now >= expiryMillis; |
| 149 | + } |
| 150 | + } |
| 151 | + |
92 | 152 | private static void configLog4j() { |
93 | 153 | final ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
94 | 154 | URL configUrl = loader.getResource("/conf/log4j-cloud.xml"); |
@@ -166,6 +226,12 @@ private static void configProxy(Properties conf) { |
166 | 226 | defaultBufferSize = Integer.parseInt(s); |
167 | 227 | LOGGER.info("Setting defaultBufferSize=" + defaultBufferSize); |
168 | 228 | } |
| 229 | + |
| 230 | + s = conf.getProperty("session_reconnection_window"); |
| 231 | + if (s != null) { |
| 232 | + sessionReconnectionWindowMs = Long.parseLong(s); |
| 233 | + LOGGER.info("Setting sessionReconnectionWindowMs=" + sessionReconnectionWindowMs); |
| 234 | + } |
169 | 235 | } |
170 | 236 |
|
171 | 237 | public static ConsoleProxyServerFactory getHttpServerFactory() { |
@@ -209,13 +275,17 @@ public static ConsoleProxyAuthenticationResult authenticateConsoleAccess(Console |
209 | 275 | } |
210 | 276 |
|
211 | 277 | String sessionUuid = param.getSessionUuid(); |
212 | | - if (allowedSessions.contains(sessionUuid)) { |
213 | | - LOGGER.debug("Acquiring the session " + sessionUuid + " not available for future use"); |
214 | | - allowedSessions.remove(sessionUuid); |
215 | | - } else { |
216 | | - LOGGER.info("Session " + sessionUuid + " has already been used, cannot connect"); |
217 | | - authResult.setSuccess(false); |
218 | | - return authResult; |
| 278 | + synchronized (allowedSessionsLock) { |
| 279 | + if (allowedSessions.remove(sessionUuid)) { |
| 280 | + LOGGER.debug("Acquiring the session {} from client IP {}", sessionUuid, param.getClientIp()); |
| 281 | + } else if (isSessionReconnectionGrantedForClientIp(sessionUuid, param.getClientIp())) { |
| 282 | + LOGGER.info("Reconnecting the session {} after a dropped connection", sessionUuid); |
| 283 | + return authResult; |
| 284 | + } else { |
| 285 | + LOGGER.info("Invalid or already used session {}, cannot connect", sessionUuid); |
| 286 | + authResult.setSuccess(false); |
| 287 | + return authResult; |
| 288 | + } |
219 | 289 | } |
220 | 290 |
|
221 | 291 | String websocketUrl = param.getWebsocketUrl(); |
@@ -625,7 +695,7 @@ public static ConsoleProxyNoVncClient getNoVncViewer(ConsoleProxyClientParam par |
625 | 695 | } catch (IOException e) { |
626 | 696 | LOGGER.error("Exception while disconnect session of novnc viewer object: " + viewer, e); |
627 | 697 | } |
628 | | - removeViewer(viewer); |
| 698 | + viewer.closeClient(); |
629 | 699 | viewer = new ConsoleProxyNoVncClient(session); |
630 | 700 | viewer.initClient(param); |
631 | 701 | connectionMap.put(clientKey, viewer); |
|
0 commit comments