Additional logger messages

This commit is contained in:
sanex3339
2017-07-29 19:01:11 +03:00
parent d5e84b1d1c
commit 8b308aa59c
11 changed files with 83 additions and 39 deletions
+1 -1
View File
@@ -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
View File
@@ -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`
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -37,4 +37,9 @@ export interface IRandomGenerator {
* @returns {string}
*/
getRandomVariableName (length: number): string
/**
* @returns {number}
*/
getSeed (): number;
}
+26 -11
View File
@@ -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 loggingLevelColor
* @param {LoggingMessage} loggingMessage
* @param {string} value
* @param {string | number} value
*/
private log (loggingLevelColor: chalk.ChalkChain, loggingMessage: LoggingMessage, value?: string): void {
public warn (loggingMessage: LoggingMessage, value?: string | number): void {
this.log(this.colorWarn, loggingMessage, value);
}
/**
*
* @param {ChalkChain} loggingLevelColor
* @param {LoggingMessage} loggingMessage
* @param {string | number} value
*/
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
View File
@@ -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;
}
}