Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
What if your clock was off? Get the time from other servers date headers
var http = require('http'),
fs = require('fs'),
loadDomains, getTheTimeFromServerList, formatDateHeader;
loadDomains = new Promise(function (resolve, reject) {
var servers;
fs.readFile('hostnames.txt', 'utf8', function (err, data) {
if (err) {
reject('Unable to read hostnames.txt');
}
servers = data
.trim()
.split('\n')
.map(function (r) {
return r.trim();
});
resolve(servers);
});
});
formatDateHeader = function (date) {
return new Date(date).toISOString();
};
getTheTimeFromServerList = function (servers) {
return new Promise(function (resolve) {
var i;
for (i = 0; i < servers.length; i += 1) {
(function (server) {
http.get('http://' + server + '/', function (resp) {
if (resp.headers.date) {
return resolve(formatDateHeader(resp.headers.date));
}
}).on('error', function () {
return;
});
})(servers[i]);
}
});
};
loadDomains.then(getTheTimeFromServerList).then(console.log).catch(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.