83 lines
1.9 KiB
JavaScript
83 lines
1.9 KiB
JavaScript
#!/usr/bin/env node
|
|
'use strict';
|
|
|
|
var PromiseA = require('bluebird').Promise;
|
|
var https = require('https');
|
|
var fs = require('fs');
|
|
var path = require('path');
|
|
|
|
module.exports.update = function (opts) {
|
|
return new PromiseA(function (resolve, reject) {
|
|
var options;
|
|
var hostname = opts.updater || 'redirect-www.org';
|
|
var port = opts.port || 65443;
|
|
var req;
|
|
|
|
options = {
|
|
host: hostname
|
|
, port: port
|
|
, method: 'POST'
|
|
, headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
, path: '/api/ddns'
|
|
//, auth: opts.auth || 'admin:secret'
|
|
};
|
|
|
|
if (opts.cacert) {
|
|
if (!Array.isArray(opts.cacert)) {
|
|
opts.cacert = [opts.cacert];
|
|
}
|
|
options.ca = opts.cacert;
|
|
} else {
|
|
options.ca = [path.join(__dirname, '..', 'certs', 'ca', 'my-root-ca.crt.pem')]
|
|
}
|
|
|
|
options.ca = options.ca.map(function (str) {
|
|
if ('string' === typeof str && str.length < 1000) {
|
|
str = fs.readFileSync(str);
|
|
}
|
|
return str;
|
|
});
|
|
|
|
if (opts.token || opts.jwt) {
|
|
options.headers['Authorization'] = 'Bearer ' + (opts.token || opts.jwt);
|
|
}
|
|
|
|
if (false === opts.cacert) {
|
|
options.rejectUnauthorized = false;
|
|
}
|
|
|
|
options.agent = new https.Agent(options);
|
|
|
|
req = https.request(options, function(res) {
|
|
var textData = '';
|
|
|
|
res.on('error', function (err) {
|
|
reject(err);
|
|
});
|
|
res.on('data', function (chunk) {
|
|
textData += chunk.toString();
|
|
// console.log(chunk.toString());
|
|
});
|
|
res.on('end', function () {
|
|
var err;
|
|
try {
|
|
resolve(JSON.parse(textData));
|
|
} catch(e) {
|
|
err = new Error("Unparsable Server Response");
|
|
err.code = 'E_INVALID_SERVER_RESPONSE';
|
|
err.data = textData;
|
|
reject(err);
|
|
}
|
|
});
|
|
});
|
|
|
|
req.on('error', function (err) {
|
|
reject(err);
|
|
});
|
|
|
|
req.end(JSON.stringify(opts.ddns, null, ' '));
|
|
});
|
|
};
|