From b6c6b81e735fd851777afbe0ac72a77dadb73c51 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 14 Jun 2018 05:24:28 -0600 Subject: [PATCH] preliminary updater hook --- bin/telebitd.js | 2 + lib/updater.js | 123 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 lib/updater.js diff --git a/bin/telebitd.js b/bin/telebitd.js index 661f346..8854111 100755 --- a/bin/telebitd.js +++ b/bin/telebitd.js @@ -28,6 +28,8 @@ if (-1 !== confIndex) { confpath = confargs[1]; } +var updater = require('../lib/updater')(pkg); + function help() { console.info(''); console.info('Telebit Daemon v' + pkg.version); diff --git a/lib/updater.js b/lib/updater.js new file mode 100644 index 0000000..a5d9fb5 --- /dev/null +++ b/lib/updater.js @@ -0,0 +1,123 @@ +'use strict'; + +module.exports = function (pkg) { + function checkUpgrade() { + var https = require('https'); + + function getFile(url, cb) { + https.get(url, function (resp) { + var str = ''; + resp.on('data', function (chunk) { + str += chunk.toString('utf8'); + }); + resp.on('end', function () { + cb(null, str); + }); + resp.on('error', function (err) { + // ignore + cb(err); + }); + }).on('error', function (err) { + // ignore + cb(err); + }); + } + + function isNewer(latest, myPkg) { + var m = /^(v)?(\d+)\.(\d+)\.(\d+)(.*)/.exec(latest.version); + var n = /^(v)?(\d+)\.(\d+)\.(\d+)(.*)/.exec(myPkg.version); + if (!m) { + return 1; + } + if (parseInt(m[2], 10) > parseInt(n[2], 10)) { + return -1; + } else if (parseInt(m[2], 10) === parseInt(n[2], 10)) { + if (parseInt(m[3], 10) > parseInt(n[3], 10)) { + return -1; + } else if (parseInt(m[3], 10) === parseInt(n[3], 10)) { + if (parseInt(m[4], 10) > parseInt(n[4], 10)) { + return -1; + } else if (parseInt(m[4], 10) === parseInt(n[4], 10)) { + // lex sorting + if (m[5] > n[5]) { + return -1; + } else if (m[5] === n[5]) { + return 0; + } else { + return 1; + } + } else { + return 1; + } + } else { + return 1; + } + } else { + return 1; + } + } + + getFile("https://telebit.cloud/dist/index.tab", function (err, tab) { + if (err) { /*ignore*/ return; } + if (tab) { tab = tab && tab.toString() || ''; } + var versions = []; + var lines = tab.split(/[\r\n]/g); + var headers = lines.shift().split(/\t/g); + var chan = 'prod'; + var next; + lines.forEach(function (line) { + var tsv = {}; + var fields = line.split(/\t/g); + fields.forEach(function (value, i) { + tsv[headers[i]] = value; + }); + versions.push(tsv); + }); + // find matching version + versions.some(function (v) { + if (('v' + pkg.version) === v.version) { + chan = v.channel; + return true; + } + }); + // find first (most recent) version in channel + versions.some(function (v) { + if (chan === v.channel) { + next = v; + return true; + } + }); + if (!next || !isNewer(next.version, pkg.version)) { + console.log('DEBUG can\'t upgrade from', pkg.version, 'in channel', chan); + return; + } + console.log('Upgrade Available: ' + next.version + ' in \'' + next.channel + '\'channel'); + getFile("https://telebit.cloud/dist/upgrade.js", function (err, script) { + if (err) { /*ignore*/ return; } + var os = require('os'); + var fs = require('fs'); + var path = require('path'); + var scriptname = 'telebit-upgrade-' + Math.round(Math.random() * 99999) + '.js'; + var pathname = path.join(os.tmpdir(), scriptname); + fs.writeFile(pathname, script, function (err) { + if (err) { /*ignore*/ return; } + // console.log('DEBUG wrote', pathname); + //var str = + require(pathname)({ + package: pkg + , root: path.resolve(__dirname, '..') + , latest: next + , channel: chan + }, function () { + // console.log('upgrade complete'); + }); + //console.log(str); + }); + }); + }); + } + setInterval(checkUpgrade, 2 * 60 * 60 * 1000); + process.nextTick(function () { + checkUpgrade(); + }); +};