-
Notifications
You must be signed in to change notification settings - Fork 4
/
check.js
96 lines (93 loc) · 2.95 KB
/
check.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
'use strict';
var db = require('./database'),
config = require('./config.json'),
cache = require('./cache'),
async = require('async'),
request = require('request'),
sanitize = require('sanitize-html');
var checkers = config.checks.map(function (url) {
return createCheck(url);
}),
delay = config.pollDelay * 1000,
timeout = Math.max(12 + 3, config.pollDelay * config.checks.length) * 1000;
function createCheck(url) {
return function sitehome(next) {
var now = Date.now();
request({
rejectUnauthorized: false,
url: url,
timeout: timeout,
headers: {
'User-Agent': 'servercooties.io',
'accept': 'text/html'
}
}, function (err, resp) {
var complete = Date.now() - now;
if (err) {
return db.addCheck(url, 599, 0, complete, function () {
next();
});
}
var readonly = !!resp.headers['Discourse-Readonly'];
db.addCheck(url, resp.statusCode, readonly,
complete,
function () {
next();
});
});
};
}
exports.updated = false;
function getNotice(callback) {
return callback(); // disable notice for now as it's not on nodebb
/*request({
rejectUnauthorized: false,
url: config.siteSettings,
timeout: 3 * 1000,
headers: {
'User-Agent': 'servercooties.io',
'accept': 'text/html'
}
}, function (err, _, body) {
if (err) {
return callback();
}
try {
var settings = JSON.parse(body);
if (settings.global_notice) {
cache.global_notice = sanitize(settings.global_notice, {
allowedTags: ['a', 'abbr', 'b', 'i', 'em', 'strong'],
allowedAttributes: {
a: ['href', 'target'],
abbr: ['title']
},
transformTags: {
'a': sanitize.simpleTransform('a', {
target: '_blank'
})
}
});
cache.global_notice_text = sanitize(settings.global_notice, {
allowedTags: []
});
} else {
cache.global_notice = '';
cache.global_notice_text = '';
}
} catch (ignore) {} //eslint-disable-line no-empty
callback();
});*/
}
exports.start = function () {
async.forever(function (next) {
async.eachSeries(checkers, function (check, callback) {
exports.updated = true;
check(function () {
setTimeout(callback, delay);
});
},
function () {
getNotice(next);
});
});
};