forked from webaverse/metachromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
78 lines (74 loc) · 2.42 KB
/
index.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
const path = require('path');
const http = require('http');
const crypto = require('crypto');
const child_process = require('child_process');
const express = require('express');
const httpPort = 48080;
const wsPort = 48081;
const app = express();
app.use(express.static(__dirname));
http.createServer(app)
.listen(httpPort, async err => {
if (!err) {
let qrvrProcess, chromeProcess;
const u = process.argv.slice(2).find(arg => !/^--/.test(arg)) || `https://home.metachromium.com/`;
const qrFlag = process.argv.slice(2).some(arg => arg === '--qr');
const key = crypto.randomBytes(32).toString('hex');
const _startQr = () => new Promise((accept, reject) => {
qrvrProcess = child_process.fork(
path.join(__dirname, 'child.js'),
[
wsPort,
],
{
stdio: 'pipe',
cwd: path.dirname(require.resolve('qrvr')),
}
);
qrvrProcess.on('exit', _exit);
qrvrProcess.stdout.setEncoding('utf8');
const _data = d => {
if (/started/.test(d)) {
qrvrProcess.stdout.removeListener('data', _data);
accept();
}
};
qrvrProcess.stdout.on('data', _data);
qrvrProcess.stdout.pipe(process.stdout);
qrvrProcess.stderr.pipe(process.stderr);
});
const _startChrome = () => {
chromeProcess = child_process.spawn(
'./Chrome-bin/chrome.exe',
[
'--no-sandbox',
'--disable-web-security',
'--test-type',
'--disable-xr-device-consent-prompt-for-testing',
'--disable-gesture-requirement-for-presentation',
'--use-fake-ui-for-media-stream',
'--no-user-gesture-required',
'--autoplay-policy=no-user-gesture-required',
`--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36 Mchr/1.0 (${wsPort}, ${key})`,
u,
],
{
shell: false,
}
);
chromeProcess.on('exit', _exit);
};
function _exit() {
qrvrProcess && qrvrProcess.kill();
chromeProcess && chromeProcess.kill();
process.kill(process.pid);
}
if (qrFlag) {
await _startQr();
}
_startChrome();
} else {
console.warn(err.stack);
process.exit(1);
}
});