forked from reisxd/revanced-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
217 lines (191 loc) · 5.21 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
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
const { createServer } = require('node:http');
const { join } = require('node:path');
const exec = require('./utils/promisifiedExec.js');
const uploadAPKFile = require('./utils/uploadAPKFile.js');
const Express = require('express');
const fileUpload = require('express-fileupload');
const { WebSocketServer } = require('ws');
const open_ = require('open');
const pf = require('portfinder');
const killProcess = require('kill-process-by-name');
const {
updateFiles,
selectApp,
getPatches,
selectPatches,
getAppVersion,
checkFileAlreadyExists,
selectAppVersion,
patchApp,
checkForUpdates,
getDevices,
setDevice,
installReVanced,
getApp,
getSettings,
setSettings
} = require('./wsEvents/index.js');
const app = Express();
const server = createServer(app);
const wsServer = new WebSocketServer({ server });
const wsClients = [];
app.use(fileUpload());
app.use(Express.static(join(__dirname, 'public')));
app.get('/revanced.apk', (_, res) => {
const file = join(process.cwd(), 'revanced', global.outputName);
res.download(file);
});
app.post('/uploadApk', (req, res) => {
req.socket.setTimeout(60000 * 60);
req.setTimeout(60000 * 60);
uploadAPKFile(req, res, wsClients);
});
/**
* @param {number} port
*/
const open = async (port) => {
if (process.platform === 'android')
await exec(`termux-open-url http://localhost:${port}`);
else await open_(`http://localhost:${port}`);
};
/**
* @param {string} msg
*/
const log = (msg, newline = true, tag = true) => {
if (newline) console.log(`${tag ? '[builder] ' : ''}${msg}`);
else process.stdout.write(`${tag ? '[builder] ' : ''}${msg} `);
};
/**
* @param {number} port
*/
const listen = (port) => {
server.listen(port, async () => {
if (process.argv.includes('--no-open'))
log(`The webserver is now running at http://localhost:${port}`);
else {
log('The webserver is now running!');
try {
log('Opening the app in the default browser...', false);
await open(port);
log('Done, check if a browser window has opened', true, false);
} catch {
log(
`Failed. Open up http://localhost:${port} manually in your browser.`,
true,
false
);
}
}
});
};
/**
* @param {import('http').Server} svr
*/
const cleanExit = async (svr) => {
log('Killing any dangling processes...', false);
try {
['adb', 'java', 'aapt2'].forEach((p) => killProcess(p));
log('Done.', true, false);
} catch (error) {
log('Failed.', true, false);
log(
'If there are certain processes still running, you can kill them manually'
);
log(error?.stack, true, false);
}
log('Stopping the server...', false);
svr.close(() => log('Done', true, false));
setTimeout(() => process.exit(0), 2_500);
};
pf.getPortPromise()
.then((freePort) => {
log(`Listening at port ${freePort}`);
listen(freePort);
})
.catch((err) => {
log(`Unable to determine free ports.\nReason: ${err}`);
log('Falling back to 8080.');
listen(8080);
});
process.on('uncaughtException', (reason) => {
log(`An error occured.\n${reason.stack}`);
log(
'Please report this bug here: https://github.com/reisxd/revanced-builder/issues.'
);
});
process.on('unhandledRejection', (reason) => {
log(`An error occured.\n${reason.stack}`);
log(
'Please report this bug here: https://github.com/reisxd/revanced-builder/issues.'
);
for (const wsClient of wsClients) {
wsClient.send(
JSON.stringify({
event: 'error',
error: encodeURIComponent(
`An error occured:\n${reason.stack}\nPlease report this bug here: https://github.com/reisxd/revanced-builder/issues.`
)
})
);
}
});
process.on('SIGTERM', () => cleanExit(server));
process.on('SIGINT', () => cleanExit(server));
// The websocket server
wsServer.on('connection', (ws) => {
wsClients.push(ws);
ws.on('message', async (msg) => {
/** @type {Record<string, any>} */
const message = JSON.parse(msg);
// Theres no file handler, soo...
switch (message.event) {
case 'checkForUpdates':
await checkForUpdates(ws);
break;
case 'getAppList':
await getApp(ws);
break;
case 'updateFiles':
await updateFiles(ws);
break;
case 'getDevices':
await getDevices(ws);
break;
case 'setDevice':
setDevice(message);
break;
case 'selectApp':
selectApp(message);
break;
case 'getPatches':
await getPatches(ws);
break;
case 'selectPatches':
selectPatches(message);
break;
case 'checkFileAlreadyExists':
checkFileAlreadyExists(ws);
break;
case 'getAppVersion':
await getAppVersion(ws, message);
break;
case 'selectAppVersion':
await selectAppVersion(message, ws);
break;
case 'patchApp':
await patchApp(ws);
break;
case 'installReVanced':
await installReVanced(ws);
break;
case 'getSettings':
await getSettings(ws);
break;
case 'setSettings':
await setSettings(message);
break;
case 'exit':
process.kill(process.pid, 'SIGTERM');
}
});
});