From 806793e8d1e9c00e381db23552a99699a8dbc146 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Thu, 3 Aug 2023 00:40:22 -0400 Subject: [PATCH 1/3] Configurable logs --- src/qz/App.java | 19 +++++++++++++------ src/qz/common/Constants.java | 4 ++-- src/qz/utils/PrefsSearch.java | 11 +++++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/qz/App.java b/src/qz/App.java index cea1c3105..fd6e572db 100644 --- a/src/qz/App.java +++ b/src/qz/App.java @@ -84,6 +84,17 @@ public static Properties getTrayProperties() { } private static void setupFileLogging() { + //disable jetty logging + System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog"); + System.setProperty("org.eclipse.jetty.LEVEL", "OFF"); + + Properties app = CertificateManager.loadProperties(); + if(PrefsSearch.get(app, "log.disable", false)) { + return; + } + + int logSize = PrefsSearch.get(app, "log.size", Constants.DEFAULT_LOG_SIZE); + int logRotations = PrefsSearch.get(app, "log.rotations", Constants.DEFAULT_LOG_ROTATIONS); RollingFileAppender fileAppender = RollingFileAppender.newBuilder() .setName("log-file") .withAppend(true) @@ -91,16 +102,12 @@ private static void setupFileLogging() { .setFilter(ThresholdFilter.createFilter(Level.DEBUG, Filter.Result.ACCEPT, Filter.Result.DENY)) .withFileName(FileUtilities.USER_DIR + File.separator + Constants.LOG_FILE + ".log") .withFilePattern(FileUtilities.USER_DIR + File.separator + Constants.LOG_FILE + ".%i.log") - .withStrategy(DefaultRolloverStrategy.newBuilder().withMax(String.valueOf(Constants.LOG_ROTATIONS)).build()) - .withPolicy(SizeBasedTriggeringPolicy.createPolicy(String.valueOf(Constants.LOG_SIZE))) + .withStrategy(DefaultRolloverStrategy.newBuilder().withMax(String.valueOf(logRotations)).build()) + .withPolicy(SizeBasedTriggeringPolicy.createPolicy(String.valueOf(logSize))) .withImmediateFlush(true) .build(); fileAppender.start(); LoggerUtilities.getRootLogger().addAppender(fileAppender); - - //disable jetty logging - System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog"); - System.setProperty("org.eclipse.jetty.LEVEL", "OFF"); } } diff --git a/src/qz/common/Constants.java b/src/qz/common/Constants.java index 3284cbc0a..6e717db7b 100644 --- a/src/qz/common/Constants.java +++ b/src/qz/common/Constants.java @@ -28,8 +28,8 @@ public class Constants { public static final String[] PERSIST_PROPS = {"file.whitelist", "file.allow", "networking.hostname", "networking.port", STEAL_WEBSOCKET_PROPERTY }; public static final String AUTOSTART_FILE = ".autostart"; public static final String DATA_DIR = "qz"; - public static final int LOG_SIZE = 524288; - public static final int LOG_ROTATIONS = 5; + public static final int DEFAULT_LOG_SIZE = 524288; + public static final int DEFAULT_LOG_ROTATIONS = 5; public static final int BORDER_PADDING = 10; diff --git a/src/qz/utils/PrefsSearch.java b/src/qz/utils/PrefsSearch.java index a0d6fa19c..8fbc685cf 100644 --- a/src/qz/utils/PrefsSearch.java +++ b/src/qz/utils/PrefsSearch.java @@ -21,6 +21,17 @@ public static String get(Properties app, String name, String defaultVal, boolean return get(null, app, name, defaultVal, searchSystemProperties); } + public static int get(Properties app, String name, int defaultVal) { + try { + return Integer.parseInt(get(app, name, "", true)); + } catch(NumberFormatException ignore) {} + return defaultVal; + } + + public static boolean get(Properties app, String name, boolean defaultVal) { + return Boolean.parseBoolean(get(app, name, "" + defaultVal, true)); + } + public static String get(Properties user, Properties app, String name, String defaultVal, String... altNames) { return get(user, app, name, defaultVal, true, altNames); } From e02f4cbc2ed00dd1018940c831ed44712056fc71 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Thu, 3 Aug 2023 01:08:31 -0400 Subject: [PATCH 2/3] "rotations" -> "rotate" --- src/qz/App.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/qz/App.java b/src/qz/App.java index fd6e572db..52b4aa151 100644 --- a/src/qz/App.java +++ b/src/qz/App.java @@ -94,7 +94,7 @@ private static void setupFileLogging() { } int logSize = PrefsSearch.get(app, "log.size", Constants.DEFAULT_LOG_SIZE); - int logRotations = PrefsSearch.get(app, "log.rotations", Constants.DEFAULT_LOG_ROTATIONS); + int logRotate = PrefsSearch.get(app, "log.rotate", Constants.DEFAULT_LOG_ROTATIONS); RollingFileAppender fileAppender = RollingFileAppender.newBuilder() .setName("log-file") .withAppend(true) @@ -102,7 +102,7 @@ private static void setupFileLogging() { .setFilter(ThresholdFilter.createFilter(Level.DEBUG, Filter.Result.ACCEPT, Filter.Result.DENY)) .withFileName(FileUtilities.USER_DIR + File.separator + Constants.LOG_FILE + ".log") .withFilePattern(FileUtilities.USER_DIR + File.separator + Constants.LOG_FILE + ".%i.log") - .withStrategy(DefaultRolloverStrategy.newBuilder().withMax(String.valueOf(logRotations)).build()) + .withStrategy(DefaultRolloverStrategy.newBuilder().withMax(String.valueOf(logRotate)).build()) .withPolicy(SizeBasedTriggeringPolicy.createPolicy(String.valueOf(logSize))) .withImmediateFlush(true) .build(); From e515e72dc0487d2ea5988f1303b45138dca50d9b Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Thu, 3 Aug 2023 01:45:16 -0400 Subject: [PATCH 3/3] Consolidate preferences --- src/qz/App.java | 6 +++--- src/qz/auth/Certificate.java | 6 ++---- src/qz/common/Constants.java | 9 +++++++++ src/qz/printer/status/StatusSession.java | 3 ++- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/qz/App.java b/src/qz/App.java index 52b4aa151..376833b65 100644 --- a/src/qz/App.java +++ b/src/qz/App.java @@ -89,12 +89,12 @@ private static void setupFileLogging() { System.setProperty("org.eclipse.jetty.LEVEL", "OFF"); Properties app = CertificateManager.loadProperties(); - if(PrefsSearch.get(app, "log.disable", false)) { + if(PrefsSearch.get(app, Constants.PREFS_LOG_DISABLE, false)) { return; } - int logSize = PrefsSearch.get(app, "log.size", Constants.DEFAULT_LOG_SIZE); - int logRotate = PrefsSearch.get(app, "log.rotate", Constants.DEFAULT_LOG_ROTATIONS); + int logSize = PrefsSearch.get(app, Constants.PREFS_LOG_SIZE, Constants.DEFAULT_LOG_SIZE); + int logRotate = PrefsSearch.get(app, Constants.PREFS_LOG_ROTATE, Constants.DEFAULT_LOG_ROTATIONS); RollingFileAppender fileAppender = RollingFileAppender.newBuilder() .setName("log-file") .withAppend(true) diff --git a/src/qz/auth/Certificate.java b/src/qz/auth/Certificate.java index fda24ec4d..e91d7b3f5 100644 --- a/src/qz/auth/Certificate.java +++ b/src/qz/auth/Certificate.java @@ -37,8 +37,6 @@ public class Certificate { private static final Logger log = LogManager.getLogger(Certificate.class); private static final String QUIETLY_FAIL = "quiet"; - public static final String OVERRIDE_CA_FLAG = "trustedRootCert"; - public static final String OVERRIDE_CA_PROPERTY = "authcert.override"; public enum Algorithm { SHA1("SHA1withRSA"), @@ -142,13 +140,13 @@ public enum Algorithm { public static void scanAdditionalCAs() { ArrayList> certPaths = new ArrayList<>(); // First, look for "-DtrustedRootCert" command line property - certPaths.addAll(FileUtilities.parseDelimitedPaths(System.getProperty(OVERRIDE_CA_FLAG))); + certPaths.addAll(FileUtilities.parseDelimitedPaths(System.getProperty(Constants.PREFS_OVERRIDE_CA_CLI))); // Second, look for "override.crt" within App directory certPaths.add(new AbstractMap.SimpleEntry<>(SystemUtilities.getJarParentPath().resolve(Constants.OVERRIDE_CERT), QUIETLY_FAIL)); // Third, look for "authcert.override" property in qz-tray.properties - certPaths.addAll(FileUtilities.parseDelimitedPaths(App.getTrayProperties(), OVERRIDE_CA_PROPERTY)); + certPaths.addAll(FileUtilities.parseDelimitedPaths(App.getTrayProperties(), Constants.PREFS_OVERRIDE_CA)); for(Map.Entry certPath : certPaths) { if(certPath.getKey() != null) { diff --git a/src/qz/common/Constants.java b/src/qz/common/Constants.java index 6e717db7b..38063763e 100644 --- a/src/qz/common/Constants.java +++ b/src/qz/common/Constants.java @@ -69,6 +69,15 @@ public class Constants { public static final String PREFS_FILEIO_ENABLED = "security.file.enabled"; public static final String PREFS_FILEIO_STRICT = "security.file.strict"; + public static final String PREFS_LOG_DISABLE = "log.disable"; + public static final String PREFS_LOG_ROTATE = "log.rotate"; + public static final String PREFS_LOG_SIZE = "log.size"; + + public static final String PREFS_OVERRIDE_CA = "authcert.override"; + public static final String PREFS_OVERRIDE_CA_CLI = "trustedRootCert"; + + public static final String PREFS_PRINTER_JOB_DATA = "printer.status.jobdata"; + public static final String ALLOW_SITES_TEXT = "Permanently allowed \"%s\" to access local resources"; public static final String BLOCK_SITES_TEXT = "Permanently blocked \"%s\" from accessing local resources"; diff --git a/src/qz/printer/status/StatusSession.java b/src/qz/printer/status/StatusSession.java index e48829568..d7c0b5c1b 100644 --- a/src/qz/printer/status/StatusSession.java +++ b/src/qz/printer/status/StatusSession.java @@ -4,6 +4,7 @@ import org.apache.logging.log4j.Logger; import org.eclipse.jetty.websocket.api.Session; import qz.App; +import qz.common.Constants; import qz.printer.status.job.WmiJobStatusMap; import qz.utils.*; import qz.ws.PrintSocketClient; @@ -59,7 +60,7 @@ public void enableJobDataOnPrinter(String printer, int maxJobData, PrintingUtili if (!SystemUtilities.isWindows()) { throw new UnsupportedOperationException("Job data listeners are only supported on Windows"); } - String spoolFileMonitoring = PrefsSearch.get(App.getTrayProperties(), "printer.status.jobdata", "false", false ); + String spoolFileMonitoring = PrefsSearch.get(App.getTrayProperties(), Constants.PREFS_PRINTER_JOB_DATA, "false", false ); if (!Boolean.parseBoolean(spoolFileMonitoring)) { throw new UnsupportedOperationException("Job data listeners are currently disabled"); }