diff --git a/lib/commands/init.js b/lib/commands/init.js index 252a12a..74b483a 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) { return callback() } + + var ip = ans[0] && ans[0].address + + if (!ip) { return callback() } + + dns.reverse(ip, function (err, results) { + if (err) { return callback() } + if (!results[0]) { return 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",