|
| 1 | +package net.bc100dev.osintgram4j; |
| 2 | + |
| 3 | +import net.bc100dev.commons.utils.io.FileEncryption; |
| 4 | +import net.bc100dev.commons.utils.io.FileUtil; |
| 5 | +import org.apache.commons.cli.*; |
| 6 | + |
| 7 | +import java.io.*; |
| 8 | +import java.security.GeneralSecurityException; |
| 9 | +import java.util.Scanner; |
| 10 | + |
| 11 | +import static net.bc100dev.commons.utils.RuntimeEnvironment.USER_HOME; |
| 12 | +import static net.bc100dev.commons.utils.RuntimeEnvironment.getOperatingSystem; |
| 13 | + |
| 14 | +public class LogMainClass { |
| 15 | + |
| 16 | + private static Lookup lookup = Lookup.DEFAULT; |
| 17 | + |
| 18 | + private static void execute(String pass, TypeAction action, File file) { |
| 19 | + boolean isEncrypted = false; |
| 20 | + String finPass = pass; |
| 21 | + |
| 22 | + if (action == TypeAction.Open || action == TypeAction.Decrypt) { |
| 23 | + if (!file.exists()) { |
| 24 | + System.err.println("File \"" + file.getAbsolutePath() + "\" does not exist."); |
| 25 | + System.exit(1); |
| 26 | + } |
| 27 | + |
| 28 | + if (!file.canRead()) { |
| 29 | + System.err.println("File \"" + file.getAbsolutePath() + "\" is not readable, or cannot be opened by the current user."); |
| 30 | + System.exit(1); |
| 31 | + } |
| 32 | + |
| 33 | + try (FileInputStream fis = new FileInputStream(file)) { |
| 34 | + byte[] buff = new byte[1024]; |
| 35 | + int len; |
| 36 | + StringBuilder strBd = new StringBuilder(); |
| 37 | + |
| 38 | + while ((len = fis.read(buff, 0, 1024)) > 0) |
| 39 | + strBd.append(new String(buff, 0, len)); |
| 40 | + |
| 41 | + String str = strBd.toString(); |
| 42 | + if (!(str.contains("INFO") || str.contains("OFF") || |
| 43 | + str.contains("SEVERE") || str.contains("WARNING") || str.contains("ALL") || |
| 44 | + str.contains("CONFIG") || str.contains("FINE") || str.contains("FINER") || str.contains("FINEST"))) { |
| 45 | + if (!str.contains("Osintgram4j Log Data")) { |
| 46 | + if (!str.trim().isEmpty()) |
| 47 | + isEncrypted = true; |
| 48 | + } |
| 49 | + } |
| 50 | + } catch (IOException ex) { |
| 51 | + ex.printStackTrace(System.err); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (action == TypeAction.Open || action == TypeAction.Decrypt || action == TypeAction.Write_Encrypted) { |
| 56 | + if (pass.equalsIgnoreCase("ask")) { |
| 57 | + Console console = System.console(); |
| 58 | + Scanner sc = new Scanner(System.in); |
| 59 | + |
| 60 | + System.out.print("Enter password: "); |
| 61 | + |
| 62 | + if (console != null) |
| 63 | + finPass = new String(console.readPassword()); |
| 64 | + else |
| 65 | + finPass = sc.nextLine().trim(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + File og4jLogFile = switch (getOperatingSystem()) { |
| 70 | + case LINUX -> new File(USER_HOME.getAbsolutePath() + "/.config/net.bc100dev/osintgram4j/log.txt"); |
| 71 | + case WINDOWS -> new File(USER_HOME.getAbsolutePath() + "\\AppData\\Local\\BC100Dev\\Osintgram4j\\log.txt"); |
| 72 | + case MAC_OS -> new File(USER_HOME.getAbsolutePath() + "/Library/net.bc100dev/osintgram4j/log.txt"); |
| 73 | + }; |
| 74 | + |
| 75 | + File og4jNetLogFile = switch (getOperatingSystem()) { |
| 76 | + case LINUX -> new File(USER_HOME.getAbsolutePath() + "/.config/net.bc100dev/osintgram4j/net-log.txt"); |
| 77 | + case WINDOWS -> |
| 78 | + new File(USER_HOME.getAbsolutePath() + "\\AppData\\Local\\BC100Dev\\Osintgram4j\\net-log.txt"); |
| 79 | + case MAC_OS -> new File(USER_HOME.getAbsolutePath() + "/Library/net.bc100dev/osintgram4j/net-log.txt"); |
| 80 | + }; |
| 81 | + |
| 82 | + switch (action) { |
| 83 | + case Decrypt -> { |
| 84 | + if (!isEncrypted) { |
| 85 | + System.err.println("Cannot decrypt file"); |
| 86 | + System.err.println("Already in plain text"); |
| 87 | + System.exit(1); |
| 88 | + } |
| 89 | + |
| 90 | + try { |
| 91 | + byte[] data = FileEncryption.toDecrypted(file, finPass); |
| 92 | + File outFile = new File(file.getAbsolutePath() + ".log"); |
| 93 | + |
| 94 | + try (FileOutputStream fos = new FileOutputStream(outFile)) { |
| 95 | + fos.write(data); |
| 96 | + } |
| 97 | + } catch (IOException | GeneralSecurityException ex) { |
| 98 | + ex.printStackTrace(System.err); |
| 99 | + } |
| 100 | + } |
| 101 | + case Open -> { |
| 102 | + if (isEncrypted) { |
| 103 | + try { |
| 104 | + byte[] data = FileEncryption.toDecrypted(file, finPass); |
| 105 | + System.out.println(new String(data)); |
| 106 | + } catch (IOException | GeneralSecurityException ex) { |
| 107 | + System.err.println("Not able to decrypt file: " + file.getAbsolutePath()); |
| 108 | + ex.printStackTrace(System.err); |
| 109 | + } |
| 110 | + } else { |
| 111 | + try (FileInputStream fis = new FileInputStream(file)) { |
| 112 | + int len; |
| 113 | + byte[] buff = new byte[1024]; |
| 114 | + |
| 115 | + while ((len = fis.read(buff, 0, 1024)) != -1) |
| 116 | + System.out.println(new String(buff, 0, len)); |
| 117 | + } catch (IOException ex) { |
| 118 | + ex.printStackTrace(System.err); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + case Write -> { |
| 123 | + File inFile = lookup == Lookup.DEFAULT ? og4jLogFile : og4jNetLogFile; |
| 124 | + |
| 125 | + try { |
| 126 | + if (!file.exists()) |
| 127 | + FileUtil.createFile(file.getAbsolutePath(), true); |
| 128 | + } catch (IOException ex) { |
| 129 | + ex.printStackTrace(System.err); |
| 130 | + } |
| 131 | + |
| 132 | + try (FileInputStream fis = new FileInputStream(inFile); |
| 133 | + FileOutputStream fos = new FileOutputStream(file)) { |
| 134 | + byte[] buff = new byte[1024]; |
| 135 | + int len; |
| 136 | + |
| 137 | + while ((len = fis.read(buff, 0, 1024)) != -1) |
| 138 | + fos.write(buff, 0, len); |
| 139 | + } catch (IOException ex) { |
| 140 | + ex.printStackTrace(System.err); |
| 141 | + } |
| 142 | + } |
| 143 | + case Write_Encrypted -> { |
| 144 | + try { |
| 145 | + File outFile = new File(file.getAbsolutePath() + ".enc"); |
| 146 | + if (!outFile.exists()) |
| 147 | + FileUtil.createFile(outFile.getAbsolutePath(), true); |
| 148 | + |
| 149 | + File inFile = lookup == Lookup.DEFAULT ? og4jLogFile : og4jNetLogFile; |
| 150 | + |
| 151 | + byte[] encData = FileEncryption.toEncrypted(inFile, finPass); |
| 152 | + |
| 153 | + try (FileOutputStream fos = new FileOutputStream(outFile)) { |
| 154 | + fos.write(encData); |
| 155 | + } |
| 156 | + } catch (IOException | GeneralSecurityException ex) { |
| 157 | + ex.printStackTrace(System.err); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public static void main(String[] args) { |
| 164 | + Options opts = new Options(); |
| 165 | + |
| 166 | + Option passOption = new Option("p", "pass", true, "The password used for encrypting the log file"); |
| 167 | + passOption.setRequired(false); |
| 168 | + opts.addOption(passOption); |
| 169 | + |
| 170 | + Option logLookupOpt = new Option("l", "lookup", true, "The specific internal log file to look up (passes either \"def\" or \"net\")"); |
| 171 | + logLookupOpt.setRequired(false); |
| 172 | + opts.addOption(logLookupOpt); |
| 173 | + |
| 174 | + Option decryptOption = new Option("d", "decrypt", false, "Decrypt a specific log file"); |
| 175 | + decryptOption.setRequired(false); |
| 176 | + opts.addOption(decryptOption); |
| 177 | + |
| 178 | + Option openOption = new Option("o", "open", false, "Open a specific log file"); |
| 179 | + openOption.setRequired(false); |
| 180 | + opts.addOption(openOption); |
| 181 | + |
| 182 | + Option writeOption = new Option("w", "write", false, "Copies the specific log file from the Config Directory to the pointed directory"); |
| 183 | + writeOption.setRequired(false); |
| 184 | + opts.addOption(writeOption); |
| 185 | + |
| 186 | + Option writeEncryptedOption = new Option("we", "write-encrypted", false, "Same as \"-w\", but with File Encryption. If \"-p\" is not given, it defaults to asking via prompt."); |
| 187 | + writeEncryptedOption.setRequired(false); |
| 188 | + opts.addOption(writeEncryptedOption); |
| 189 | + |
| 190 | + Option outputFileOpt = new Option("f", "file", true, "Given to write to a (new) file, or given to read from the file itself"); |
| 191 | + writeEncryptedOption.setRequired(true); |
| 192 | + opts.addOption(outputFileOpt); |
| 193 | + |
| 194 | + CommandLineParser parser = new DefaultParser(); |
| 195 | + HelpFormatter formatter = new HelpFormatter(); |
| 196 | + CommandLine cmd; |
| 197 | + |
| 198 | + String pass = "ask"; |
| 199 | + TypeAction action = null; |
| 200 | + File outFile = null; |
| 201 | + |
| 202 | + try { |
| 203 | + cmd = parser.parse(opts, args); |
| 204 | + |
| 205 | + if (cmd.hasOption(passOption)) |
| 206 | + pass = cmd.getOptionValue(passOption).trim(); |
| 207 | + |
| 208 | + if (cmd.hasOption(decryptOption)) |
| 209 | + action = TypeAction.Decrypt; |
| 210 | + |
| 211 | + if (cmd.hasOption(openOption)) |
| 212 | + action = TypeAction.Open; |
| 213 | + |
| 214 | + if (cmd.hasOption(writeOption)) |
| 215 | + action = TypeAction.Write; |
| 216 | + |
| 217 | + if (cmd.hasOption(writeEncryptedOption)) |
| 218 | + action = TypeAction.Write_Encrypted; |
| 219 | + |
| 220 | + if (cmd.hasOption(outputFileOpt)) |
| 221 | + outFile = new File(cmd.getOptionValue(outputFileOpt).trim()); |
| 222 | + |
| 223 | + if (cmd.hasOption(logLookupOpt)) { |
| 224 | + String v = cmd.getOptionValue(logLookupOpt).toLowerCase().trim(); |
| 225 | + |
| 226 | + if (v.isEmpty()) { |
| 227 | + System.err.println("Value cannot be empty"); |
| 228 | + System.exit(1); |
| 229 | + } |
| 230 | + |
| 231 | + char c = v.toCharArray()[0]; |
| 232 | + if (c == 'd') |
| 233 | + lookup = Lookup.DEFAULT; |
| 234 | + else if (c == 'n') |
| 235 | + lookup = Lookup.NETWORK; |
| 236 | + else |
| 237 | + throw new ParseException("Invalid value \"" + v + "\""); |
| 238 | + } |
| 239 | + |
| 240 | + if (action == null) |
| 241 | + throw new ParseException("No action given"); |
| 242 | + |
| 243 | + if (outFile == null) |
| 244 | + throw new ParseException("No file given"); |
| 245 | + |
| 246 | + execute(pass, action, outFile); |
| 247 | + } catch (ParseException ex) { |
| 248 | + System.out.println(ex.getMessage()); |
| 249 | + formatter.printHelp("og4j-logdata", opts); |
| 250 | + |
| 251 | + System.exit(1); |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + enum Lookup { |
| 256 | + |
| 257 | + DEFAULT, |
| 258 | + |
| 259 | + NETWORK |
| 260 | + |
| 261 | + } |
| 262 | + |
| 263 | + enum TypeAction { |
| 264 | + |
| 265 | + Decrypt, |
| 266 | + |
| 267 | + Open, |
| 268 | + |
| 269 | + Write, |
| 270 | + |
| 271 | + Write_Encrypted |
| 272 | + |
| 273 | + } |
| 274 | + |
| 275 | +} |
0 commit comments