forked from toyobayashi/electron-asar-encrypt-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostBuild.js
More file actions
127 lines (106 loc) · 3.95 KB
/
Copy pathpostBuild.js
File metadata and controls
127 lines (106 loc) · 3.95 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
const asarmor = require("asarmor");
const fs = require("fs");
const path = require("path");
/*
For some reason, the node file that ends up in resources/app.asar.unpacked/main.node is sometimes an old one from an earlier build, even though the right version is in literally every other location, including the build folder. I cannot tell why this is or where else the earlier version's file is even being copied from, but this is the only working solution I can find.
TODO: Check if this was because of the app folder instead of appbuild folder and by extension its outdated node files being referenced in the past.
*/
console.log("Fixing outdated node files");
(async () => {
fs.unlink(
path.join(__dirname, "./test/resources/app.asar.unpacked/main.node"),
(err) => {
if (err) throw err;
console.log("Unlinked main.node");
}
);
fs.unlink(
path.join(
__dirname,
"./test/resources/app.asar.unpacked/renderer.node"
),
(err) => {
if (err) throw err;
console.log("Unlinked renderer.node");
}
);
fs.copyFile(
path.join(__dirname, "./build/Release/main.node"),
path.join(__dirname, "./test/resources/app.asar.unpacked/main.node"),
(err) => {
if (err) throw err;
console.log("Updated main.node");
}
);
fs.copyFile(
path.join(__dirname, "./build/Release/renderer.node"),
path.join(
__dirname,
"./test/resources/app.asar.unpacked/renderer.node"
),
(err) => {
if (err) throw err;
console.log("Updated renderer.node");
}
);
})();
// Apply asarmor to the asar file
const generateRandomString = (length) => {
// https://stackoverflow.com/a/1349426
var result = "";
var characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(
Math.floor(Math.random() * charactersLength)
);
}
return result;
};
const randomNumber = (min, max) => {
Math.floor(Math.random() * max) + min;
};
const applyPatch = async (file) => {
console.log(`Applying asarmor to ${file}`);
const archive = await asarmor.open(file);
if (file.includes("node_modules")) {
archive.patch(
asarmor.createTrashPatch({
filenames: [
generateRandomString(randomNumber(15, 1)),
generateRandomString(randomNumber(15, 1)),
generateRandomString(randomNumber(15, 1)),
generateRandomString(randomNumber(15, 1)),
generateRandomString(randomNumber(15, 1)),
generateRandomString(randomNumber(15, 1)),
generateRandomString(randomNumber(15, 1)),
generateRandomString(randomNumber(15, 1))
]
})
);
} else {
archive.patch(
asarmor.createTrashPatch({
filenames: [
"helper.node",
"package.json",
"package-lock.json",
"settings.json",
"index.tsx",
"header.tsx",
"button.tsx",
generateRandomString(randomNumber(15, 1))
]
})
);
}
// Apply customized bloat patch.
// The bloat patch by itself will write randomness to disk on extraction attempt.
archive.patch(asarmor.createBloatPatch(1000)); // adds 1,000 GB of bloat in total
// Write changes back to disk.
const outputPath = await archive.write(file);
console.log(`Applied asarmor to ${outputPath}`);
};
applyPatch(path.join(__dirname, "./test/resources/app.asar"));
applyPatch(path.join(__dirname, "./test/resources/node_modules.asar"));