forked from Automattic/auto-update-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
426 lines (384 loc) · 11.4 KB
/
app.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
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
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/env node
/**
* Module dependencies
*/
require('gnode'); // Run node with ES6 Generators, today!
var co = require('co');
var fs = require('fs');
var ms = require('ms');
var sleep = require('co-wait');
var http = require('http');
var dive_ = require('dive');
var path = require('path');
var https = require('https');
var semver = require('semver');
var express = require('express');
var router = require('express').Router()
var bodyParser = require('body-parser');
var basicAuth = require('express-basic-auth');
var serveIndex = require('serve-index');
const fileUpload = require('express-fileupload');
var thunkify = require('thunkify');
var exec = require('child_process').exec;
/**
* Config
*/
var config = {
username: process.env.USERNAME,
password: process.env.PASSWORD,
directory: path.join(__dirname, 'updates')
}
var app = express();
var updates = [];
/**
* Completes a version with missing info. e.g. '1.3' => '1.3.0'
*/
function completeVer(ver) {
if (ver.match(/^[0-9]+$/)) {
return ver + '.0.0';
} else if (ver.match(/^[0-9]+\.[0-9]+$/)) {
return ver + '.0';
} else {
return ver;
}
}
/**
* Removes the Byte Order Mark
*/
function removeBOM(str) {
if (str.charCodeAt(0) === 0xFEFF) {
return str.slice(1);
}
return str;
}
/**
* Thunkified dive() module.
*/
function dive (dir, iterator) {
return function(fn){
dive_(dir, {}, function(err, filepath){
if (err) return fn(err);
iterator(filepath);
}, fn);
};
}
/**
* Reads and loads update data from the `updates` directory
*/
function* loadUpdates() {
console.log('Loading updates...');
var newUpdates = [];
// add a new "file" entry to the `newUpdates` array
function add(data, filepath, stats){
newUpdates.push({
app: data.app,
version: completeVer(data.version),
channels: data.channels,
compatible: {
os: data.os,
osversion: data.osversion,
architectures: data.architectures,
appversion: data.appversion
},
percentage: parseFloat(data.percentage) || 100,
path: path.resolve(path.dirname(filepath), data.path),
format: data.format || path.extname(data.path).slice(1),
jsonpath: filepath,
date: stats.ctime
});
}
// iterate through each file in the "updates" dir
yield dive(config.directory, function(filepath) {
if (filepath.match(/\.json$/)) {
const splited = filepath.split('/');
console.log('Reading `' + splited[splited.length - 1] + '`...');
var data;
var stats;
try {
// TODO: turn the readFileSync() into a yield call...
data = JSON.parse(removeBOM(fs.readFileSync(filepath, 'utf8')));
stats = fs.statSync(filepath);
} catch (e) {
console.error(e.stack);
return;
}
if (data.entries) {
data.entries.forEach(function(entry) {
entry.app = data.app;
entry.version = data.version;
entry.channels = data.channels;
add(entry, filepath, stats);
});
} else {
add(data, filepath, stats);
}
}
});
console.log(newUpdates.length + ' update' + (newUpdates.length == 1 ? '' : 's') + ' loaded successfully.');
updates = newUpdates;
}
/**
* Cleans up updates in disposable channels after ~24 hours
*/
function* cleanup() {
if (!config.disposableChannels || config.disposableChannels.length == 0) {
return;
}
console.log('Cleaning up updates in channels: %j', config.disposableChannels);
var cleaned = 0;
function isDisposable(channel) {
return config.disposableChannels.indexOf(channel) != -1;
}
updates.forEach(function(update) {
if (update.date < new Date(new Date() - 1000 * 60 * 60 * 24)) {
if (update.channels.every(isDisposable)) {
try {
cleaned++;
console.log('Removing file `' + update.path + '`.');
fs.unlinkSync(update.path);
console.log('Removing file `' + update.jsonpath + '`.');
fs.unlinkSync(update.jsonpath);
} catch (e) {
console.error(e.stack);
}
}
}
});
if (cleaned > 0) {
console.log('cleaned %d entries - reloading updates', cleaned);
yield loadUpdates();
}
console.log('Cleanup completed. ' + cleaned + ' updates cleaned.');
}
/**
* Match the latest update for the given info
*/
function matchUpdate(info) {
var match;
// TODO: use component/find here...
updates.forEach(function(update) {
// console.log();
// console.log("update.app == info.app", update.app == info.app);
// console.log("semver.gt(update.version, completeVer(info.appversion))", semver.gt(update.version, completeVer(info.appversion)));
// console.log("update.channels.indexOf(info.channel) != -1", update.channels.indexOf(info.channel) != -1);
// console.log("update.compatible.architectures.indexOf(info.architecture) != -1", update.compatible.architectures.indexOf(info.architecture) != -1);
// console.log("update.compatible.os == info.os", update.compatible.os == info.os);
// console.log("semver.satisfies(completeVer(info.osversion), update.compatible.osversion)", semver.satisfies(completeVer(info.osversion), update.compatible.osversion));
// console.log("semver.satisfies(completeVer(info.appversion), update.compatible.appversion)", semver.satisfies(completeVer(info.appversion), update.compatible.appversion));
// console.log("update.percentage >= parseFloat(info.percentile)", update.percentage >= parseFloat(info.percentile));
// console.log("update.format == info.format", update.format == info.format);
//
// console.log("match",
// update.app == info.app
// && semver.gt(update.version, completeVer(info.appversion))
// && update.channels.indexOf(info.channel) != -1
// && update.compatible.architectures.indexOf(info.architecture) != -1
// && update.compatible.os == info.os
// && semver.satisfies(completeVer(info.osversion), update.compatible.osversion)
// && semver.satisfies(completeVer(info.appversion), update.compatible.appversion)
// && update.percentage >= parseFloat(info.percentile)
// && update.format == info.format
// );
if (
update.app == info.app
&& semver.gt(update.version, completeVer(info.appversion))
&& update.channels.indexOf(info.channel) != -1
&& update.compatible.architectures.indexOf(info.architecture) != -1
&& update.compatible.os == info.os
&& semver.satisfies(completeVer(info.osversion), update.compatible.osversion)
&& semver.satisfies(completeVer(info.appversion), update.compatible.appversion)
&& update.percentage >= parseFloat(info.percentile)
&& update.format == info.format
) {
if (match) {
if (semver.gt(update.version, match.version)) {
match = update;
}
} else {
match = update;
}
}
});
return match;
}
/**
* Middleware
*/
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.use(fileUpload({
// useTempFiles : true,
// tempFileDir : '/tmp/'
}));
// Basic Auth
var users = {};
users[`${config.username}`] = config.password;
function getUnauthorizedResponse(req) {
// return req.auth
// ? ('Credentials ' + req.auth.user + ':' + req.auth.password + ' rejected')
// : 'No credentials provided'
return 'auth failed'
}
const auth = basicAuth({
users,
challenge: true,
unauthorizedResponse: getUnauthorizedResponse
});
/**
* Normalizes a `req.params` or `req.query` object with the proper default values.
*
* @api private
*/
function defaults(info) {
if (!info) info = {};
if (!info.percentile) info.percentile = 100;
if (!info.channel) info.channel = 'release';
if (!info.appversion) info.appversion = '0.0.0';
if (!info.osversion) {
if (info.os == 'windows') {
info.osversion = '5.1';
} else if (info.os == 'osx') {
info.osversion = '10.6';
} else if (info.os == 'linux') {
info.osversion = '8.0';
}
}
if (!info.architecture) {
if (info.os == 'windows') {
info.architecture = 'x86';
} else if (info.os == 'osx') {
info.architecture = 'x86-64';
} else if (info.os == 'linux') {
info.architecture = 'armv7';
}
}
if (!info.format) {
if (info.os == 'windows') {
info.format = 'zip';
} else if (info.os == 'osx') {
info.format = 'gz';
} else if (info.os == 'linux') {
info.format = 'bz2';
}
}
return info;
}
/**
* Check and download update.
*
* @api public
*/
app.get('/update', function(req, res, next) {
var info = defaults(req.query);
var update = matchUpdate(info);
res.setHeader("Connection", "close");
if (update) {
res.download(update.path, path.basename(update.path));
} else {
res.status(404).send("No updates");
}
});
/**
* Get updates
*
* @api public
*/
app.get('/updates', function(req, res, next) {
res.setHeader("Connection", "close");
res.send(updates);
});
/**
* Returns a JSON document describing the "latest" version.
*/
app.get('/update.json', function(req, res, next) {
var info = defaults(req.query);
var update = matchUpdate(info);
res.setHeader("Connection", "close");
if (!update) {
update = {
error: 'No updates'
};
res.status(404);
}
res.send(update);
});
/**
* Upload new updates
*/
app.post('/upload', auth, function(req, res, next) {
if (Object.keys(req.files).length == 0) {
return res.status(400).send('No files were uploaded.');
}
let update = req.files.update;
update.mv(`/dev/shm/${req.files.update.name}`, function(err) {
if (err)
return res.status(500).send(err);
console.log('New update received. Extracting contents...');
exec('tar -xf ' + `/dev/shm/${req.files.update.name}` + ' -C "' + config.directory + '"', {}, function(err, stdout, stderr) {
if (err) {
console.log('Extraction failed.');
return next(err);
}
console.log('Extraction completed.');
co(loadUpdates)();
return res.status(201).send("Created")
});
});
});
/**
* Reload
*/
app.post('/reload', auth, function(req, res, next) {
res.send(202);
co(loadUpdates)();
});
/**
* Used for monitoring
*/
// app.get('/*', function(req, res) {
// res.sendFile(__dirname + '/www');
// // res.send(`
// // <h1>Auto Update Server ${process.env.npm_package_version || ""}</h1>
// // <h2><a href="/static">Browse</a></h2>
// // `);
// });
app.use(
'/',
express.static(__dirname + '/www')
);
/**
* Static route to get updates
*/
app.use(
'/static',
express.static(config.directory),
serveIndex(config.directory, {'icons': true})
);
/**
* Loads the .json update data in a never ending generator loop.
* This is kinda like setInterval() :D
*/
function* loadUpdatesLoop() {
while (true) {
yield loadUpdates();
yield cleanup();
yield sleep(ms('1 day'));
}
}
/**
* Initialize
*/
co(function*(){
// set process' title
process.title = 'auto-update-server';
// load the .json and apps in the "updates" directory
co(loadUpdatesLoop)();
// create HTTP server instance
var server = http.createServer(app);
// bind HTTP server to port
var listen = thunkify(server.listen.bind(server));
yield listen(parseInt(process.env.PORT, 10) || 3000);
console.log('auto-update-server HTTP server listening on port %d', server.address().port);
})();