|
| 1 | +/* |
| 2 | + * Object representing a single engine scan with engine name and scan result. |
| 3 | + * public static multiPrint methods is used in VT.java to print engine scans in |
| 4 | + * tabular format. |
| 5 | + */ |
| 6 | + |
| 7 | +// Import libraries |
| 8 | +// jansi - color printing |
| 9 | + |
| 10 | +import org.fusesource.jansi.Ansi; |
| 11 | + |
| 12 | +import static org.fusesource.jansi.Ansi.Color.BLACK; |
| 13 | +import static org.fusesource.jansi.Ansi.Color.GREEN; |
| 14 | +import static org.fusesource.jansi.Ansi.Color.RED; |
| 15 | +import static org.fusesource.jansi.Ansi.Color.WHITE; |
| 16 | +import static org.fusesource.jansi.Ansi.Color.YELLOW; |
| 17 | + |
| 18 | + |
| 19 | +public class ScanResult { |
| 20 | + |
| 21 | + // Constants |
| 22 | + private static final int MAX_ENGINE_LEN = 20; |
| 23 | + private static final int TABLE_ROW_LEN = 36; |
| 24 | + private static final Ansi.Color NORMAL_HIGHLIGHT = BLACK; |
| 25 | + private static final Ansi.Color NORMAL_TEXT = GREEN; |
| 26 | + private static final Ansi.Color ERROR_HIGHLIGHT = YELLOW; |
| 27 | + private static final Ansi.Color ERROR_TEXT = BLACK; |
| 28 | + private static final Ansi.Color BAD_HIGHLIGHT = RED; |
| 29 | + private static final Ansi.Color BAD_TEXT = WHITE; |
| 30 | + private static final String[] ERROR_MESSAGES = new String[] { |
| 31 | + "Confirmed timeout", "Timeout", |
| 32 | + "Unable to process file type", "" |
| 33 | + }; |
| 34 | + private static final String UNDETECTED = "Undetected"; |
| 35 | + |
| 36 | + // Instance variables |
| 37 | + private String engine; // Engine name |
| 38 | + private String result; // Result from engine scan |
| 39 | + private boolean error; // Was there error in engine scan |
| 40 | + |
| 41 | + // Default constructor |
| 42 | + public ScanResult() { |
| 43 | + engine = "VirusTotal"; |
| 44 | + result = UNDETECTED; |
| 45 | + error = false; |
| 46 | + } |
| 47 | + |
| 48 | + // Specified constructor sets instance variables with 'engine' and 'result' |
| 49 | + public ScanResult(String engine, String result) { |
| 50 | + this.engine = engine; |
| 51 | + this.result = result; |
| 52 | + error = false; |
| 53 | + |
| 54 | + // Change 'result' and 'error' instance variables if there was error |
| 55 | + for (String errorMessage : ERROR_MESSAGES) { |
| 56 | + if (result.equalsIgnoreCase(errorMessage)) { |
| 57 | + this.result = UNDETECTED; |
| 58 | + error = true; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Return engine name |
| 64 | + private String getEngine() { |
| 65 | + return engine; |
| 66 | + } |
| 67 | + |
| 68 | + // Return whether engine had error |
| 69 | + private boolean hasError() { |
| 70 | + return error; |
| 71 | + } |
| 72 | + |
| 73 | + // Return engine scan result |
| 74 | + private String getResult() { |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | + // Print colored coded (truncated or padded) engine name based on 'scanResult' |
| 79 | + private static void colorPrintEngine(ScanResult scanResult) { |
| 80 | + |
| 81 | + // Engine name |
| 82 | + String engine = ""; |
| 83 | + |
| 84 | + // Truncate engine name |
| 85 | + // https://docs.oracle.com/javase/7/docs/api/java/lang/String.html |
| 86 | + try { |
| 87 | + engine = scanResult.getEngine().substring(0, MAX_ENGINE_LEN); |
| 88 | + } |
| 89 | + |
| 90 | + // Pad engine name with spaces |
| 91 | + catch (IndexOutOfBoundsException e) { |
| 92 | + engine = scanResult.getEngine(); |
| 93 | + for (int i = engine.length(); i < MAX_ENGINE_LEN; i++) |
| 94 | + engine += " "; |
| 95 | + } |
| 96 | + |
| 97 | + // If result is "Undetected" -> print engine name NORMAL |
| 98 | + if (scanResult.getResult().equalsIgnoreCase(UNDETECTED)) { |
| 99 | + VT.colorPrint(false, NORMAL_HIGHLIGHT, NORMAL_TEXT, engine); |
| 100 | + } |
| 101 | + |
| 102 | + // If scan had error -> print engine name ERROR |
| 103 | + else if (scanResult.hasError()) { |
| 104 | + VT.colorPrint(false, ERROR_HIGHLIGHT, ERROR_TEXT, engine); |
| 105 | + } |
| 106 | + |
| 107 | + // If result is malicious -> print engine name BAD |
| 108 | + else { |
| 109 | + VT.colorPrint(false, BAD_HIGHLIGHT, BAD_TEXT, engine); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Print out 'scanResults' in tabular format with 'column' columns |
| 114 | + public static void multiPrint(ScanResult[] scanResults, int column) { |
| 115 | + StdOut.println(); |
| 116 | + |
| 117 | + // Determine width of table |
| 118 | + int tableWidth = TABLE_ROW_LEN * column; |
| 119 | + |
| 120 | + // If user specified engine was found |
| 121 | + if (scanResults.length == 1) |
| 122 | + tableWidth = TABLE_ROW_LEN; |
| 123 | + |
| 124 | + // Add upper bar to table |
| 125 | + for (int i = 0; i < tableWidth; i++) |
| 126 | + StdOut.print('-'); |
| 127 | + StdOut.println(); |
| 128 | + |
| 129 | + // Add engine-result content |
| 130 | + int nextLineCount = 1; // Determine when to print to next row |
| 131 | + int engineIndex = 0; // Determine when printing last engine |
| 132 | + |
| 133 | + // Iterate through given engine scans |
| 134 | + for (ScanResult scanResult : scanResults) { |
| 135 | + |
| 136 | + // Print color coded |
| 137 | + colorPrintEngine(scanResult); |
| 138 | + |
| 139 | + // https://introcs.cs.princeton.edu/java/15inout/ |
| 140 | + // https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html |
| 141 | + StdOut.printf("%-11.10s", scanResult.getResult()); |
| 142 | + |
| 143 | + // If time to print to next line or if user selected engine was found or |
| 144 | + // if printing the last engine |
| 145 | + if (nextLineCount == column || scanResults.length == 1 |
| 146 | + || engineIndex == scanResults.length - 1) { |
| 147 | + |
| 148 | + // Add lower bar |
| 149 | + StdOut.println(); |
| 150 | + for (int i = 0; i < tableWidth; i++) |
| 151 | + StdOut.print('-'); |
| 152 | + StdOut.println(); |
| 153 | + nextLineCount = 1; |
| 154 | + } |
| 155 | + |
| 156 | + // Print next engine scan tabbed next to previous engine scan |
| 157 | + else { |
| 158 | + StdOut.print("\t|\t"); |
| 159 | + nextLineCount++; |
| 160 | + } |
| 161 | + |
| 162 | + engineIndex++; |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | +} |
0 commit comments