From 66b28732c895f2c54e0447ceebb57cfb9194a7e9 Mon Sep 17 00:00:00 2001 From: Vartan Simonian Date: Sat, 22 Aug 2015 14:19:11 -0700 Subject: [PATCH 1/2] feat(init): Use current computer's FQDN as default issuer --- lib/commands/init.js | 55 +++++++++++++++++++++++++++++++++++++++++++- package.json | 1 + 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/commands/init.js b/lib/commands/init.js index 252a12a..22b40c7 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -3,6 +3,7 @@ */ var path = require('path') +var dns = require('native-dns') var mkdirp = require('mkdirp') var crypto = require('crypto') var _s = require('underscore.string') @@ -121,6 +122,58 @@ function registerInit (cli, options, done) { }.bind(this)) } + /** + * Get default hostname (current computer's FQDN) + */ + + function execDNSQuery (server, question, callback) { + var req = dns.Request({ + server: server, + question: dns.Question(question) + }) + + req.on('timeout', function () { + callback(new Error('Request timed out.')) + }) + + req.on('message', function (err, res) { + if (err) { return callback(err) } + + callback(null, res.answer) + }) + + req.send() + } + + Generator.prototype.getDefaultHostname = function () { + var callback = this.async() + + this.issuer = 'connect.example.com' // Default in case request fails + + execDNSQuery({ + address: '208.67.222.222', // OpenDNS + port: 53, + type: 'udp' + }, { + name: 'myip.opendns.com', + type: 'A' + }, function (err, ans) { + if (err) { callback() } + + var ip = ans[0] && ans[0].address + + if (!ip) { callback() } + + dns.reverse(ip, function (err, results) { + if (err) { callback() } + if (!results[0]) { callback() } + + this.issuer = results[0] + callback() + }.bind(this)) + }.bind(this)) + } + /** * Ask for instance name */ @@ -152,7 +205,7 @@ function registerInit (cli, options, done) { type: 'input', name: 'issuer', message: 'What (sub)domain will you use?', - default: 'connect.example.com' + default: this.issuer }], function (answers) { this.issuer = answers.issuer callback() diff --git a/package.json b/package.json index 3864141..2b4af03 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "inquirer": "^0.9.0", "mkdirp": "^0.5.0", "nash": "^2.0.0", + "native-dns": "^0.7.0", "request": "^2.61.0", "underscore.string": "^3.1.1", "update-notifier": "^0.5.0", From fceb1fffddc97eebd365ab23902fef5affff7489 Mon Sep 17 00:00:00 2001 From: Vartan Simonian Date: Sat, 22 Aug 2015 14:32:21 -0700 Subject: [PATCH 2/2] fix(init): Don't execute further if DNS request fails --- lib/commands/init.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/commands/init.js b/lib/commands/init.js index 22b40c7..74b483a 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -158,15 +158,15 @@ function registerInit (cli, options, done) { name: 'myip.opendns.com', type: 'A' }, function (err, ans) { - if (err) { callback() } + if (err) { return callback() } var ip = ans[0] && ans[0].address - if (!ip) { callback() } + if (!ip) { return callback() } dns.reverse(ip, function (err, results) { - if (err) { callback() } - if (!results[0]) { callback() } + if (err) { return callback() } + if (!results[0]) { return callback() } this.issuer = results[0] callback()