Prechádzať zdrojové kódy

Merge branch 'master' of git://github.com/adiantek/javascript-obfuscator into adiantek-master

# Conflicts:
#	dist/index.browser.js
#	dist/index.cli.js
#	dist/index.js
#	src/storages/string-array/StringArrayStorage.ts
sanex3339 5 rokov pred
rodič
commit
c87a6262a6

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
dist/index.browser.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
dist/index.cli.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
dist/index.js


+ 6 - 1
src/cli/JavaScriptObfuscatorCLI.ts

@@ -251,7 +251,7 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
             .option(
                 '--identifier-names-generator <string>',
                 'Sets identifier names generator. ' +
-                'Values: hexadecimal, mangled. ' +
+                'Values: hexadecimal, mangled, dictionary. ' +
                 'Default: hexadecimal',
                 IdentifierNamesGeneratorSanitizer
             )
@@ -259,6 +259,11 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
                 '--identifiers-prefix <string>',
                 'Sets prefix for all global identifiers.'
             )
+            .option(
+                '--identifiers-dictionary <list> (comma separated, without whitespaces)',
+                'Identifiers dictionary (comma separated)',
+                ArraySanitizer
+            )
             .option(
                 '--log <boolean>', 'Enables logging of the information to the console',
                 BooleanSanitizer

+ 14 - 0
src/container/modules/generators/GeneratorsModule.ts

@@ -8,6 +8,7 @@ import { IdentifierNamesGenerator } from '../../../enums/generators/identifier-n
 
 import { HexadecimalIdentifierNamesGenerator } from '../../../generators/identifier-names-generators/HexadecimalIdentifierNamesGenerator';
 import { MangledIdentifierNamesGenerator } from '../../../generators/identifier-names-generators/MangledIdentifierNamesGenerator';
+import { DictionaryNamesGenerator } from '../../../generators/identifier-names-generators/DictionaryNamesGenerator';
 
 export const generatorsModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
     // identifier name generators
@@ -21,6 +22,11 @@ export const generatorsModule: interfaces.ContainerModule = new ContainerModule(
         .inSingletonScope()
         .whenTargetNamed(IdentifierNamesGenerator.MangledIdentifierNamesGenerator);
 
+    bind<IIdentifierNamesGenerator>(ServiceIdentifiers.IIdentifierNamesGenerator)
+        .to(DictionaryNamesGenerator)
+        .inSingletonScope()
+        .whenTargetNamed(IdentifierNamesGenerator.DictionaryNamesGenerator);
+
     // identifier name generator factory
     bind<IIdentifierNamesGenerator>(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
         .toFactory<IIdentifierNamesGenerator>((context: interfaces.Context): (options: IOptions) => IIdentifierNamesGenerator => {
@@ -41,6 +47,14 @@ export const generatorsModule: interfaces.ContainerModule = new ContainerModule(
                         );
 
                         break;
+                    
+                    case IdentifierNamesGenerator.DictionaryNamesGenerator:
+                        identifierNamesGenerator = context.container.getNamed<IIdentifierNamesGenerator>(
+                            ServiceIdentifiers.IIdentifierNamesGenerator,
+                            IdentifierNamesGenerator.DictionaryNamesGenerator
+                        );
+
+                        break;
 
                     case IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator:
                     default:

+ 1 - 0
src/custom-nodes/node-calls-controller-nodes/NodeCallsControllerFunctionNode.ts

@@ -77,6 +77,7 @@ export class NodeCallsControllerFunctionNode extends AbstractCustomNode {
                 {
                     ...NO_ADDITIONAL_NODES_PRESET,
                     identifierNamesGenerator: this.options.identifierNamesGenerator,
+                    identifiersDictionary: this.options.identifiersDictionary,
                     seed: this.options.seed
                 }
             ).getObfuscatedCode();

+ 1 - 0
src/custom-nodes/self-defending-nodes/SelfDefendingUnicodeNode.ts

@@ -77,6 +77,7 @@ export class SelfDefendingUnicodeNode extends AbstractCustomNode {
             {
                 ...NO_ADDITIONAL_NODES_PRESET,
                 identifierNamesGenerator: this.options.identifierNamesGenerator,
+                identifiersDictionary: this.options.identifiersDictionary,
                 seed: this.options.seed,
                 unicodeEscapeSequence: true
             }

+ 1 - 0
src/custom-nodes/string-array-nodes/StringArrayCallsWrapper.ts

@@ -100,6 +100,7 @@ export class StringArrayCallsWrapper extends AbstractCustomNode {
             {
                 ...NO_ADDITIONAL_NODES_PRESET,
                 identifierNamesGenerator: this.options.identifierNamesGenerator,
+                identifiersDictionary: this.options.identifiersDictionary,
                 seed: this.options.seed
             }
         ).getObfuscatedCode();

+ 1 - 0
src/custom-nodes/string-array-nodes/StringArrayRotateFunctionNode.ts

@@ -107,6 +107,7 @@ export class StringArrayRotateFunctionNode extends AbstractCustomNode {
             {
                 ...NO_ADDITIONAL_NODES_PRESET,
                 identifierNamesGenerator: this.options.identifierNamesGenerator,
+                identifiersDictionary: this.options.identifiersDictionary,
                 seed: this.options.seed
             }
         ).getObfuscatedCode();

+ 2 - 1
src/enums/generators/identifier-names-generators/IdentifierNamesGenerator.ts

@@ -1,4 +1,5 @@
 export enum IdentifierNamesGenerator {
     HexadecimalIdentifierNamesGenerator = 'hexadecimal',
-    MangledIdentifierNamesGenerator = 'mangled'
+    MangledIdentifierNamesGenerator = 'mangled',
+    DictionaryNamesGenerator = 'dictionary'
 }

+ 118 - 0
src/generators/identifier-names-generators/DictionaryNamesGenerator.ts

@@ -0,0 +1,118 @@
+import { inject, injectable } from 'inversify';
+import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
+
+import { IOptions } from '../../interfaces/options/IOptions';
+import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
+
+import { AbstractIdentifierNamesGenerator } from './AbstractIdentifierNamesGenerator';
+
+@injectable()
+export class DictionaryNamesGenerator extends AbstractIdentifierNamesGenerator {
+    /**
+     * @type {string[]}
+     */
+    private identifiers: string[] = [];
+    
+    /**
+     * @type {IterableIterator<string>}
+     */
+    private it: IterableIterator<string>;
+
+    /**
+     * @param {IRandomGenerator} randomGenerator
+     * @param {IOptions} options
+     */
+    constructor (
+        @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
+        @inject(ServiceIdentifiers.IOptions) options: IOptions
+    ) {
+        super(randomGenerator, options);
+        for (const dict of options.identifiersDictionary) {
+            this.identifiers[this.identifiers.length] = dict.toLowerCase();
+        }
+        this.shuffle(this.identifiers);
+        this.it = this.identifiers.values();
+    }
+
+    /**
+     * @param {string} str
+     * @returns {string | null}
+     */
+    private static increment (str: string): string | null {
+        let newString: string = "";
+        let success: boolean = false;
+        
+        for (const i of str) {
+            if (!success && i === i.toUpperCase()) {
+                newString += i.toLowerCase();
+            } else if (!success && i === i.toLowerCase()) {
+                newString += i.toUpperCase();
+                success = true;
+            } else {
+                newString += i;
+            }
+        }
+        if (success) {
+            return newString;
+        }
+
+        return null;
+    }
+
+    /**
+     * @param {string[]} arr
+     * @returns {string[]}
+     */
+    private static incrementArr (arr: string[]): string[] {
+        const newArr: string[] = [];
+        for (const dict of arr) {
+            const newDict: string | null = DictionaryNamesGenerator.increment(dict);
+            if (newDict) {
+                newArr[newArr.length] = newDict;
+            }
+        }
+
+        return newArr;
+    }
+
+    public generate (): string {
+        if (this.identifiers.length === 0) {
+            throw new Error("identifiersDictionary is empty. Add more words to identifiersDictionary");
+        }
+        let itResult: IteratorResult<string> = this.it.next();
+        if (itResult.done) {
+            this.identifiers = DictionaryNamesGenerator.incrementArr(this.identifiers);
+            this.shuffle(this.identifiers);
+            this.it = this.identifiers.values();
+            itResult = this.it.next();
+        }
+        if (itResult.done) {
+            throw new Error("Too many identifiers in JS code, add more words to identifiersDictionary");
+        }
+        const identifierName: string = itResult.value;
+
+        return identifierName;
+    }
+
+    /**
+     * @returns {string}
+     */
+    public generateWithPrefix (): string {
+        const prefix: string = this.options.identifiersPrefix ?
+            `${this.options.identifiersPrefix}_`
+            : '';
+        const identifierName: string = this.generate();
+
+        return `${prefix}${identifierName}`.replace('__', '_');;
+    }
+
+    /**
+     * @param {string[]} arr
+     */
+    private shuffle (arr: string[]): void {
+        for (let i: number = arr.length - 1; i > 0; i--) {
+            const j: number = this.randomGenerator.getRandomInteger(0, i);
+            [arr[i], arr[j]] = [arr[j], arr[i]];
+        }
+    }
+}

+ 1 - 0
src/interfaces/options/IOptions.d.ts

@@ -16,6 +16,7 @@ export interface IOptions {
     readonly domainLock: string[];
     readonly identifierNamesGenerator: IdentifierNamesGenerator;
     readonly identifiersPrefix: string;
+    readonly identifiersDictionary: string[];
     readonly inputFileName: string;
     readonly log: boolean;
     readonly renameGlobals: boolean;

+ 9 - 1
src/options/Options.ts

@@ -108,7 +108,8 @@ export class Options implements IOptions {
      */
     @IsIn([
         IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
-        IdentifierNamesGenerator.MangledIdentifierNamesGenerator
+        IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
+        IdentifierNamesGenerator.DictionaryNamesGenerator
     ])
     public readonly identifierNamesGenerator!: IdentifierNamesGenerator;
 
@@ -118,6 +119,13 @@ export class Options implements IOptions {
     @IsString()
     public readonly identifiersPrefix!: string;
 
+    @IsArray()
+    @ArrayUnique()
+    @IsString({
+        each: true
+    })
+    public readonly identifiersDictionary!: string[];
+
     /**
      * @type {string}
      */

+ 1 - 0
src/options/presets/Default.ts

@@ -18,6 +18,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
     exclude: [],
     identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
     identifiersPrefix: '',
+    identifiersDictionary: ["qwertyuiop", "asdfghjkl", "zxcvbnm"],
     inputFileName: '',
     log: false,
     renameGlobals: false,

+ 1 - 0
src/options/presets/NoCustomNodes.ts

@@ -17,6 +17,7 @@ export const NO_ADDITIONAL_NODES_PRESET: TInputOptions = Object.freeze({
     exclude: [],
     identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
     identifiersPrefix: '',
+    identifiersDictionary: ["qwertyuiop", "asdfghjkl", "zxcvbnm"],
     inputFileName: '',
     log: false,
     renameGlobals: false,

Niektoré súbory nie sú zobrazené, pretože je v týchto rozdielových dátach zmenené mnoho súborov