2015-02-12 09:40:37 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
'use strict';
|
|
|
|
|
2015-08-14 17:00:28 +00:00
|
|
|
var PromiseA = require('bluebird').Promise;
|
|
|
|
var https = require('https');
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
2015-02-12 09:40:37 +00:00
|
|
|
|
|
|
|
module.exports.update = function (opts) {
|
|
|
|
return new PromiseA(function (resolve, reject) {
|
2015-08-14 17:00:28 +00:00
|
|
|
var options;
|
|
|
|
var hostname = opts.updater || 'redirect-www.org';
|
|
|
|
var port = opts.port || 65443;
|
2015-02-12 09:40:37 +00:00
|
|
|
|
|
|
|
options = {
|
|
|
|
host: hostname
|
|
|
|
, port: port
|
|
|
|
, method: 'POST'
|
|
|
|
, headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
|
|
|
, path: '/api/ddns'
|
|
|
|
, auth: opts.auth || 'admin:secret'
|
|
|
|
, ca: [ fs.readFileSync(path.join(__dirname, '..', 'certs', 'ca', 'my-root-ca.crt.pem')) ]
|
|
|
|
};
|
2015-08-14 17:00:28 +00:00
|
|
|
|
|
|
|
if (opts.jwt) {
|
|
|
|
options.headers['Authorization'] = 'Bearer ' + opts.jwt;
|
|
|
|
}
|
|
|
|
|
2015-02-12 09:40:37 +00:00
|
|
|
options.agent = new https.Agent(options);
|
|
|
|
|
|
|
|
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 () {
|
|
|
|
resolve(textData);
|
|
|
|
});
|
|
|
|
}).end(JSON.stringify(opts.ddns, null, ' '));
|
|
|
|
});
|
|
|
|
};
|