Browse Source

Support domain name verification

master
Olli Vanhoja 8 years ago
parent
commit
1fee62c596
  1. 33
      bin/now-domains.js
  2. 16
      lib/alias.js
  3. 6
      lib/domains.js
  4. 23
      lib/index.js

33
bin/now-domains.js

@ -16,11 +16,12 @@ import NowDomains from '../lib/domains'
const argv = minimist(process.argv.slice(2), { const argv = minimist(process.argv.slice(2), {
string: ['config', 'token'], string: ['config', 'token'],
boolean: ['help', 'debug', 'force'], boolean: ['help', 'debug', 'external', 'force'],
alias: { alias: {
help: 'h', help: 'h',
config: 'c', config: 'c',
debug: 'd', debug: 'd',
external: 'e',
force: 'f', force: 'f',
token: 't' token: 't'
} }
@ -37,6 +38,7 @@ const help = () => {
-h, --help Output usage information -h, --help Output usage information
-c ${chalk.bold.underline('FILE')}, --config=${chalk.bold.underline('FILE')} Config file -c ${chalk.bold.underline('FILE')}, --config=${chalk.bold.underline('FILE')} Config file
-d, --debug Debug mode [off] -d, --debug Debug mode [off]
-e, --external Use external DNS server
-f, --force Skip DNS verification -f, --force Skip DNS verification
-t ${chalk.bold.underline('TOKEN')}, --token=${chalk.bold.underline('TOKEN')} Login token -t ${chalk.bold.underline('TOKEN')}, --token=${chalk.bold.underline('TOKEN')} Login token
@ -77,6 +79,18 @@ const help = () => {
${chalk.cyan('$ now domain rm domainId')} ${chalk.cyan('$ now domain rm domainId')}
To get the list of domain ids, use ${chalk.dim('`now domains ls`')}. To get the list of domain ids, use ${chalk.dim('`now domains ls`')}.
${chalk.gray('–')} Adding and verifying a domain name using zeit.world nameservers:
${chalk.cyan('$ now domain add my-app.com')}
The command will tell you if the domain was verified succesfully. In case the domain was not verified succesfully you should retry adding the domain after some time.
${chalk.gray('–')} Adding and verifying a domain name using an external nameserver:
${chalk.cyan('$ now domain add -e my-app.com')}
and follow the verification instructions if requested. Finally, rerun the same command after completing the verification step.
`) `)
} }
@ -137,7 +151,7 @@ async function run(token) {
const domains = await domain.ls() const domains = await domain.ls()
domains.sort((a, b) => new Date(b.created) - new Date(a.created)) domains.sort((a, b) => new Date(b.created) - new Date(a.created))
const current = new Date() const current = new Date()
const header = [['', 'id', 'dns', 'url', 'created'].map(s => chalk.dim(s))] const header = [['', 'id', 'dns', 'url', 'verified', 'created'].map(s => chalk.dim(s))]
const out = domains.length === 0 ? null : table(header.concat(domains.map(domain => { const out = domains.length === 0 ? null : table(header.concat(domains.map(domain => {
const ns = domain.isExternal ? 'external' : 'zeit.world' const ns = domain.isExternal ? 'external' : 'zeit.world'
const url = chalk.underline(`https://${domain.name}`) const url = chalk.underline(`https://${domain.name}`)
@ -147,9 +161,10 @@ async function run(token) {
domain.uid, domain.uid,
ns, ns,
url, url,
domain.verified,
time time
] ]
})), {align: ['l', 'r', 'l', 'l', 'l'], hsep: ' '.repeat(2), stringLength: strlen}) })), {align: ['l', 'r', 'l', 'l', 'l', 'l'], hsep: ' '.repeat(2), stringLength: strlen})
const elapsed_ = ms(new Date() - start_) const elapsed_ = ms(new Date() - start_)
console.log(`> ${domains.length} domain${domains.length === 1 ? '' : 's'} found ${chalk.gray(`[${elapsed_}]`)}`) console.log(`> ${domains.length} domain${domains.length === 1 ? '' : 's'} found ${chalk.gray(`[${elapsed_}]`)}`)
@ -209,12 +224,18 @@ async function run(token) {
const start = new Date() const start = new Date()
const name = String(args[0]) const name = String(args[0])
const {uid, created} = await domain.add(name, argv.force) const {uid, code, verified, verifyToken, created} = await domain.add(name, argv.force, argv.external)
const elapsed = ms(new Date() - start) const elapsed = ms(new Date() - start)
if (created) { if (created && verified) {
console.log(`${chalk.cyan('> Success!')} Domain ${chalk.bold(chalk.underline(name))} ${chalk.dim(`(${uid})`)} added [${elapsed}]`) console.log(`${chalk.cyan('> Success!')} Domain ${chalk.bold(chalk.underline(name))} ${chalk.dim(`(${uid})`)} added [${elapsed}]`)
} else { } else if (verified) {
console.log(`${chalk.cyan('> Success!')} Domain ${chalk.bold(chalk.underline(name))} ${chalk.dim(`(${uid})`)} verified [${elapsed}]`)
} else if (verifyToken) {
console.log(`> Verification required: Please add the following TXT record on the external DNS server: _now.${name}: ${verifyToken}`)
} else if (code === 'not_modified') {
console.log(`${chalk.cyan('> Success!')} Domain ${chalk.bold(chalk.underline(name))} ${chalk.dim(`(${uid})`)} already exists [${elapsed}]`) console.log(`${chalk.cyan('> Success!')} Domain ${chalk.bold(chalk.underline(name))} ${chalk.dim(`(${uid})`)} already exists [${elapsed}]`)
} else {
console.log('> Verification required: Please rerun this command after some time')
} }
break break

16
lib/alias.js

@ -119,13 +119,27 @@ export default class Alias extends Now {
console.log(`> Verifying the DNS settings for ${chalk.bold(chalk.underline(alias))} (see ${chalk.underline('https://zeit.world')} for help)`) console.log(`> Verifying the DNS settings for ${chalk.bold(chalk.underline(alias))} (see ${chalk.underline('https://zeit.world')} for help)`)
const {domain, nameservers} = await this.getNameservers(alias) const {domain, nameservers} = await this.getNameservers(alias)
let skipDNSVerification = false
if (this._debug) { if (this._debug) {
console.log(`> [debug] Found domain ${domain} and nameservers ${nameservers}`) console.log(`> [debug] Found domain ${domain} and nameservers ${nameservers}`)
} }
if (!isZeitWorld(nameservers)) {
const domainInfo = await this.getDomain(domain)
if (domainInfo.verified) {
skipDNSVerification = true
} else if (domainInfo.uid) {
const e = new Error(`> The domain ${domain} is already registered with now but additional verification is needed, please refer to 'now domain --help'.`)
e.userError = true
throw e
}
}
try { try {
if (!skipDNSVerification) {
await this.verifyOwnership(alias) await this.verifyOwnership(alias)
}
} catch (err) { } catch (err) {
if (err.userError) { if (err.userError) {
// a user error would imply that verification failed // a user error would imply that verification failed
@ -178,7 +192,7 @@ export default class Alias extends Now {
} }
} }
if (!isZeitWorld(nameservers)) { if (!isZeitWorld(nameservers) && !skipDNSVerification) {
if (this._debug) { if (this._debug) {
console.log(`> [debug] Trying to register a non-ZeitWorld domain ${domain} for the current user`) console.log(`> [debug] Trying to register a non-ZeitWorld domain ${domain} for the current user`)
} }

6
lib/domains.js

@ -37,15 +37,15 @@ export default class Domains extends Now {
}) })
} }
async add(domain, skipVerification) { async add(domain, skipVerification, isExternal) {
if (!domainRegex.test(domain)) { if (!domainRegex.test(domain)) {
const err = new Error(`The supplied value ${chalk.bold(`"${domain}"`)} is not a valid domain.`) const err = new Error(`The supplied value ${chalk.bold(`"${domain}"`)} is not a valid domain.`)
err.userError = true err.userError = true
throw err throw err
} }
if (skipVerification) { if (skipVerification || isExternal) {
return this.setupDomain(domain) return this.setupDomain(domain, {isExternal})
} }
let ns let ns

23
lib/index.js

@ -387,6 +387,22 @@ export default class Now extends EventEmitter {
}) })
} }
async getDomain(domain) {
return this.retry(async (bail, attempt) => {
if (this._debug) {
console.time(`> [debug] #${attempt} GET /domains/${domain}`)
}
const res = await this._fetch(`/domains/${domain}`)
if (this._debug) {
console.timeEnd(`> [debug] #${attempt} GET /domains/${domain}`)
}
return await res.json()
})
}
getNameservers(domain) { getNameservers(domain) {
return new Promise(resolve => { return new Promise(resolve => {
let fallback = false let fallback = false
@ -449,8 +465,9 @@ export default class Now extends EventEmitter {
console.timeEnd(`> [debug] #${attempt} POST /domains`) console.timeEnd(`> [debug] #${attempt} POST /domains`)
} }
if (res.status === 403) {
const body = await res.json() const body = await res.json()
if (res.status === 403) {
const code = body.error.code const code = body.error.code
let err let err
@ -464,15 +481,13 @@ export default class Now extends EventEmitter {
return bail(err) return bail(err)
} }
const body = await res.json()
// domain already exists // domain already exists
if (res.status === 409) { if (res.status === 409) {
if (this._debug) { if (this._debug) {
console.log('> [debug] Domain already exists (noop)') console.log('> [debug] Domain already exists (noop)')
} }
return {uid: body.error.uid} return {uid: body.error.uid, code: body.error.code}
} }
if (res.status !== 200) { if (res.status !== 200) {

Loading…
Cancel
Save