-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cjs
414 lines (386 loc) · 14.7 KB
/
server.cjs
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
const WebSocket = require('ws');
const { v4: uuidv4 } = require('uuid');
const http = require('http');
const url = require('url');
const puppeteer = require('puppeteer');
const env = require('dotenv');
const pm2 = require('pm2');
const fs = require('fs');
const { exec } = require('child_process');
const path = require('path');
env.config();
const minimal_args = [
'--autoplay-policy=user-gesture-required',
'--disable-background-networking',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-breakpad',
'--disable-client-side-phishing-detection',
'--disable-component-update',
'--disable-default-apps',
'--disable-dev-shm-usage',
'--disable-domain-reliability',
'--disable-extensions',
'--disable-features=AudioServiceOutOfProcess',
'--disable-hang-monitor',
'--disable-ipc-flooding-protection',
'--disable-notifications',
'--disable-offer-store-unmasked-wallet-cards',
'--disable-popup-blocking',
'--disable-print-preview',
'--disable-prompt-on-repost',
'--disable-renderer-backgrounding',
'--disable-speech-api',
'--disable-sync',
'--hide-scrollbars',
'--ignore-gpu-blacklist',
'--metrics-recording-only',
'--mute-audio',
'--no-default-browser-check',
'--no-first-run',
'--no-pings',
'--no-zygote',
'--password-store=basic',
'--use-gl=swiftshader',
'--use-mock-keychain',
];
const wss = new WebSocket.Server({ port: process.env.VITE_WS_PORT });
console.log(`WebSocket server started on port ${process.env.VITE_WS_PORT}`);
const PORT = process.env.VITE_RPC_PORT;
let connectedClient = null;
let puppeteerPage = null;
let LOGSARRAY = [];
function pushToLimitedArray(arr, item, limit = 1000) {
if (arr.length >= limit) {
arr.shift(); // Remove the first element
}
arr.push(item);
return arr;
}
wss.on('connection', (ws) => {
console.log(
`WebSocket client connection received. Ready to receive RPC requests at http://127.0.0.1:${PORT}/rpc`,
);
connectedClient = ws;
// ws.on('message', (message, isBinary) => {
// message = isBinary ? message : message.toString();
// console.log('Received message:', message);
// ws.send(message);
// });
connectedClient.on('message', (message) => {
const receivedMessage = message.toString();
const receivedMessageObj = JSON.parse(receivedMessage);
if (receivedMessageObj.logObject && receivedMessageObj.logObject.line) {
pushToLimitedArray(LOGSARRAY, receivedMessageObj, process.env.VITE_LOGS_LIMIT);
fs.appendFileSync(
process.env.MM2_LOG_FILE,
receivedMessageObj.logObject.time + receivedMessageObj.logObject.line + '\n',
);
}
});
ws.on('close', () => {
console.log('WebSocket client disconnected');
connectedClient = null;
});
});
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
if (req.method === 'GET') {
if (parsedUrl.pathname === '/status') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
status: 'success',
message: {
rpc_server_running: true,
ws_client_connected:
connectedClient && connectedClient.readyState === WebSocket.OPEN ? true : false,
},
}),
);
} else if (parsedUrl.pathname === '/logs') {
const query = parsedUrl.query;
const limit = query.limit ? parseInt(query.limit, 10) : LOGSARRAY.length;
const logsToReturn = LOGSARRAY.slice(-limit);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'success', message: logsToReturn }));
}
} else if (req.method === 'POST') {
if (parsedUrl.pathname === '/admin') {
let body = '';
req.on('data', (chunk) => {
body += chunk.toString();
});
req.on('end', () => {
body = JSON.parse(body);
if (body.action === 'reload_kdf_page') {
let encoded_coins_json_url;
let encoded_mm2_conf;
if (body.coins_json_url) {
encoded_coins_json_url = btoa(body.coins_json_url);
}
if (body.mm2_conf) {
encoded_mm2_conf = btoa(JSON.stringify(body.mm2_conf));
}
if (encoded_coins_json_url || encoded_mm2_conf) {
puppeteerPage.goto(
`http://127.0.0.1:${process.env.VITE_WEB_PORT}/${encoded_mm2_conf ? '?mm2_conf=' + encoded_mm2_conf : ''}${encoded_coins_json_url ? '&coins_json_url=' + encoded_coins_json_url : ''}`,
);
} else {
puppeteerPage.reload();
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'success', action: 'reload_kdf_page' }));
} else if (body.action === 'restart_kdf') {
if (connectedClient && connectedClient.readyState === WebSocket.OPEN) {
const uuid = uuidv4();
const message = JSON.stringify({
action: 'restart_kdf',
mm2_conf: body.mm2_conf,
coins_json_url: body.coins_json_url,
uuid: uuid,
});
connectedClient.send(message);
connectedClient.on('message', (message) => {
const receivedMessage = message.toString();
const receivedMessageObj = JSON.parse(receivedMessage);
if (receivedMessageObj.message?.action && receivedMessageObj.uuid === uuid) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
status: 'success',
action: receivedMessageObj.message.action,
}),
);
}
});
} else {
res.writeHead(503, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
status: 'error',
message: 'Webpage running KDF is not connected to websocket',
}),
);
}
} else if (body.action === 'update_wasm_lib') {
const wasm_lib_url = body.wasm_lib_url;
const zipfile_name = wasm_lib_url.split('/').pop();
const kdf_zips_dir = path.join(__dirname, 'kdf_zips');
const zipfile_path = path.join(kdf_zips_dir, zipfile_name);
// Ensure kdf_zips directory exists
if (!fs.existsSync(kdf_zips_dir)) {
fs.mkdirSync(kdf_zips_dir);
}
if (fs.existsSync(zipfile_path)) {
console.log(`Zip file ${zipfile_name} already exists. Skipping download.`);
processZipFile(zipfile_path, kdf_zips_dir, res);
} else {
fetch(wasm_lib_url)
.then((response) => {
const totalSize = parseInt(response.headers.get('content-length'), 10);
let downloadedSize = 0;
console.log(
`Starting download of ${zipfile_name}. Total size: ${(totalSize / 1024 / 1024).toFixed(2)} MB`,
);
const reader = response.body.getReader();
return new Response(
new ReadableStream({
async start(controller) {
let previousProgress = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
downloadedSize += value.length;
const progress = ((downloadedSize / totalSize) * 100).toFixed(2);
const downloadedMB = (downloadedSize / 1024 / 1024).toFixed(2);
const totalMB = (totalSize / 1024 / 1024).toFixed(2);
// Only log progress if it has increased by 10% or more
if (progress - previousProgress >= 10) {
console.log(
`Downloaded ${downloadedMB} MB of ${totalMB} MB (${progress}%)`,
);
previousProgress = progress;
}
controller.enqueue(value);
}
controller.close();
},
}),
).arrayBuffer();
})
.then((arrayBuffer) => {
const buffer = Buffer.from(arrayBuffer);
const zipfile_path = path.join(kdf_zips_dir, zipfile_name);
fs.writeFileSync(zipfile_path, buffer);
console.log(`Download complete. File saved to ${zipfile_path}`);
processZipFile(zipfile_path, kdf_zips_dir, res);
})
.catch((error) => {
console.error(`Fetch error: ${error}`);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({ status: 'error', message: 'Failed to download WASM library' }),
);
});
}
} else {
res.writeHead(405, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'error', message: 'Action not found/not recognized' }));
}
});
} else if (parsedUrl.pathname === '/rpc') {
let body = '';
req.on('data', (chunk) => {
body += chunk.toString();
});
req.on('end', () => {
if (connectedClient && connectedClient.readyState === WebSocket.OPEN) {
const uuid = uuidv4();
const message = JSON.stringify({ message: body, uuid: uuid });
connectedClient.send(message);
connectedClient.on('message', (message) => {
const receivedMessage = message.toString();
const receivedMessageObj = JSON.parse(receivedMessage);
if (receivedMessageObj.uuid === uuid) {
res.writeHead(200, { 'Content-Type': 'application/json' });
//res.end(JSON.stringify({ status: 'success', message: receivedMessageObj.message }));
res.end(JSON.stringify(receivedMessageObj.message));
}
});
} else {
res.writeHead(503, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({ status: 'error', message: 'Webpage with KDF lib is not running' }),
);
}
});
} else {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'error', message: 'Endpoint not found' }));
}
} else {
res.writeHead(405, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'error', message: 'Method not allowed' }));
}
});
server.listen(PORT, () => {
console.log(`RPC server started on port ${PORT}, please wait`);
});
(async () => {
console.log('Starting browser');
const browser = await puppeteer.launch({
executablePath: '/usr/bin/google-chrome',
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
puppeteerPage = await browser.newPage();
console.log('Chrome Page created');
checkIfWebServerIsRunning(async () => {
await puppeteerPage.goto(`http://127.0.0.1:${process.env.VITE_WEB_PORT}`);
console.log('KDF Page loaded successfully');
});
})();
async function checkIfWebServerIsRunning(cb) {
const maxRetries = 50;
const retryDelay = 2000; // 2 seconds
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
// Check if the endpoint is active
await new Promise((resolve, reject) => {
const req = http.get(`http://127.0.0.1:${process.env.VITE_WEB_PORT}`, (res) => {
if (res.statusCode === 200) {
resolve();
} else {
reject(new Error(`Web server responded with status code ${res.statusCode}`));
}
});
req.on('error', reject);
req.end();
});
// If the check passes, callback
await cb();
break; // Exit the loop if successful
} catch (error) {
console.error(`Checking web server status: attempt ${attempt} failed: ${error.message}`);
if (attempt === maxRetries) {
console.error("Max retries reached. Web server didn't start.");
await browser.close();
process.exit(1);
} else {
console.log(`Retrying in ${retryDelay / 1000} seconds...`);
await new Promise((resolve) => setTimeout(resolve, retryDelay));
}
}
}
}
function processZipFile(zipfile_name, kdf_zips_dir, res) {
// Extract version from filename
const basename = path.basename(zipfile_name, '.zip');
const temp = basename.split('_').slice(1).join('_');
const version = temp.split('-')[0];
// Unzip and process files
console.log('Extracting and processing files...');
exec(
`
cd "${kdf_zips_dir}" &&
mkdir -p temp &&
unzip -o "${zipfile_name}" -d temp &&
cd temp &&
mv kdflib.js ../../js/kdflib.js &&
mv kdflib.d.ts ../../js/kdflib.d.ts &&
rm -rf ../../js/snippets/*
cp -r snippets/* ../../js/snippets/
mv kdflib_bg.wasm ../../public/kdflib_bg_${version}.wasm &&
cd .. &&
rm -rf temp
`,
(error, stdout, stderr) => {
if (error) {
console.error(`Extraction error: ${error}`);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
status: 'error',
message: 'Failed to extract WASM library',
}),
);
} else {
// Update .env file
const envPath = path.join(__dirname, '.env');
let envContent = fs.readFileSync(envPath, 'utf8');
envContent = envContent.replace(
/VITE_WASM_BIN=.*/,
`VITE_WASM_BIN=kdflib_bg_${version}.wasm`,
);
fs.writeFileSync(envPath, envContent);
pm2.connect((err) => {
if (err) {
console.error('Error connecting to PM2:', err);
process.exit(2);
}
// Restart a specific process by name
pm2.restart('web-server', (err) => {
if (err) {
console.error('[web-server]: Error restarting process:', err);
} else {
console.log('[web-server]:Process restarted successfully');
}
pm2.disconnect();
checkIfWebServerIsRunning(async () => {
await puppeteerPage.reload();
console.log('KDF Page reloaded successfully');
});
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
status: 'success',
action: 'update_wasm_lib',
version: version,
}),
);
});
});
}
},
);
}