@@ -80,6 +80,11 @@ public class ConsoleProxy {
8080 static String factoryClzName ;
8181 static boolean standaloneStart = false ;
8282
83+ /**
84+ * Session timeout in milliseconds, default 300000 (5 minutes).
85+ */
86+ public static int sessionTimeoutMillis = 300000 ;
87+
8388 static String encryptorPassword = "Dummy" ;
8489 static final String [] skipProperties = new String []{"certificate" , "cacertificate" , "keystore_password" , "privatekey" };
8590
@@ -92,11 +97,13 @@ public static void addAllowedSession(String sessionUuid) {
9297 private static void configLog4j () {
9398 final ClassLoader loader = Thread .currentThread ().getContextClassLoader ();
9499 URL configUrl = loader .getResource ("/conf/log4j-cloud.xml" );
95- if (configUrl == null )
100+ if (configUrl == null ) {
96101 configUrl = ClassLoader .getSystemResource ("log4j-cloud.xml" );
102+ }
97103
98- if (configUrl == null )
104+ if (configUrl == null ) {
99105 configUrl = ClassLoader .getSystemResource ("conf/log4j-cloud.xml" );
106+ }
100107
101108 if (configUrl != null ) {
102109 try {
@@ -121,9 +128,8 @@ private static void configLog4j() {
121128 private static void configProxy (Properties conf ) {
122129 LOGGER .info ("Configure console proxy..." );
123130 for (Object key : conf .keySet ()) {
124- LOGGER .info ("Property " + (String )key + ": " + conf .getProperty ((String )key ));
125131 if (!ArrayUtils .contains (skipProperties , key )) {
126- LOGGER .info ("Property " + (String )key + ": " + conf .getProperty ((String )key ));
132+ LOGGER .info ("Property " + (String ) key + ": " + conf .getProperty ((String ) key ));
127133 }
128134 }
129135
@@ -165,13 +171,31 @@ private static void configProxy(Properties conf) {
165171 defaultBufferSize = Integer .parseInt (s );
166172 LOGGER .info ("Setting defaultBufferSize=" + defaultBufferSize );
167173 }
174+
175+ // Read consoleproxy.session.timeout in milliseconds.
176+ s = conf .getProperty ("consoleproxy.session.timeout" );
177+ if (s != null ) {
178+ try {
179+ int parsedTimeout = Integer .parseInt (s );
180+ if (parsedTimeout < 1000 ) {
181+ LOGGER .warn ("Invalid value for consoleproxy.session.timeout: " + s
182+ + " ms, must be >= 1000 ms, keeping default " + sessionTimeoutMillis + " ms" );
183+ } else {
184+ sessionTimeoutMillis = parsedTimeout ;
185+ LOGGER .info ("Setting consoleproxy.session.timeout=" + sessionTimeoutMillis + " ms" );
186+ }
187+ } catch (NumberFormatException e ) {
188+ LOGGER .warn ("Invalid value for consoleproxy.session.timeout: " + s
189+ + ", keeping default " + sessionTimeoutMillis + " ms" , e );
190+ }
191+ }
168192 }
169193
170194 public static ConsoleProxyServerFactory getHttpServerFactory () {
171195 try {
172196 Class <?> clz = Class .forName (factoryClzName );
173197 try {
174- ConsoleProxyServerFactory factory = (ConsoleProxyServerFactory )clz .newInstance ();
198+ ConsoleProxyServerFactory factory = (ConsoleProxyServerFactory ) clz .newInstance ();
175199 factory .init (ConsoleProxy .ksBits , ConsoleProxy .ksPassword );
176200 return factory ;
177201 } catch (InstantiationException e ) {
@@ -243,7 +267,7 @@ public static ConsoleProxyAuthenticationResult authenticateConsoleAccess(Console
243267 }
244268
245269 if (result != null && result instanceof String ) {
246- authResult = new Gson ().fromJson ((String )result , ConsoleProxyAuthenticationResult .class );
270+ authResult = new Gson ().fromJson ((String ) result , ConsoleProxyAuthenticationResult .class );
247271 } else {
248272 LOGGER .error ("Invalid authentication return object " + result + " for vm: " + param .getClientTag () + ", decline the access" );
249273 authResult .setSuccess (false );
@@ -318,19 +342,25 @@ public static void startWithContext(Properties conf, Object context, byte[] ksBi
318342 LOGGER .error ("Unable to setup private channel due to ClassNotFoundException" , e );
319343 }
320344
345+ // ensure we have a Properties object before merging defaults
346+ if (conf == null ) {
347+ conf = new Properties ();
348+ }
349+
321350 // merge properties from conf file
322351 InputStream confs = ConsoleProxy .class .getResourceAsStream ("/conf/consoleproxy.properties" );
323352 Properties props = new Properties ();
324353 if (confs == null ) {
325354 final File file = PropertiesUtil .findConfigFile ("consoleproxy.properties" );
326- if (file == null )
355+ if (file == null ) {
327356 LOGGER .info ("Can't load consoleproxy.properties from classpath, will use default configuration" );
328- else
357+ } else {
329358 try {
330359 confs = new FileInputStream (file );
331360 } catch (FileNotFoundException e ) {
332361 LOGGER .info ("Ignoring file not found exception and using defaults" );
333362 }
363+ }
334364 }
335365 if (confs != null ) {
336366 try {
@@ -339,15 +369,18 @@ public static void startWithContext(Properties conf, Object context, byte[] ksBi
339369 for (Object key : props .keySet ()) {
340370 // give properties passed via context high priority, treat properties from consoleproxy.properties
341371 // as default values
342- if (conf .get (key ) == null )
372+ if (conf .get (key ) == null ) {
343373 conf .put (key , props .get (key ));
374+ }
344375 }
345376 } catch (Exception e ) {
346377 LOGGER .error (e .toString (), e );
347378 }
348379 }
349380 try {
350- confs .close ();
381+ if (confs != null ) {
382+ confs .close ();
383+ }
351384 } catch (IOException e ) {
352385 LOGGER .error ("Failed to close consolepropxy.properties : " + e .toString (), e );
353386 }
@@ -481,8 +514,9 @@ public static ConsoleProxyClient getVncViewer(ConsoleProxyClientParam param) thr
481514 ConsoleProxyClientStatsCollector statsCollector = getStatsCollector ();
482515 String loadInfo = statsCollector .getStatsReport ();
483516 reportLoadInfo (loadInfo );
484- if (LOGGER .isDebugEnabled ())
517+ if (LOGGER .isDebugEnabled ()) {
485518 LOGGER .debug ("Report load change : " + loadInfo );
519+ }
486520 }
487521
488522 return viewer ;
@@ -506,13 +540,15 @@ public static ConsoleProxyClient getAjaxVncViewer(ConsoleProxyClientParam param,
506540 // protected against malicious attack by modifying URL content
507541 if (ajaxSession != null ) {
508542 long ajaxSessionIdFromUrl = Long .parseLong (ajaxSession );
509- if (ajaxSessionIdFromUrl != viewer .getAjaxSessionId ())
543+ if (ajaxSessionIdFromUrl != viewer .getAjaxSessionId ()) {
510544 throw new AuthenticationException ("Cannot use the existing viewer " + viewer + ": modified AJAX session id" );
545+ }
511546 }
512547
513548 if (param .getClientHostPassword () == null || param .getClientHostPassword ().isEmpty () ||
514- !param .getClientHostPassword ().equals (viewer .getClientHostPassword ()))
549+ !param .getClientHostPassword ().equals (viewer .getClientHostPassword ())) {
515550 throw new AuthenticationException ("Cannot use the existing viewer " + viewer + ": bad sid" );
551+ }
516552
517553 if (!viewer .isFrontEndAlive ()) {
518554
@@ -526,8 +562,9 @@ public static ConsoleProxyClient getAjaxVncViewer(ConsoleProxyClientParam param,
526562 ConsoleProxyClientStatsCollector statsCollector = getStatsCollector ();
527563 String loadInfo = statsCollector .getStatsReport ();
528564 reportLoadInfo (loadInfo );
529- if (LOGGER .isDebugEnabled ())
565+ if (LOGGER .isDebugEnabled ()) {
530566 LOGGER .debug ("Report load change : " + loadInfo );
567+ }
531568 }
532569 return viewer ;
533570 }
@@ -593,7 +630,7 @@ public void execute(Runnable r) {
593630 }
594631
595632 public static ConsoleProxyNoVncClient getNoVncViewer (ConsoleProxyClientParam param , String ajaxSession ,
596- Session session ) throws AuthenticationException {
633+ Session session ) throws AuthenticationException {
597634 boolean reportLoadChange = false ;
598635 String clientKey = param .getClientMapKey ();
599636 LOGGER .debug ("Getting NoVNC viewer for {}. Session requires new viewer: {}, client tag: {}. session UUID: {}" ,
@@ -609,8 +646,9 @@ public static ConsoleProxyNoVncClient getNoVncViewer(ConsoleProxyClientParam par
609646 reportLoadChange = true ;
610647 } else {
611648 if (param .getClientHostPassword () == null || param .getClientHostPassword ().isEmpty () ||
612- !param .getClientHostPassword ().equals (viewer .getClientHostPassword ()))
649+ !param .getClientHostPassword ().equals (viewer .getClientHostPassword ())) {
613650 throw new AuthenticationException ("Cannot use the existing viewer " + viewer + ": bad sid" );
651+ }
614652
615653 try {
616654 authenticationExternally (param );
@@ -620,7 +658,7 @@ public static ConsoleProxyNoVncClient getNoVncViewer(ConsoleProxyClientParam par
620658 }
621659 LOGGER .info ("Initializing new novnc client and disconnecting existing session" );
622660 try {
623- ((ConsoleProxyNoVncClient )viewer ).getSession ().disconnect ();
661+ ((ConsoleProxyNoVncClient ) viewer ).getSession ().disconnect ();
624662 } catch (IOException e ) {
625663 LOGGER .error ("Exception while disconnect session of novnc viewer object: " + viewer , e );
626664 }
@@ -635,10 +673,11 @@ public static ConsoleProxyNoVncClient getNoVncViewer(ConsoleProxyClientParam par
635673 ConsoleProxyClientStatsCollector statsCollector = getStatsCollector ();
636674 String loadInfo = statsCollector .getStatsReport ();
637675 reportLoadInfo (loadInfo );
638- if (LOGGER .isDebugEnabled ())
676+ if (LOGGER .isDebugEnabled ()) {
639677 LOGGER .debug ("Report load change : " + loadInfo );
678+ }
640679 }
641- return (ConsoleProxyNoVncClient )viewer ;
680+ return (ConsoleProxyNoVncClient ) viewer ;
642681 }
643682 }
644683}
0 commit comments