Skip to content
This repository has been archived by the owner on Feb 25, 2019. It is now read-only.

Use current computer's FQDN as default issuer #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down