소스 검색

Additional logger messages

sanex3339 7 년 전
부모
커밋
8b308aa59c

+ 1 - 1
CHANGELOG.md

@@ -2,7 +2,7 @@ Change Log
 ===
 v0.11.0
 ---
-* **New option:** `--log` enables logging of the information to the console.
+* **New option:** `log` enables logging of the information to the console.
 * **New option:** `renameGlobals` allows to enable obfuscation of global variable and function names with declaration.
 
 v0.10.0

+ 1 - 1
README.md

@@ -497,7 +497,7 @@ It's possible to lock your code to more than one domain or sub-domain. For insta
 ### `log`
 Type: `boolean` Default: `false`
 
-Enables logging of the information to the console..
+Enables logging of the information to the console.
 
 ### `mangle`
 Type: `boolean` Default: `false`

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/index.js


+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "javascript-obfuscator",
-  "version": "0.11.0-beta.1",
+  "version": "0.11.0-beta.2",
   "description": "JavaScript obfuscator",
   "keywords": [
     "obfuscator",

+ 14 - 4
src/JavaScriptObfuscatorInternal.ts

@@ -12,6 +12,7 @@ import { ILogger } from './interfaces/logger/ILogger';
 import { IObfuscationResult } from './interfaces/IObfuscationResult';
 import { IObfuscator } from './interfaces/IObfuscator';
 import { IOptions } from './interfaces/options/IOptions';
+import { IRandomGenerator } from './interfaces/utils/IRandomGenerator';
 import { ISourceMapCorrector } from './interfaces/ISourceMapCorrector';
 
 import { LoggingMessage } from './enums/logger/LoggingMessage';
@@ -41,6 +42,11 @@ export class JavaScriptObfuscatorInternal implements IJavaScriptObfuscator {
      */
     private readonly options: IOptions;
 
+    /**
+     * @type {IRandomGenerator}
+     */
+    private readonly randomGenerator: IRandomGenerator;
+
     /**
      * @type {ISourceMapCorrector}
      */
@@ -49,17 +55,20 @@ export class JavaScriptObfuscatorInternal implements IJavaScriptObfuscator {
     /**
      * @param {IObfuscator} obfuscator
      * @param {ISourceMapCorrector} sourceMapCorrector
+     * @param {IRandomGenerator} randomGenerator
      * @param {ILogger} logger
      * @param {IOptions} options
      */
     constructor (
         @inject(ServiceIdentifiers.IObfuscator) obfuscator: IObfuscator,
         @inject(ServiceIdentifiers.ISourceMapCorrector) sourceMapCorrector: ISourceMapCorrector,
+        @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
         @inject(ServiceIdentifiers.ILogger) logger: ILogger,
         @inject(ServiceIdentifiers.IOptions) options: IOptions
     ) {
         this.obfuscator = obfuscator;
         this.sourceMapCorrector = sourceMapCorrector;
+        this.randomGenerator = randomGenerator;
         this.logger = logger;
         this.options = options;
     }
@@ -69,8 +78,9 @@ export class JavaScriptObfuscatorInternal implements IJavaScriptObfuscator {
      * @returns {IObfuscationResult}
      */
     public obfuscate (sourceCode: string): IObfuscationResult {
-        const timeStart: number = +(new Date());
-        this.logger.logInfo(LoggingMessage.ObfuscationStarted);
+        const timeStart: number = Date.now();
+        this.logger.info(LoggingMessage.ObfuscationStarted);
+        this.logger.info(LoggingMessage.RandomGeneratorSeed, this.randomGenerator.getSeed());
 
         // parse AST tree
         const astTree: ESTree.Program = this.parseCode(sourceCode);
@@ -81,8 +91,8 @@ export class JavaScriptObfuscatorInternal implements IJavaScriptObfuscator {
         // generate code
         const generatorOutput: IGeneratorOutput = this.generateCode(sourceCode, obfuscatedAstTree);
 
-        const obfuscationTime: number = (+(new Date()) - timeStart) / 1000;
-        this.logger.logSuccess(LoggingMessage.ObfuscationCompleted, String(obfuscationTime));
+        const obfuscationTime: number = (Date.now() - timeStart) / 1000;
+        this.logger.success(LoggingMessage.ObfuscationCompleted, obfuscationTime);
 
         return this.getObfuscationResult(generatorOutput);
     }

+ 6 - 7
src/Obfuscator.ts

@@ -154,6 +154,8 @@ export class Obfuscator implements IObfuscator {
      */
     public obfuscateAstTree (astTree: ESTree.Program): ESTree.Program {
         if (Node.isProgramNode(astTree) && !astTree.body.length) {
+            this.logger.warn(LoggingMessage.EmptySourceCode);
+
             return astTree;
         }
 
@@ -177,20 +179,20 @@ export class Obfuscator implements IObfuscator {
 
         // first pass transformers: dead code injection transformer
         if (this.options.deadCodeInjection) {
-            this.logger.logInfo(LoggingMessage.StageDeadCodeInjection);
+            this.logger.info(LoggingMessage.StageDeadCodeInjection);
 
             astTree = this.transformAstTree(astTree, Obfuscator.deadCodeInjectionTransformersList);
         }
 
         // second pass transformers: control flow flattening transformers
         if (this.options.controlFlowFlattening) {
-            this.logger.logInfo(LoggingMessage.StageControlFlowFlattening);
+            this.logger.info(LoggingMessage.StageControlFlowFlattening);
 
             astTree = this.transformAstTree(astTree, Obfuscator.controlFlowTransformersList);
         }
 
         // third pass: converting and obfuscating transformers
-        this.logger.logInfo(LoggingMessage.StageObfuscation);
+        this.logger.info(LoggingMessage.StageObfuscation);
         astTree = this.transformAstTree(astTree, [
             ...Obfuscator.convertingTransformersList,
             ...Obfuscator.obfuscatingTransformersList
@@ -206,10 +208,7 @@ export class Obfuscator implements IObfuscator {
      * @param {NodeTransformer[]} nodeTransformers
      * @returns {Program}
      */
-    private transformAstTree (
-        astTree: ESTree.Program,
-        nodeTransformers: NodeTransformer[]
-    ): ESTree.Program {
+    private transformAstTree (astTree: ESTree.Program, nodeTransformers: NodeTransformer[]): ESTree.Program {
         if (!nodeTransformers.length) {
             return astTree;
         }

+ 5 - 3
src/enums/logger/LoggingMessage.ts

@@ -1,7 +1,9 @@
 export enum LoggingMessage {
+    EmptySourceCode = 'Empty source code. Obfuscation canceled...',
+    ObfuscationCompleted = 'Obfuscation completed. Total time: %s sec.',
     ObfuscationStarted = 'Obfuscation started...',
-    StageDeadCodeInjection = 'Stage: dead code injection...',
+    RandomGeneratorSeed = 'Random generator seed: %s...',
     StageControlFlowFlattening = 'Stage: control flow flattening...',
-    StageObfuscation = 'Stage: obfuscation...',
-    ObfuscationCompleted = 'Obfuscation completed, %s sec.'
+    StageDeadCodeInjection = 'Stage: dead code injection...',
+    StageObfuscation = 'Stage: obfuscation...'
 }

+ 10 - 4
src/interfaces/logger/ILogger.d.ts

@@ -3,13 +3,19 @@ import { LoggingMessage } from '../../enums/logger/LoggingMessage';
 export interface ILogger {
     /**
      * @param {LoggingMessage} loggingMessage
-     * @param {string} value
+     * @param {string | number} value
      */
-    logInfo (loggingMessage: LoggingMessage, value?: string): void;
+    info (loggingMessage: LoggingMessage, value?: string | number): void;
 
     /**
      * @param {LoggingMessage} loggingMessage
-     * @param {string} value
+     * @param {string | number} value
      */
-    logSuccess (loggingMessage: LoggingMessage, value?: string): void;
+    success (loggingMessage: LoggingMessage, value?: string | number): void;
+
+    /**
+     * @param {LoggingMessage} loggingMessage
+     * @param {string | number} value
+     */
+    warn (loggingMessage: LoggingMessage, value?: string | number): void;
 }

+ 5 - 0
src/interfaces/utils/IRandomGenerator.d.ts

@@ -37,4 +37,9 @@ export interface IRandomGenerator {
      * @returns {string}
      */
     getRandomVariableName (length: number): string
+
+    /**
+     * @returns {number}
+     */
+    getSeed (): number;
 }

+ 25 - 10
src/logger/Logger.ts

@@ -7,10 +7,10 @@ import { IInitializable } from '../interfaces/IInitializable';
 import { ILogger } from '../interfaces/logger/ILogger';
 import { IOptions } from '../interfaces/options/IOptions';
 
-import { initializable } from '../decorators/Initializable';
-
 import { LoggingMessage } from '../enums/logger/LoggingMessage';
 
+import { initializable } from '../decorators/Initializable';
+
 @injectable()
 export class Logger implements ILogger, IInitializable {
     /**
@@ -30,6 +30,12 @@ export class Logger implements ILogger, IInitializable {
     @initializable()
     private colorSuccess: chalk.ChalkChain;
 
+    /**
+     * @type {ChalkChain}
+     */
+    @initializable()
+    private colorWarn: chalk.ChalkChain;
+
     /**
      * @type {IOptions}
      */
@@ -48,36 +54,45 @@ export class Logger implements ILogger, IInitializable {
     public initialize (): void {
         this.colorInfo = chalk.cyan;
         this.colorSuccess = chalk.green;
+        this.colorWarn = chalk.yellow;
     }
 
     /**
      * @param {LoggingMessage} loggingMessage
-     * @param {string} value
+     * @param {string | number} value
      */
-    public logInfo (loggingMessage: LoggingMessage, value?: string): void {
+    public info (loggingMessage: LoggingMessage, value?: string | number): void {
         this.log(this.colorInfo, loggingMessage, value);
     }
 
     /**
      * @param {LoggingMessage} loggingMessage
-     * @param {string} value
+     * @param {string | number} value
      */
-    public logSuccess (loggingMessage: LoggingMessage, value?: string): void {
+    public success (loggingMessage: LoggingMessage, value?: string | number): void {
         this.log(this.colorSuccess, loggingMessage, value);
     }
 
+    /**
+     * @param {LoggingMessage} loggingMessage
+     * @param {string | number} value
+     */
+    public warn (loggingMessage: LoggingMessage, value?: string | number): void {
+        this.log(this.colorWarn, loggingMessage, value);
+    }
+
     /**
      *
-     * @param loggingLevelColor
+     * @param {ChalkChain} loggingLevelColor
      * @param {LoggingMessage} loggingMessage
-     * @param {string} value
+     * @param {string | number} value
      */
-    private log (loggingLevelColor: chalk.ChalkChain, loggingMessage: LoggingMessage, value?: string): void {
+    private log (loggingLevelColor: chalk.ChalkChain, loggingMessage: LoggingMessage, value?: string | number): void {
         if (!this.options.log) {
             return;
         }
 
-        const processedMessage: string = loggingLevelColor(`${Logger.loggingPrefix} ${loggingMessage}`);
+        const processedMessage: string = loggingLevelColor(`\n${Logger.loggingPrefix} ${loggingMessage}`);
 
         !value ? console.log(processedMessage) : console.log(processedMessage, value);
     }

+ 13 - 6
src/utils/RandomGenerator.ts

@@ -30,12 +30,6 @@ export class RandomGenerator implements IRandomGenerator, IInitializable {
      */
     public static readonly randomGeneratorPoolHexadecimal: string = `abcdef${RandomGenerator.randomGeneratorPoolNumbers}`;
 
-    /**
-     * @type {number}
-     */
-    @initializable()
-    public seed: number;
-
     /**
      * @type {IOptions}
      */
@@ -52,6 +46,12 @@ export class RandomGenerator implements IRandomGenerator, IInitializable {
     @initializable()
     private randomGenerator: Chance.Chance | Chance.SeededChance;
 
+    /**
+     * @type {number}
+     */
+    @initializable()
+    private seed: number;
+
     /**
      * @type {ISourceCode}
      */
@@ -159,4 +159,11 @@ export class RandomGenerator implements IRandomGenerator, IInitializable {
 
         return randomVariableName;
     }
+
+    /**
+     * @returns {number}
+     */
+    public getSeed (): number {
+        return this.seed;
+    }
 }

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.