forked from anadius/gd-efc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
237 lines (207 loc) · 6.65 KB
/
build.js
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
const ENCRYPT_FUNCTION_STATIC = `{COMMON_JS}
async function encryptId(folderId) {
return encrypt(folderId, "{KEY}");
}`;
const ENCRYPT_FUNCTION_SERVER = `async function encryptId(folderId) {
const result = await fetch("encrypt", {method: "POST", body: JSON.stringify({folder: folderId})});
const data = await result.json();
if(result.ok) {
return data.data;
}
else {
throw Error(data.reason);
}
}`;
const ENCRYPT_PARSER = `
async function encrypt_(data) {
return encrypt(data.folder, KEY);
}
`;
const ENCRYPT_GET = `
else if(request.method === "GET" && request.url.endsWith("/encrypt")) {
return new Response(\`{ENCRYPT_HTML}\`, {status: 200, headers: {"Content-Type": "text/html; charset=UTF-8"}});
}`;
const ENCRYPT_POST = `
else if(request.url.endsWith("/encrypt")) {
parser = encrypt_;
}`;
const saveAsFile = (filename, blob) => {
const a = document.createElement("a");
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
a.style = "display: none";
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
a.remove();
};
const parseLines = text => {
const goodLines = new Set();
for(const [, url] of text.matchAll(/^\s*(https?:\/\/.+?)\s*$/gm)) {
goodLines.add(url);
}
return goodLines;
};
const parseServer = url => {
const m = url.match(/^(https?):\/\/(.+?)\/?$/);
if(m[1] === "https") {
return m[2];
}
else { // this must be http, `parseLines` won't allow anything else
return `i:${m[2]}`;
}
};
const parseServerList = url => {
let m = url.match(/^https:\/\/gist\.(?:github|githubusercontent)\.com\/([^\/]+\/[0-9a-f]+)\/raw\/[0-9a-f]+\/(.+?)$/);
if(m !== null) {
return `g:${m[1]}<${m[2]}`;
}
m = url.match(/^https:\/\/p\.teknik\.io\/Raw\/(.+?)$/);
if(m !== null) {
return `t:${m[1]}`;
}
m = url.match(/^https:\/\/pastebin\.com\/raw\/(.+?)$/);
if(m !== null) {
return `p:${m[1]}`;
}
m = url.match(/^(https?):\/\/(.+?)$/);
if(m[1] === "https") {
return `l:${m[2]}`;
}
else { // this must be http, `parseLines` won't allow anything else
return `k:${m[2]}`;
}
};
const getFile = async path => {
return (await fetch(path)).text();
};
const addSection = (name, content) => `<h3>${name}</h3><pre>${content}</pre>`;
const parseForm = async () => {
const ALLOW_SITES = "*";
const decryptServers = parseLines($("#decrypt_servers").val());
const decryptServerLists = parseLines($("#decrypt_server_lists").val());
const urlsAndOptions = [];
for(const url of decryptServers) {
urlsAndOptions.push(parseServer(url));
}
for(const url of decryptServerLists) {
urlsAndOptions.push(parseServerList(url));
}
if(urlsAndOptions.length === 0) {
throw Error("No decode servers");
}
const ENCODED_PREFIX = btoa(urlsAndOptions.join(";"));
const KEY = $("#encryption_key").val();
try {
if(b64.base64ToBytes(KEY).length !== 32) {
throw Error("wrong key length");
}
}
catch(e) {
throw Error(`Invalid encryption key; ${e.message}`);
}
const OPTIONS = [];
if(decryptServers.size > 0) {
OPTIONS.push(addSection("Decryption servers", Array.from(decryptServers).join("\n")));
}
if(decryptServerLists.size > 0) {
OPTIONS.push(addSection("Decryption server lists", Array.from(decryptServerLists).join("\n")));
}
const OPTIONS_USED = `<div class="card-body">${OPTIONS.join("")}</div>`;
const zip = new JSZip();
zip.file("key.txt", KEY);
const COMMON_JS = await getFile("common.js");
const ENCRYPT_HTML = await getFile("templates/encrypt.html.template");
zip.file("STATIC_ENCRYPTION/encrypt.html", ENCRYPT_HTML
.replace("{ENCRYPT_FUNCTION}", ENCRYPT_FUNCTION_STATIC)
.replace("{COMMON_JS}", COMMON_JS)
.replace("{KEY}", KEY)
.replace("{OPTIONS_USED}", OPTIONS_USED)
.replace("{ENCODED_PREFIX}", ENCODED_PREFIX)
);
if($("#worker_decrypt").is(":checked")) {
let DECRYPT_WORKER = await getFile("templates/worker.js.template");
if($("#worker_encrypt").is(":checked")) {
DECRYPT_WORKER = DECRYPT_WORKER
.replace("{ENCRYPT_PARSER}", ENCRYPT_PARSER)
.replace("{ENCRYPT_GET}", ENCRYPT_GET)
.replace("{ENCRYPT_POST}", ENCRYPT_POST)
.replace("{ENCRYPT_HTML}", ENCRYPT_HTML.replace(/\\/g, "\\\\"))
.replace("{ENCRYPT_FUNCTION}", ENCRYPT_FUNCTION_SERVER)
.replace("{OPTIONS_USED}", "")
.replace("{ENCODED_PREFIX}", ENCODED_PREFIX);
}
else {
DECRYPT_WORKER = DECRYPT_WORKER
.replace("{ENCRYPT_PARSER}", "")
.replace("{ENCRYPT_GET}", "")
.replace("{ENCRYPT_POST}", "");
}
zip.file("WORKER/worker.js", DECRYPT_WORKER
.replace("{COMMON_JS}", COMMON_JS)
.replace("{KEY}", KEY)
.replace("{ALLOW_SITES}", ALLOW_SITES)
);
}
if($("#php_decrypt").is(":checked")) {
const DECRYPT_PHP = await getFile("templates/decrypt.php.template");
const ENCRYPT_PHP_FUNCTION = await getFile("templates/encrypt_function.php.template");
zip.file("PHP/decrypt.php", DECRYPT_PHP
.replace("{KEY}", KEY)
.replace("{ENCRYPT_PHP_FUNCTION}", ENCRYPT_PHP_FUNCTION)
.replace("{ALLOW_SITES}", ALLOW_SITES)
);
let htaccess = `Options -Indexes
RewriteEngine on
RewriteRule "^info$" "decrypt.php?page=info"
RewriteRule "^clone$" "decrypt.php?page=clone"
`;
if($("#php_encrypt").is(":checked")) {
htaccess += `RewriteRule "^encrypt$" "encrypt.php"
`;
const ENCRYPT_PHP = await getFile("templates/encrypt.php.template");
zip.file("PHP/encrypt.php", ENCRYPT_PHP
.replace("{KEY}", KEY)
.replace("{ENCRYPT_PHP_FUNCTION}", ENCRYPT_PHP_FUNCTION)
.replace("{ENCRYPT_HTML}", ENCRYPT_HTML)
.replace("{ENCRYPT_FUNCTION}", ENCRYPT_FUNCTION_SERVER)
.replace("{OPTIONS_USED}", "")
.replace("{ENCODED_PREFIX}", ENCODED_PREFIX)
);
}
zip.file("PHP/.htaccess", htaccess);
}
const content = await zip.generateAsync({type:"blob"});
saveAsFile("gd.zip", content);
};
$("#generate_key").click(async function(event) {
event.preventDefault();
$("#encryption_key").val(await generateKey());
});
$("#download_files").click(async function(event) {
event.preventDefault();
try {
await parseForm();
}
catch(e) {
console.error(e);
alert(e.message);
}
});
$("#worker_decrypt").change(function(event) {
if(event.delegateTarget.checked) {
$("#worker_encrypt").removeAttr("disabled");
}
else {
$("#worker_encrypt").attr("disabled", "").prop("checked", false);
}
});
$("#php_decrypt").change(function(event) {
if(event.delegateTarget.checked) {
$("#php_encrypt").removeAttr("disabled");
}
else {
$("#php_encrypt").attr("disabled", "").prop("checked", false);
}
});