New optionsPreset option

This commit is contained in:
sanex
2020-08-29 10:27:47 +03:00
parent c7e30aa3e2
commit 645a3c5cef
18 changed files with 442 additions and 79 deletions
+8
View File
@@ -19,6 +19,7 @@ import { ArraySanitizer } from './sanitizers/ArraySanitizer';
import { BooleanSanitizer } from './sanitizers/BooleanSanitizer';
import { IdentifierNamesGeneratorSanitizer } from './sanitizers/IdentifierNamesGeneratorSanitizer';
import { ObfuscationTargetSanitizer } from './sanitizers/ObfuscatingTargetSanitizer';
import { OptionsPresetSanitizer } from './sanitizers/OptionsPresetSanitizer';
import { SourceMapModeSanitizer } from './sanitizers/SourceMapModeSanitizer';
import { StringArrayEncodingSanitizer } from './sanitizers/StringArrayEncodingSanitizer';
@@ -253,6 +254,13 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
'--numbers-to-expressions <boolean>', 'Enables numbers conversion to expressions',
BooleanSanitizer
)
.option(
'--options-preset <string>',
'Allows to set options preset. ' +
'Values: default, low-obfuscation, medium-obfuscation, high-obfuscation. ' +
'Default: default',
OptionsPresetSanitizer
)
.option(
'--reserved-names <list> (comma separated, without whitespaces)',
'Disables obfuscation and generation of identifiers, which being matched by passed RegExp patterns (comma separated)',
@@ -0,0 +1,21 @@
import { TCLISanitizer } from '../../types/cli/TCLISanitizer';
import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
/**
* @param {string} value
* @returns {string}
*/
export const OptionsPresetSanitizer: TCLISanitizer <string> = (value: string): string => {
const isCorrectOptionsPreset: boolean = Object
.keys(OptionsPreset)
.some((key: string): boolean => {
return OptionsPreset[<keyof typeof OptionsPreset>key] === value;
});
if (!isCorrectOptionsPreset) {
throw new ReferenceError('Invalid value of `--options-preset` option');
}
return value;
};
@@ -0,0 +1,13 @@
import { MakeEnum } from '@gradecam/tsenum';
export const OptionsPreset: Readonly<{
Default: 'default';
LowObfuscation: 'low-obfuscation';
MediumObfuscation: 'medium-obfuscation';
HighObfuscation: 'high-obfuscation';
}> = MakeEnum({
Default: 'default',
LowObfuscation: 'low-obfuscation',
MediumObfuscation: 'medium-obfuscation',
HighObfuscation: 'high-obfuscation'
});
+2
View File
@@ -4,6 +4,7 @@ import { TStringArrayEncoding } from '../../types/options/TStringArrayEncoding';
import { IdentifierNamesGenerator } from '../../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
import { SourceMapMode } from '../../enums/source-map/SourceMapMode';
export interface IOptions {
@@ -22,6 +23,7 @@ export interface IOptions {
readonly inputFileName: string;
readonly log: boolean;
readonly numbersToExpressions: boolean;
readonly optionsPreset: TypeFromEnum<typeof OptionsPreset>;
readonly renameGlobals: boolean;
readonly renameProperties: boolean;
readonly reservedNames: string[];
+30 -1
View File
@@ -28,16 +28,30 @@ import { IOptionsNormalizer } from '../interfaces/options/IOptionsNormalizer';
import { IdentifierNamesGenerator } from '../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
import { ObfuscationTarget } from '../enums/ObfuscationTarget';
import { OptionsPreset } from '../enums/options/presets/OptionsPreset';
import { SourceMapMode } from '../enums/source-map/SourceMapMode';
import { StringArrayEncoding } from '../enums/StringArrayEncoding';
import { DEFAULT_PRESET } from './presets/Default';
import { LOW_OBFUSCATION_PRESET } from './presets/LowObfuscation';
import { MEDIUM_OBFUSCATION_PRESET } from './presets/MediumObfuscation';
import { HIGH_OBFUSCATION_PRESET } from './presets/HighObfuscation';
import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
import { IsAllowedForObfuscationTargets } from './validators/IsAllowedForObfuscationTargets';
@injectable()
export class Options implements IOptions {
/**
* @type {Map<TypeFromEnum<typeof OptionsPreset>, TInputOptions>}
*/
private static readonly optionPresetsMap: Map<TypeFromEnum<typeof OptionsPreset>, TInputOptions> = new Map([
[OptionsPreset.Default, DEFAULT_PRESET],
[OptionsPreset.LowObfuscation, LOW_OBFUSCATION_PRESET],
[OptionsPreset.MediumObfuscation, MEDIUM_OBFUSCATION_PRESET],
[OptionsPreset.HighObfuscation, HIGH_OBFUSCATION_PRESET]
]);
/**
* @type {ValidatorOptions}
*/
@@ -157,6 +171,17 @@ export class Options implements IOptions {
@IsBoolean()
public readonly numbersToExpressions!: boolean;
/**
* @type {OptionsPreset}
*/
@IsIn([
OptionsPreset.Default,
OptionsPreset.LowObfuscation,
OptionsPreset.MediumObfuscation,
OptionsPreset.HighObfuscation
])
public readonly optionsPreset!: TypeFromEnum<typeof OptionsPreset>;
/**
* @type {boolean}
*/
@@ -308,7 +333,11 @@ export class Options implements IOptions {
@inject(ServiceIdentifiers.TInputOptions) inputOptions: TInputOptions,
@inject(ServiceIdentifiers.IOptionsNormalizer) optionsNormalizer: IOptionsNormalizer
) {
Object.assign(this, DEFAULT_PRESET, inputOptions);
const optionsPreset: TInputOptions = Options.optionPresetsMap
.get(inputOptions.optionsPreset ?? OptionsPreset.Default)
?? DEFAULT_PRESET;
Object.assign(this, optionsPreset, inputOptions);
const errors: ValidationError[] = validateSync(this, Options.validatorOptions);
+2
View File
@@ -2,6 +2,7 @@ import { TInputOptions } from '../../types/options/TInputOptions';
import { IdentifierNamesGenerator } from '../../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
import { SourceMapMode } from '../../enums/source-map/SourceMapMode';
export const DEFAULT_PRESET: TInputOptions = Object.freeze({
@@ -22,6 +23,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
inputFileName: '',
log: false,
numbersToExpressions: false,
optionsPreset: OptionsPreset.Default,
renameGlobals: false,
renameProperties: false,
reservedNames: [],
+18
View File
@@ -0,0 +1,18 @@
import { TInputOptions } from '../../types/options/TInputOptions';
import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
import { StringArrayEncoding } from '../../enums/StringArrayEncoding';
import { MEDIUM_OBFUSCATION_PRESET } from './MediumObfuscation';
export const HIGH_OBFUSCATION_PRESET: TInputOptions = Object.freeze({
...MEDIUM_OBFUSCATION_PRESET,
controlFlowFlatteningThreshold: 1,
deadCodeInjectionThreshold: 1,
debugProtection: true,
debugProtectionInterval: true,
optionsPreset: OptionsPreset.HighObfuscation,
splitStringsChunkLength: 5,
stringArrayEncoding: StringArrayEncoding.Rc4,
stringArrayThreshold: 1
});
+15
View File
@@ -0,0 +1,15 @@
import { TInputOptions } from '../../types/options/TInputOptions';
import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
import { DEFAULT_PRESET } from './Default';
export const LOW_OBFUSCATION_PRESET: TInputOptions = Object.freeze({
...DEFAULT_PRESET,
disableConsoleOutput: true,
optionsPreset: OptionsPreset.LowObfuscation,
rotateStringArray: true,
selfDefending: true,
shuffleStringArray: true,
simplify: true
});
+18
View File
@@ -0,0 +1,18 @@
import { TInputOptions } from '../../types/options/TInputOptions';
import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
import { StringArrayEncoding } from '../../enums/StringArrayEncoding';
import { LOW_OBFUSCATION_PRESET } from './LowObfuscation';
export const MEDIUM_OBFUSCATION_PRESET: TInputOptions = Object.freeze({
...LOW_OBFUSCATION_PRESET,
controlFlowFlattening: true,
deadCodeInjection: true,
numbersToExpressions: true,
optionsPreset: OptionsPreset.MediumObfuscation,
splitStrings: true,
splitStringsChunkLength: 10,
stringArrayEncoding: StringArrayEncoding.Base64,
transformObjectKeys: true
});