-
Notifications
You must be signed in to change notification settings - Fork 27
/
options.ts
920 lines (856 loc) · 26.6 KB
/
options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
import { ok } from "assert";
import presets from "./presets";
import { ProbabilityMap } from "./probability";
export interface ObfuscateOptions {
/**
* ### `preset`
*
* JS-Confuser comes with three presets built into the obfuscator.
*
* | Preset | Transforms | Performance Reduction | Sample |
* | --- | --- | --- | --- |
* | High | 21/22 | 98% | [Sample](https://github.com/MichaelXF/js-confuser/blob/master/samples/high.js) |
* | Medium | 15/22 | 52% | [Sample](https://github.com/MichaelXF/js-confuser/blob/master/samples/medium.js) |
* | Low | 10/22 | 30% | [Sample](https://github.com/MichaelXF/js-confuser/blob/master/samples/low.js) |
*
* You can extend each preset or all go without them entirely. (`"high"/"medium"/"low"`)
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
preset?: "high" | "medium" | "low" | false;
/**
* ### `target`
*
* The execution context for your output. _Required_.
*
* 1. `"node"`
* 2. `"browser"`
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
target: "node" | "browser";
/**
* ### `indent`
*
* Controls the indentation of the output.
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
indent?: 2 | 4 | "tabs";
/**
* ### `compact`
*
* Remove's whitespace from the final output. (`true/false`)
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
compact?: boolean;
/**
* ### `hexadecimalNumbers`
*
* Uses the hexadecimal representation (`50` -> `0x32`) for numbers. (`true/false`)
*/
hexadecimalNumbers?: boolean;
/**
* ### `minify`
*
* Minifies redundant code. (`true/false`)
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
minify?: boolean;
/**
* ### `es5`
*
* Converts output to ES5-compatible code. (`true/false`)
*
* Does not cover all cases such as Promises or Generator functions. Use [Babel](https://babel.dev/).
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
es5?: boolean;
/**
* ### `renameVariables`
*
* Determines if variables should be renamed. (`true/false`)
* - Potency High
* - Resilience High
* - Cost Medium
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
renameVariables?: ProbabilityMap<boolean>;
/**
* ### `renameGlobals`
*
* Renames top-level variables, turn this off for web-related scripts. Enabled by default. (`true/false`)
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
renameGlobals?: ProbabilityMap<boolean>;
/**
* ### `identifierGenerator`
*
* Determines how variables are renamed.
*
* | Mode | Description | Example |
* | --- | --- | --- |
* | `"hexadecimal"` | Random hex strings | \_0xa8db5 |
* | `"randomized"` | Random characters | w$Tsu4G |
* | `"zeroWidth"` | Invisible characters | U+200D |
* | `"mangled"` | Alphabet sequence | a, b, c |
* | `"number"` | Numbered sequence | var_1, var_2 |
* | `<function>` | Write a custom name generator | See Below |
*
* ```js
* // Custom implementation
* JsConfuser.obfuscate(code, {
* target: "node",
* renameVariables: true,
* identifierGenerator: function () {
* return "$" + Math.random().toString(36).substring(7);
* },
* });
*
* // Numbered variables
* var counter = 0;
* JsConfuser.obfuscate(code, {
* target: "node",
* renameVariables: true,
* identifierGenerator: function () {
* return "var_" + (counter++);
* },
* });
* ```
*
* JSConfuser tries to reuse names when possible, creating very potent code.
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
identifierGenerator?: ProbabilityMap<
"hexadecimal" | "randomized" | "zeroWidth" | "mangled" | "number"
>;
/**
* ### `nameRecycling`
*
* Attempts to reuse released names.
*
* - Potency Medium
* - Resilience High
* - Cost Low
*
* ```js
* // Input
* function percentage(x) {
* var multiplied = x * 100;
* var floored = Math.floor(multiplied);
* var output = floored + "%"
* return output;
* }
*
* // Output
* function percentage(x) {
* var multiplied = x * 100;
* var floored = Math.floor(multiplied);
* multiplied = floored + "%";
* return multiplied;
* }
* ```
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
nameRecycling?: ProbabilityMap<boolean>;
/**
* ### `controlFlowFlattening`
*
* ⚠️ Significantly impacts performance, use sparingly!
*
* [Control-flow Flattening](https://docs.jscrambler.com/code-integrity/documentation/transformations/control-flow-flattening) hinders program comprehension by creating convoluted switch statements. (`true/false/0-1`)
*
* Use a number to control the percentage from 0 to 1.
*
* - Potency High
* - Resilience High
* - Cost High
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
controlFlowFlattening?: ProbabilityMap<boolean>;
/**
* ### `hideInitializingCode`
*
* Hides initializing code with complex ternary expressions. (`true/false`)
*
* - Potency High
* - Resilience High
* - Cost High
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
hideInitializingCode?: ProbabilityMap<boolean>;
/**
* ### `globalConcealing`
*
* Global Concealing hides global variables being accessed. (`true/false`)
*
* - Potency Medium
* - Resilience High
* - Cost Low
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
globalConcealing?: ProbabilityMap<boolean>;
/**
* ### `stringCompression`
*
* String Compression uses LZW's compression algorithm to compress strings. (`true/false/0-1`)
*
* `"console"` -> `inflate('replaĕ!ğğuģģ<~@')`
*
* - Potency High
* - Resilience Medium
* - Cost Medium
*/
stringCompression?: ProbabilityMap<boolean>;
/**
* ### `stringConcealing`
*
* [String Concealing](https://docs.jscrambler.com/code-integrity/documentation/transformations/string-concealing) involves encoding strings to conceal plain-text values. (`true/false/0-1`)
*
* `"console"` -> `decrypt('<~@rH7+Dert~>')`
*
* - Potency High
* - Resilience Medium
* - Cost Medium
*/
stringConcealing?: ProbabilityMap<boolean>;
/**
* ### `stringEncoding`
*
* [String Encoding](https://docs.jscrambler.com/code-integrity/documentation/transformations/string-encoding) transforms a string into an encoded representation. (`true/false/0-1`)
*
* `"console"` -> `'\x63\x6f\x6e\x73\x6f\x6c\x65'`
*
* - Potency Low
* - Resilience Low
* - Cost Low
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
stringEncoding?: ProbabilityMap<boolean>;
/**
* ### `stringSplitting`
*
* [String Splitting](https://docs.jscrambler.com/code-integrity/documentation/transformations/string-splitting) splits your strings into multiple expressions. (`true/false/0-1`)
*
* `"console"` -> `String.fromCharCode(99) + 'ons' + 'ole'`
*
* - Potency Medium
* - Resilience Medium
* - Cost Medium
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
stringSplitting?: ProbabilityMap<boolean>;
/**
* ### `duplicateLiteralsRemoval`
*
* [Duplicate Literals Removal](https://docs.jscrambler.com/code-integrity/documentation/transformations/duplicate-literals-removal) replaces duplicate literals with a single variable name. (`true/false`)
*
* - Potency Medium
* - Resilience Low
* - Cost High
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
duplicateLiteralsRemoval?: ProbabilityMap<boolean>;
/**
* ### `dispatcher`
*
* Creates a middleman function to process function calls. (`true/false/0-1`)
*
* - Potency Medium
* - Resilience Medium
* - Cost High
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
dispatcher?: ProbabilityMap<boolean>;
/**
* ### `eval`
*
* #### **`Security Warning`**
*
* From [MDN](<(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)**>): Executing JavaScript from a string is an enormous security risk. It is far too easy
* for a bad actor to run arbitrary code when you use eval(). Never use eval()!
*
* Wraps defined functions within eval statements.
*
* - **`false`** - Avoids using the `eval` function. _Default_.
* - **`true`** - Wraps function's code into an `eval` statement.
*
* ```js
* // Output.js
* var Q4r1__ = {
* Oo$Oz8t: eval(
* "(function(YjVpAp){var gniSBq6=kHmsJrhOO;switch(gniSBq6){case'RW11Hj5x':return console;}});"
* ),
* };
* Q4r1__.Oo$Oz8t("RW11Hj5x");
* ```
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
eval?: ProbabilityMap<boolean>;
/**
* ### `rgf`
*
* RGF (Runtime-Generated-Functions) uses the [`new Function(code...)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function) syntax to construct executable code from strings. (`"all"/true/false`)
*
* - **This can break your code. This is also as dangerous as `eval`.**
* - **Due to the security concerns of arbitrary code execution, you must enable this yourself.**
* - The arbitrary code is also obfuscated.
*
* | Mode | Description |
* | --- | --- |
* | `"all"` | Recursively applies to every scope (slow) |
* | `true` | Applies to the top level only |
* | `false` | Feature disabled |
*
* ```js
* // Input
* function log(x){
* console.log(x)
* }
*
* log("Hello World")
*
* // Output
* var C6z0jyO=[new Function('a2Fjjl',"function OqNW8x(OqNW8x){console['log'](OqNW8x)}return OqNW8x(...Array.prototype.slice.call(arguments,1))")];(function(){return C6z0jyO[0](C6z0jyO,...arguments)}('Hello World'))
* ```
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
rgf?: ProbabilityMap<boolean | "all">;
/**
* ### `stack`
*
* Local variables are consolidated into a rotating array.
*
* [Similar to Jscrambler's Variable Masking](https://docs.jscrambler.com/code-integrity/documentation/transformations/variable-masking)
*
* - Potency Medium
* - Resilience Medium
* - Cost Low
*
* ```js
* // Input
* function add3(x, y, z){
* return x + y + z;
* }
*
* // Output
* function iVQoGQD(...iVQoGQD){
* ~(iVQoGQD.length = 3, iVQoGQD[215] = iVQoGQD[2], iVQoGQD[75] = 227, iVQoGQD[iVQoGQD[75] - (iVQoGQD[75] - 75)] = iVQoGQD[75] - (iVQoGQD[75] - 239), iVQoGQD[iVQoGQD[iVQoGQD[75] - 164] - 127] = iVQoGQD[iVQoGQD[75] - 238], iVQoGQD[iVQoGQD[75] - 104] = iVQoGQD[75] - 482, iVQoGQD[iVQoGQD[135] + 378] = iVQoGQD[iVQoGQD[135] + 318] - 335, iVQoGQD[21] = iVQoGQD[iVQoGQD[135] + 96], iVQoGQD[iVQoGQD[iVQoGQD[75] - 104] - (iVQoGQD[75] - 502)] = iVQoGQD[iVQoGQD[75] - 164] - 440);
* return iVQoGQD[75] > iVQoGQD[75] + 90 ? iVQoGQD[iVQoGQD[135] - (iVQoGQD[135] + 54)] : iVQoGQD[iVQoGQD[135] + 117] + iVQoGQD[iVQoGQD[iVQoGQD[75] - (iVQoGQD[75] - (iVQoGQD[75] - 104))] - (iVQoGQD[135] - 112)] + iVQoGQD[215];
* };
* ```
*/
stack?: ProbabilityMap<boolean>;
/**
* ### `objectExtraction`
*
* Extracts object properties into separate variables. (`true/false`)
*
* - Potency Medium
* - Resilience Medium
* - Cost Low
*
* ```js
* // Input
* var utils = {
* isString: x=>typeof x === "string",
* isBoolean: x=>typeof x === "boolean"
* }
* if ( utils.isString("Hello") ) {
* // ...
* }
*
* // Output
* var utils_isString = x=>typeof x === "string";
* var utils_isBoolean = x=>typeof x === "boolean"
* if ( utils_isString("Hello") ) {
* // ...
* }
* ```
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
objectExtraction?: ProbabilityMap<boolean>;
/**
* ### `flatten`
*
* Brings independent declarations to the highest scope. (`true/false`)
*
* - Potency Medium
* - Resilience Medium
* - Cost High
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
flatten?: ProbabilityMap<boolean>;
/**
* ### `deadCode`
*
* Randomly injects dead code. (`true/false/0-1`)
*
* Use a number to control the percentage from 0 to 1.
*
* - Potency Medium
* - Resilience Medium
* - Cost Low
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
deadCode?: ProbabilityMap<boolean>;
/**
* ### `calculator`
*
* Creates a calculator function to handle arithmetic and logical expressions. (`true/false/0-1`)
*
* - Potency Medium
* - Resilience Medium
* - Cost Low
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
calculator?: ProbabilityMap<boolean>;
lock?: {
/**
* ### `lock.selfDefending`
*
* Prevents the use of code beautifiers or formatters against your code.
*
* [Identical to Obfuscator.io's Self Defending](https://github.com/javascript-obfuscator/javascript-obfuscator#selfdefending)
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
selfDefending?: boolean;
/**
* ### `lock.antiDebug`
*
* Adds `debugger` statements throughout the code. Additionally adds a background function for DevTools detection. (`true/false/0-1`)
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
antiDebug?: ProbabilityMap<boolean>;
/**
* ### `lock.context`
*
* Properties that must be present on the `window` object (or `global` for NodeJS). (`string[]`)
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
context?: string[];
/**
* ### `lock.nativeFunctions`
*
* Set of global functions that are native. Such as `require`, `fetch`. If these variables are modified the program crashes.
* Set to `true` to use the default set of native functions. (`string[]/true/false`)
*
* - Potency Low
* - Resilience Medium
* - Cost Medium
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
nativeFunctions?: string[] | Set<string> | boolean;
/**
* ### `lock.startDate`
*
* When the program is first able to be used. (`number` or `Date`)
*
* Number should be in milliseconds.
*
* - Potency Low
* - Resilience Medium
* - Cost Medium
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
startDate?: number | Date | false;
/**
* ### `lock.endDate`
*
* When the program is no longer able to be used. (`number` or `Date`)
*
* Number should be in milliseconds.
*
* - Potency Low
* - Resilience Medium
* - Cost Medium
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
endDate?: number | Date | false;
/**
* ### `lock.domainLock`
* Array of regex strings that the `window.location.href` must follow. (`Regex[]` or `string[]`)
*
* - Potency Low
* - Resilience Medium
* - Cost Medium
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
domainLock?: RegExp[] | string[] | false;
/**
* ### `lock.osLock`
* Array of operating-systems where the script is allowed to run. (`string[]`)
*
* - Potency Low
* - Resilience Medium
* - Cost Medium
*
* Allowed values: `"linux"`, `"windows"`, `"osx"`, `"android"`, `"ios"`
*
* Example: `["linux", "windows"]`
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
osLock?: ("linux" | "windows" | "osx" | "android" | "ios")[] | false;
/**
* ### `lock.browserLock`
* Array of browsers where the script is allowed to run. (`string[]`)
*
* - Potency Low
* - Resilience Medium
* - Cost Medium
*
* Allowed values: `"firefox"`, `"chrome"`, `"iexplorer"`, `"edge"`, `"safari"`, `"opera"`
*
* Example: `["firefox", "chrome"]`
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
browserLock?:
| ("firefox" | "chrome" | "iexplorer" | "edge" | "safari" | "opera")[]
| false;
/**
* ### `lock.integrity`
*
* Integrity ensures the source code is unchanged. (`true/false/0-1`)
*
* [Learn more here](https://github.com/MichaelXF/js-confuser/blob/master/Integrity.md).
*
* - Potency Medium
* - Resilience High
* - Cost High
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
integrity?: ProbabilityMap<boolean>;
/**
* ### `lock.countermeasures`
*
* A custom callback function to invoke when a lock is triggered. (`string/false`)
*
* This could be due to an invalid domain, incorrect time, or code's integrity changed.
*
* [Learn more about the rules of your countermeasures function](https://github.com/MichaelXF/js-confuser/blob/master/Countermeasures.md).
*
* Otherwise, the obfuscator falls back to crashing the process.
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
countermeasures?: string | boolean;
};
/**
* ### `movedDeclarations`
*
* Moves variable declarations to the top of the context. (`true/false`)
*
* - Potency Medium
* - Resilience Medium
* - Cost Low
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
movedDeclarations?: ProbabilityMap<boolean>;
/**
* ### `opaquePredicates`
*
* An [Opaque Predicate](https://en.wikipedia.org/wiki/Opaque_predicate) is a predicate(true/false) that is evaluated at runtime, this can confuse reverse engineers
* understanding your code. (`true/false`)
*
* - Potency Medium
* - Resilience Medium
* - Cost Low
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
opaquePredicates?: ProbabilityMap<boolean>;
/**
* ### `shuffle`
*
* Shuffles the initial order of arrays. The order is brought back to the original during runtime. (`"hash"/true/false/0-1`)
*
* - Potency Medium
* - Resilience Low
* - Cost Low
*
* | Mode | Description |
* | --- | --- |
* | `"hash"`| Array is shifted based on hash of the elements |
* | `true`| Arrays are shifted *n* elements, unshifted at runtime |
* | `false` | Feature disabled |
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
shuffle?: ProbabilityMap<boolean | "hash">;
/**
* ### `verbose`
*
* Enable logs to view the obfuscator's state. (`true/false`)
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
verbose?: boolean;
/**
* ### `globalVariables`
*
* Set of global variables. *Optional*. (`Set<string>`)
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
globalVariables?: Set<string>;
/**
* ### `debugComments`
*
* Enable debug comments. (`true/false`)
*
* [See all settings here](https://github.com/MichaelXF/js-confuser/blob/master/README.md#options)
*/
debugComments?: boolean;
}
const validProperties = new Set([
"preset",
"target",
"indent",
"compact",
"hexadecimalNumbers",
"minify",
"es5",
"renameVariables",
"renameGlobals",
"identifierGenerator",
"nameRecycling",
"controlFlowFlattening",
"hideInitializingCode",
"globalConcealing",
"stringCompression",
"stringConcealing",
"stringEncoding",
"stringSplitting",
"duplicateLiteralsRemoval",
"dispatcher",
"eval",
"rgf",
"objectExtraction",
"flatten",
"deadCode",
"calculator",
"lock",
"movedDeclarations",
"opaquePredicates",
"shuffle",
"stack",
"verbose",
"globalVariables",
"debugComments",
]);
const validOses = new Set(["windows", "linux", "osx", "ios", "android"]);
const validBrowsers = new Set([
"firefox",
"chrome",
"iexplorer",
"edge",
"safari",
"opera",
]);
export function validateOptions(options: ObfuscateOptions) {
if (Object.keys(options).length <= 1) {
/**
* Give a welcoming introduction to those who skipped the documentation.
*/
var line = `You provided zero obfuscation options. By default everything is disabled.\nYou can use a preset with:\n\n> {target: '${
options.target || "node"
}', preset: 'high' | 'medium' | 'low'}.\n\n\nView all settings here:\nhttps://github.com/MichaelXF/js-confuser#options`;
throw new Error(
`\n\n` +
line
.split("\n")
.map((x) => `\t${x}`)
.join("\n") +
`\n\n`
);
}
ok(options, "options cannot be null");
ok(
options.target,
"Missing options.target option (required, must one the following: 'browser' or 'node')"
);
ok(
["browser", "node"].includes(options.target),
`'${options.target}' is not a valid target mode`
);
Object.keys(options).forEach((key) => {
if (!validProperties.has(key)) {
throw new TypeError("Invalid option: '" + key + "'");
}
});
if (
options.target === "node" &&
options.lock &&
options.lock.browserLock &&
options.lock.browserLock.length
) {
throw new TypeError('browserLock can only be used when target="browser"');
}
if (options.lock) {
// Validate browser-lock option
if (typeof options.lock.browserLock !== "undefined") {
ok(
Array.isArray(options.lock.browserLock),
"browserLock must be an array"
);
ok(
!options.lock.browserLock.find(
(browserName) => !validBrowsers.has(browserName)
),
'Invalid browser name. Allowed: "firefox", "chrome", "iexplorer", "edge", "safari", "opera"'
);
}
// Validate os-lock option
if (typeof options.lock.osLock !== "undefined") {
ok(Array.isArray(options.lock.osLock), "osLock must be an array");
ok(
!options.lock.osLock.find((osName) => !validOses.has(osName)),
'Invalid OS name. Allowed: "windows", "linux", "osx", "ios", "android"'
);
}
// Validate domain-lock option
if (typeof options.lock.domainLock !== "undefined") {
ok(Array.isArray(options.lock.domainLock), "domainLock must be an array");
}
// Validate context option
if (typeof options.lock.context !== "undefined") {
ok(Array.isArray(options.lock.context), "context must be an array");
}
// Validate start-date option
if (
typeof options.lock.startDate !== "undefined" &&
options.lock.startDate
) {
ok(
typeof options.lock.startDate === "number" ||
options.lock.startDate instanceof Date,
"startDate must be Date object or number"
);
}
// Validate end-date option
if (typeof options.lock.endDate !== "undefined" && options.lock.endDate) {
ok(
typeof options.lock.endDate === "number" ||
options.lock.endDate instanceof Date,
"endDate must be Date object or number"
);
}
}
if (options.preset) {
if (!presets[options.preset]) {
throw new TypeError("Unknown preset of '" + options.preset + "'");
}
}
}
/**
* Corrects the user's options. Sets the default values and validates the configuration.
* @param options
* @returns
*/
export async function correctOptions(
options: ObfuscateOptions
): Promise<ObfuscateOptions> {
if (options.preset) {
// Clone and allow overriding
options = Object.assign({}, presets[options.preset], options);
}
if (!options.hasOwnProperty("debugComments")) {
options.debugComments = false; // debugComments is off by default
}
if (!options.hasOwnProperty("compact")) {
options.compact = true; // Compact is on by default
}
if (!options.hasOwnProperty("renameGlobals")) {
options.renameGlobals = true; // RenameGlobals is on by default
}
if (options.globalVariables && !(options.globalVariables instanceof Set)) {
options.globalVariables = new Set(Object.keys(options.globalVariables));
}
if (options.lock && options.lock.selfDefending) {
options.compact = true; // self defending forcibly enables this
}
// options.globalVariables was never used.
// GlobalConcealing implicitly determines a global to be a variable referenced but never defined or modified.
if (!options.hasOwnProperty("globalVariables")) {
options.globalVariables = new Set([]);
if (options.target == "browser") {
// browser
[
"window",
"document",
"postMessage",
"alert",
"confirm",
"location",
].forEach((x) => options.globalVariables.add(x));
} else {
// node
[
"global",
"Buffer",
"require",
"process",
"__dirname",
"__filename",
].forEach((x) => options.globalVariables.add(x));
}
[
"globalThis",
"console",
"parseInt",
"parseFloat",
"Math",
"Promise",
"String",
"Boolean",
"Function",
"Object",
"Array",
"Proxy",
"Error",
"setTimeout",
"clearTimeout",
"setInterval",
"clearInterval",
"setImmediate",
"clearImmediate",
"queueMicrotask",
"exports",
"module",
"isNaN",
"isFinite",
].forEach((x) => options.globalVariables.add(x));
}
return options;
}