forked from UseInterstellar/Interstellar-Astro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomize.ts
More file actions
458 lines (387 loc) · 16.3 KB
/
randomize.ts
File metadata and controls
458 lines (387 loc) · 16.3 KB
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
import { execSync } from "node:child_process";
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
interface FileMapping {
original: string;
randomized: string;
type: "file" | "route" | "favicon";
}
interface ObfuscatorConfig {
enabled: boolean;
preserveExtensions?: string[];
directories?: string[];
}
class BuildObfuscator {
private mappings = new Map<string, FileMapping>();
private usedNames = new Set<string>();
private root: string;
private config: ObfuscatorConfig;
constructor(config: ObfuscatorConfig = { enabled: true }) {
this.root = process.cwd();
this.config = {
preserveExtensions: [".json", ".css", ".scss"],
directories: ["src/components", "src/layouts", "src/lib", "src/pages"],
...config,
};
}
private generateName(extension: string, isRoute: boolean = false): string {
let name: string;
do {
if (isRoute) {
name = this.generateRealisticRouteName();
} else {
const strategy = Math.random();
if (strategy < 0.25) {
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
name = chars[Math.floor(Math.random() * chars.length)];
} else if (strategy < 0.5) {
const length = Math.floor(Math.random() * 3) + 2;
name = crypto.randomBytes(length).toString("base64url").slice(0, length);
} else if (strategy < 0.75) {
const length = Math.floor(Math.random() * 8) + 5;
name = crypto.randomBytes(length).toString("base64url").slice(0, length);
} else {
const segments = [crypto.randomBytes(4).toString("hex"), crypto.randomBytes(2).toString("hex"), crypto.randomBytes(2).toString("hex"), crypto.randomBytes(2).toString("hex"), crypto.randomBytes(6).toString("hex")];
name = segments.join("-");
}
}
} while (this.usedNames.has(name));
this.usedNames.add(name);
return `${name}${extension}`;
}
private generateRealisticRouteName(): string {
const routeStyles = [
() =>
this.pickRandom([
"dashboard",
"profile",
"settings",
"account",
"preferences",
"notifications",
"messages",
"inbox",
"archive",
"favorites",
"history",
"activity",
"analytics",
"reports",
"downloads",
"uploads",
"gallery",
"library",
"collection",
"playlist",
"workspace",
"projects",
"tasks",
"calendar",
"schedule",
"contacts",
"teams",
"members",
"groups",
"communities",
"explore",
"discover",
"trending",
"featured",
"popular",
"search",
"results",
"filter",
"sort",
"categories",
"tags",
"labels",
"bookmarks",
"saved",
"drafts",
"published",
"pending",
"reviews",
"feedback",
"support",
"help",
"docs",
"guides",
"tutorials",
"faq",
]),
() => {
const part1 = this.pickRandom(["user", "admin", "my", "view", "edit", "new", "create"]);
const part2 = this.pickRandom(["profile", "settings", "dashboard", "content", "posts", "items"]);
return `${part1}-${part2}`;
},
() => {
const prefix = this.pickRandom(["item", "post", "page", "doc", "file", "media"]);
const id = Math.floor(Math.random() * 10000);
return `${prefix}-${id}`;
},
() => {
const length = Math.floor(Math.random() * 2) + 2;
return crypto.randomBytes(length).toString("hex").slice(0, length);
},
() => this.pickRandom(["login", "logout", "signup", "register", "reset", "verify", "confirm", "activate", "deactivate", "delete", "create", "update", "edit", "remove", "add"]),
];
const generator = this.pickRandom(routeStyles);
return generator();
}
private pickRandom<T>(array: T[]): T {
return array[Math.floor(Math.random() * array.length)];
}
private findFiles(directory: string, extensions: string[]): string[] {
const results: string[] = [];
if (!fs.existsSync(directory)) return results;
const traverse = (dir: string) => {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
traverse(fullPath);
} else if (extensions.includes(path.extname(entry.name))) {
results.push(fullPath);
}
}
};
traverse(directory);
return results;
}
private processFavicons(): void {
const faviconDir = path.join(this.root, "public", "assets", "media", "favicons");
if (!fs.existsSync(faviconDir)) return;
const favicons = fs.readdirSync(faviconDir);
for (const file of favicons) {
const originalPath = path.join(faviconDir, file);
const randomizedName = this.generateName(path.extname(file));
const newPath = path.join(faviconDir, randomizedName);
this.mappings.set(originalPath, {
original: originalPath,
randomized: newPath,
type: "favicon",
});
}
}
private processSourceFiles(): void {
const directories = this.config.directories || ["src/components", "src/layouts", "src/lib", "src/pages"];
for (const dir of directories) {
const dirPath = path.join(this.root, dir);
if (!fs.existsSync(dirPath)) continue;
const files = this.findFiles(dirPath, [".astro", ".ts", ".tsx", ".js", ".jsx"]);
for (const file of files) {
if (path.basename(file) === "index.astro" || this.config.preserveExtensions?.includes(path.extname(file))) {
continue;
}
const isRoute = file.includes("/pages/");
const randomizedName = this.generateName(path.extname(file), isRoute);
const newPath = path.join(path.dirname(file), randomizedName);
const mapping: FileMapping = {
original: file,
randomized: newPath,
type: isRoute ? "route" : "file",
};
this.mappings.set(file, mapping);
}
}
}
private updateFileContents(): void {
const filesToUpdate = this.findFiles(path.join(this.root, "src"), [".astro", ".ts", ".tsx", ".js", ".jsx"]);
for (const file of filesToUpdate) {
let content = fs.readFileSync(file, "utf-8");
let modified = false;
for (const [originalPath, mapping] of this.mappings) {
const originalName = path.basename(mapping.original);
const newName = path.basename(mapping.randomized);
const originalNameNoExt = path.basename(mapping.original, path.extname(mapping.original));
const newNameNoExt = path.basename(mapping.randomized, path.extname(mapping.randomized));
if (mapping.type === "favicon") {
const faviconName = path.basename(originalPath);
const patterns = [
{ from: `/assets/media/favicons/${faviconName}`, to: `/assets/media/favicons/${newName}` },
{ from: `assets/media/favicons/${faviconName}`, to: `assets/media/favicons/${newName}` },
{ from: `'${faviconName}'`, to: `'${newName}'` },
{ from: `"${faviconName}"`, to: `"${newName}"` },
];
for (const { from, to } of patterns) {
if (content.includes(from)) {
content = content.replaceAll(from, to);
modified = true;
}
}
} else {
const relDir = path.dirname(mapping.original).replace(path.join(this.root, "src"), "");
const componentType = relDir.includes("/components") ? "components" : relDir.includes("/layouts") ? "layouts" : relDir.includes("/lib") ? "lib" : relDir.includes("/pages") ? "pages" : null;
if (componentType) {
const patterns = `@/${componentType}/${this.escapeRegex(originalName)}`;
const regex = new RegExp(`(['"\`])${patterns}(['"\`])`, "g");
const replacement = `$1@/${componentType}/${newName}$2`;
let newContent = content.replace(regex, replacement);
if (newContent !== content) {
content = newContent;
modified = true;
}
const noExtPattern = `@/${componentType}/${this.escapeRegex(originalNameNoExt)}`;
const noExtRegex = new RegExp(`(['"\`])${noExtPattern}(['"\`])`, "g");
const noExtReplacement = `$1@/${componentType}/${newName}$2`;
newContent = content.replace(noExtRegex, noExtReplacement);
if (newContent !== content) {
content = newContent;
modified = true;
}
const scriptSrcPattern = `src\\s*=\\s*(['"\`])@/${componentType}/${this.escapeRegex(originalName)}\\1`;
const scriptSrcRegex = new RegExp(scriptSrcPattern, "g");
const scriptSrcReplacement = `src=$1@/${componentType}/${newName}$1`;
newContent = content.replace(scriptSrcRegex, scriptSrcReplacement);
if (newContent !== content) {
content = newContent;
modified = true;
}
const scriptSrcNoExtPattern = `src\\s*=\\s*(['"\`])@/${componentType}/${this.escapeRegex(originalNameNoExt)}\\1`;
const scriptSrcNoExtRegex = new RegExp(scriptSrcNoExtPattern, "g");
const scriptSrcNoExtReplacement = `src=$1@/${componentType}/${newName}$1`;
newContent = content.replace(scriptSrcNoExtRegex, scriptSrcNoExtReplacement);
if (newContent !== content) {
content = newContent;
modified = true;
}
const absoluteSrcPattern = `(src\\s*=\\s*['"\`])/src/${componentType}/${this.escapeRegex(originalName)}(['"\`])`;
const absoluteSrcRegex = new RegExp(absoluteSrcPattern, "g");
const absoluteSrcReplacement = `$1/src/${componentType}/${newName}$2`;
newContent = content.replace(absoluteSrcRegex, absoluteSrcReplacement);
if (newContent !== content) {
content = newContent;
modified = true;
}
const absoluteSrcNoExtPattern = `(src\\s*=\\s*['"\`])/src/${componentType}/${this.escapeRegex(originalNameNoExt)}(['"\`])`;
const absoluteSrcNoExtRegex = new RegExp(absoluteSrcNoExtPattern, "g");
const absoluteSrcNoExtReplacement = `$1/src/${componentType}/${newName}$2`;
newContent = content.replace(absoluteSrcNoExtRegex, absoluteSrcNoExtReplacement);
if (newContent !== content) {
content = newContent;
modified = true;
}
}
const currentFileDir = path.dirname(file);
let relativePath = path.relative(currentFileDir, mapping.original);
relativePath = relativePath.replace(/\\/g, "/");
if (!relativePath.startsWith(".")) {
relativePath = `./${relativePath}`;
}
const regex = new RegExp(`(['"\`])${this.escapeRegex(relativePath)}(['"\`])`, "g");
let newRelativePath = path.relative(currentFileDir, mapping.randomized);
newRelativePath = newRelativePath.replace(/\\/g, "/");
if (!newRelativePath.startsWith(".")) {
newRelativePath = `./${newRelativePath}`;
}
let newContent = content.replace(regex, `$1${newRelativePath}$2`);
if (newContent !== content) {
content = newContent;
modified = true;
}
const relativePathNoExt = relativePath.replace(path.extname(relativePath), "");
const noExtRegex = new RegExp(`(['"\`])${this.escapeRegex(relativePathNoExt)}(['"\`])`, "g");
const newRelativePathWithExt = newRelativePath;
newContent = content.replace(noExtRegex, `$1${newRelativePathWithExt}$2`);
if (newContent !== content) {
content = newContent;
modified = true;
}
const scriptRelativePattern = `src\\s*=\\s*(['"\`])${this.escapeRegex(relativePath)}\\1`;
const scriptRelativeRegex = new RegExp(scriptRelativePattern, "g");
newContent = content.replace(scriptRelativeRegex, `src=$1${newRelativePath}$1`);
if (newContent !== content) {
content = newContent;
modified = true;
}
const scriptRelativeNoExtPattern = `src\\s*=\\s*(['"\`])${this.escapeRegex(relativePathNoExt)}\\1`;
const scriptRelativeNoExtRegex = new RegExp(scriptRelativeNoExtPattern, "g");
newContent = content.replace(scriptRelativeNoExtRegex, `src=$1${newRelativePath}$1`);
if (newContent !== content) {
content = newContent;
modified = true;
}
if (mapping.type === "route") {
const routeReplacements = [
{ pattern: new RegExp(`(['"\`])/${this.escapeRegex(originalNameNoExt)}(['"\`])`, "g"), replacement: `$1/${newNameNoExt}$2` },
{ pattern: new RegExp(`(href\\s*=\\s*['"\`])/${this.escapeRegex(originalNameNoExt)}(['"\`])`, "g"), replacement: `$1/${newNameNoExt}$2` },
{ pattern: new RegExp(`===\\s*['"\`]/${this.escapeRegex(originalNameNoExt)}['"\`]`, "g"), replacement: `=== '/${newNameNoExt}'` },
{ pattern: new RegExp(`['"\`]/${this.escapeRegex(originalNameNoExt)}['"\`]\\s*===`, "g"), replacement: `'/${newNameNoExt}' ===` },
];
for (const { pattern, replacement } of routeReplacements) {
const newContent = content.replace(pattern, replacement);
if (newContent !== content) {
content = newContent;
modified = true;
}
}
}
}
}
if (modified) {
fs.writeFileSync(file, content, "utf-8");
}
}
}
private renameFiles(): void {
const sortedMappings = Array.from(this.mappings.values()).sort((a, b) => b.original.split(path.sep).length - a.original.split(path.sep).length);
for (const mapping of sortedMappings) {
if (fs.existsSync(mapping.original)) {
fs.renameSync(mapping.original, mapping.randomized);
console.log(`Renamed: ${path.relative(this.root, mapping.original)} → ${path.basename(mapping.randomized)}`);
}
}
}
private escapeRegex(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
public async obfuscate(): Promise<void> {
if (!this.config.enabled) {
console.log("Build obfuscation is disabled in config.");
return;
}
console.log("Starting build obfuscation...");
this.processFavicons();
this.processSourceFiles();
console.log(`Found ${this.mappings.size} files to obfuscate`);
this.updateFileContents();
this.renameFiles();
const routeMappings: Record<string, string> = {};
for (const [, mapping] of this.mappings) {
if (mapping.type === "route") {
const originalRoute = `/${path.basename(mapping.original, path.extname(mapping.original))}`;
const newRoute = `/${path.basename(mapping.randomized, path.extname(mapping.randomized))}`;
routeMappings[originalRoute] = newRoute;
}
}
if (Object.keys(routeMappings).length > 0) {
console.log("Routes updated:");
for (const [old, newRoute] of Object.entries(routeMappings)) {
console.log(` ${old} → ${newRoute}`);
}
}
console.log("Obfuscation completed!");
}
public async revert(): Promise<void> {
try {
console.log("Reverting changes...");
execSync("git restore src/ public/", { cwd: this.root, stdio: "inherit" });
execSync("git clean -fdx src/ public/", { cwd: this.root, stdio: "inherit" });
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log("Revert completed.");
} catch (error) {
console.error("Error while reverting:", error instanceof Error ? error.message : error);
throw error;
}
}
}
export async function Main(config?: ObfuscatorConfig): Promise<void> {
const obfuscator = new BuildObfuscator(config);
await obfuscator.obfuscate();
}
export async function Revert(): Promise<void> {
const obfuscator = new BuildObfuscator();
await obfuscator.revert();
}
export default BuildObfuscator;