1 25 import java.io.*; 26 import java.util.Vector ; 27 import net.sf.javaguard.*; 28 import net.sf.javaguard.log.*; 29 30 31 114 public class JavaGuard { 115 116 private Vector inputJars; 117 121 private Vector inputDirs; 122 126 private Vector inputFileFilters; 127 128 private File outputFile; 129 130 private File scriptFile; 131 132 private File logFile; 133 136 private boolean dumpClassTree; 137 138 139 140 141 145 public static void main(String args[]) { 146 (new JavaGuard()).start(args); 147 } 148 149 150 151 152 154 private JavaGuard() { 155 setInputJars(new Vector ()); 156 setInputDirs(new Vector ()); 157 setInputFileFilters(new Vector ()); 158 setOutputFile(null); 159 setScriptFile(null); 160 setLogFile(null); 161 setDumpClassTree(false); 162 } 163 164 165 166 167 170 private void start(String [] args) { 171 try { 172 parseParameters(args); 173 verifyParameters(); 174 } catch (IllegalArgumentException iex) { 175 ScreenLogger.getInstance().println(iex.getMessage()); 176 System.out.println(); 177 help(); 178 } 179 obfuscate(); 180 } 181 182 183 184 185 190 private void parseParameters(String [] args) 191 throws IllegalArgumentException { 192 if (args.length < 1) help(); 194 195 setDumpClassTree(false); 196 197 int i = 0; 198 while (i<args.length) { 199 if (args[i].equals("-i") || args[i].equals("--in") || args[i].equals("--input")) { 200 if (i+1 == args.length) throw new IllegalArgumentException ("Input JAR file name not specified!"); 201 i++; 202 getInputJars().addElement(new File(args[i])); 203 } else if (args[i].equals("-d") || args[i].equals("--dir")) { 204 if (i+1 == args.length) throw new IllegalArgumentException ("Input directory not specified!"); 205 if (i+2 == args.length) throw new IllegalArgumentException ("Regular expression not specified!"); 206 i++; 207 getInputDirs().addElement(new File(args[i])); 208 i++; 209 getInputFileFilters().addElement(args[i]); 210 } else if (args[i].equals("-o") || args[i].equals("--out") || args[i].equals("--output")) { 211 if (i+1 == args.length) throw new IllegalArgumentException ("Output JAR file name not specified!"); 212 i++; 213 setOutputFile(new File(args[i])); 214 } else if (args[i].equals("-s") || args[i].equals("--script")) { 215 if (i+1 == args.length) throw new IllegalArgumentException ("Script file name not specified!"); 216 i++; 217 setScriptFile(new File(args[i])); 218 } else if (args[i].equals("-l") || args[i].equals("--log")) { 219 if (i+1 == args.length) throw new IllegalArgumentException ("Log file name not specified!"); 220 i++; 221 setLogFile(new File(args[i])); 222 } else if (args[i].equals("--dump")) { 223 setDumpClassTree(true); 224 } else if (args[i].equals("-v") || args[i].equals("--verbose")) { 225 ScreenLogger.getInstance().incrementLoggingLevel(); 226 } else if (args[i].equals("-h") || args[i].equals("--help")) { 227 System.out.println(Version.getProgramName()); 228 System.out.println(); 229 help(); 230 } else if (args[i].equals("--version")) { 231 System.out.println(Version.getProgramName()); 232 System.out.println(); 233 System.exit(0); 234 } else { 235 throw new IllegalArgumentException ("Unknown argument: " + args[i]); 236 } 237 i++; 238 } 239 } 240 241 242 243 244 248 private void verifyParameters() 249 throws IllegalArgumentException { 250 if (0 == getInputJars().size() && 0 == getInputDirs().size()) { 252 throw new IllegalArgumentException ("No input Jar files and/or input directories given!"); 253 } 254 for (int i=0; i<getInputJars().size(); i++) { 255 File inFile = (File) getInputJars().elementAt(i); 256 if (!inFile.exists()) { 258 throw new IllegalArgumentException ("Input Jar file '" 259 + inFile.getName() + "' does not exist!"); 260 } 261 if (!inFile.canRead()) { 262 throw new IllegalArgumentException ("Input Jar file '" 263 + inFile.getName() + "' cannot be read!"); 264 } 265 } 266 for (int i=0; i<getInputDirs().size(); i++) { 267 File inDir = (File) getInputDirs().elementAt(i); 268 if (!inDir.isDirectory() || !inDir.canRead()) { 269 throw new IllegalArgumentException ("Input directory '" 270 + inDir.getName() + "' is not a valid directory!"); 271 } 272 } 273 274 if (null == getOutputFile()) { 276 throw new IllegalArgumentException ("No output file specified!"); 277 } 278 if (getOutputFile().exists() && !getOutputFile().canWrite()) { 279 throw new IllegalArgumentException ("The output file is not writable!"); 280 } 281 282 if (null != getScriptFile() && getScriptFile().exists() && !getScriptFile().canRead()) { 284 throw new IllegalArgumentException ("Script file is not readable!"); 285 } 286 287 if (null != getLogFile() && getLogFile().exists() && !getLogFile().canWrite()) { 289 throw new IllegalArgumentException ("Logfile is not writable!"); 290 } 291 } 292 293 294 295 296 298 private void obfuscate() { 299 ScreenLogger logger = ScreenLogger.getInstance(); 301 logger.println(Version.getProgramName()); 302 try { 303 if (null != getLogFile()) { 304 FileLogger.setWriter(new PrintWriter( 306 new BufferedOutputStream( 307 new FileOutputStream(getLogFile())) 308 )); 309 } 310 311 GuardDB db = new GuardDB(); 314 if (null != getScriptFile()) { 316 logger.println("Reading and parsing script file..."); 317 ScriptFile scriptFile = new ScriptFile(getScriptFile()); 318 db.setScriptFile(scriptFile); 319 } 320 db.setInput(getInputJars(), getInputDirs(), getInputFileFilters()); 321 db.setOutputFile(getOutputFile()); 322 db.obfuscate(canDumpClassTree()); 323 } catch (Exception ex) { 324 logger.println(); 326 logger.println("Unrecoverable error during obfuscation:"); 327 logger.println(ex.getMessage()); 328 logger.printStackTrace(ex); 329 logger.println(); 330 } finally { 331 FileLogger.getInstance().close(); 332 } 333 } 334 335 336 337 338 342 private void setInputJars(Vector vec) { 343 inputJars = vec; 344 } 345 346 347 351 private Vector getInputJars() { 352 return inputJars; 353 } 354 355 356 357 358 362 private void setInputDirs(Vector vec) { 363 inputDirs = vec; 364 } 365 366 367 371 private Vector getInputDirs() { 372 return inputDirs; 373 } 374 375 376 377 378 382 private void setInputFileFilters(Vector vec) { 383 inputFileFilters = vec; 384 } 385 386 387 391 private Vector getInputFileFilters() { 392 return inputFileFilters; 393 } 394 395 396 397 398 402 private void setOutputFile(File file) { 403 outputFile = file; 404 } 405 406 407 411 private File getOutputFile() { 412 return outputFile; 413 } 414 415 416 417 418 422 private void setScriptFile(File file) { 423 scriptFile = file; 424 } 425 426 427 431 private File getScriptFile() { 432 return scriptFile; 433 } 434 435 436 437 438 442 private void setLogFile(File file) { 443 logFile = file; 444 } 445 446 447 451 private File getLogFile() { 452 return logFile; 453 } 454 455 456 457 458 462 private void setDumpClassTree(boolean dump) { 463 dumpClassTree = dump; 464 } 465 466 467 471 private boolean canDumpClassTree() { 472 return dumpClassTree; 473 } 474 475 476 477 478 481 public static void help() { 482 System.out.println("Usage: java JavaGuard -i <input-file> -o <output-file> [options]"); 483 System.out.println(); 484 System.out.println("-i <input-file> The name of an input JAR file to be obfuscated"); 485 System.out.println("--input"); 486 System.out.println(); 487 System.out.println("-d <dir> <regex> Obfuscate all files below the directory that match the regular"); 488 System.out.println("--dir expression"); 489 System.out.println(); 490 System.out.println("-o <output-file> The name of the output file that will contain the obfuscated"); 491 System.out.println("--output contents of all input files"); 492 System.out.println(); 493 System.out.println("-s <script> The name of a valid JavaGuard script file"); 494 System.out.println("--script"); 495 System.out.println(); 496 System.out.println("-l <log-file> The name for the log file"); 497 System.out.println("--log"); 498 System.out.println(); 499 System.out.println("--dump Dump the parsed class tree before obfuscation."); 500 System.out.println(); 501 System.out.println("-v Increment the logging level. To be more verbose specify the"); 502 System.out.println("--verbose parameter several times."); 503 System.out.println(); 504 System.out.println("-h Show this info page."); 505 System.out.println("--help"); 506 System.out.println(); 507 System.out.println("--version Show the program version."); 508 System.out.println(); 509 System.exit(-1); 510 } 511 } 512 | Popular Tags |