-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
alias.js
746 lines (633 loc) · 22.5 KB
/
alias.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// Packages
const { readFileSync } = require('fs')
const publicSuffixList = require('psl')
const minimist = require('minimist')
const ms = require('ms')
const chalk = require('chalk')
const { write: copy } = require('clipboardy')
// Ours
const promptBool = require('../lib/utils/input/prompt-bool')
const info = require('../lib/utils/output/info')
const param = require('../lib/utils/output/param')
const wait = require('../lib/utils/output/wait')
const success = require('../lib/utils/output/success')
const uid = require('../lib/utils/output/uid')
const eraseLines = require('../lib/utils/output/erase-lines')
const stamp = require('../lib/utils/output/stamp')
const error = require('../lib/utils/output/error')
const treatBuyError = require('../lib/utils/domains/treat-buy-error')
const scaleInfo = require('./scale-info')
const { DOMAIN_VERIFICATION_ERROR } = require('./errors')
const isZeitWorld = require('./is-zeit-world')
const resolve4 = require('./dns')
const toHost = require('./to-host')
const exit = require('./utils/exit')
const Now = require('./')
const argv = minimist(process.argv.slice(2), {
boolean: ['no-clipboard'],
alias: { 'no-clipboard': 'C' }
})
const isTTY = process.stdout.isTTY
const clipboard = !argv['no-clipboard']
const domainRegex = /^((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}$/
module.exports = class Alias extends Now {
async ls(deployment) {
if (deployment) {
const target = await this.findDeployment(deployment)
if (!target) {
const err = new Error(
`Aliases not found by "${deployment}". Run ${chalk.dim('`now alias ls`')} to see your aliases.`
)
err.userError = true
throw err
}
return this.listAliases(target.uid)
}
return this.listAliases()
}
async rm(_alias) {
return this.retry(async bail => {
const res = await this._fetch(`/now/aliases/${_alias.uid}`, {
method: 'DELETE'
})
if (res.status === 403) {
return bail(new Error('Unauthorized'))
}
if (res.status !== 200) {
const err = new Error('Deletion failed. Try again later.')
throw err
}
})
}
async findDeployment(deployment) {
const list = await this.list()
let key
let val
if (/\./.test(deployment)) {
val = toHost(deployment)
key = 'url'
} else {
val = deployment
key = 'uid'
}
const depl = list.find(d => {
if (d[key] === val) {
if (this._debug) {
console.log(`> [debug] matched deployment ${d.uid} by ${key} ${val}`)
}
return true
}
// Match prefix
if (`${val}.now.sh` === d.url) {
if (this._debug) {
console.log(`> [debug] matched deployment ${d.uid} by url ${d.url}`)
}
return true
}
return false
})
return depl
}
async updatePathBasedroutes(alias, rules, domains) {
alias = await this.maybeSetUpDomain(alias, domains)
return this.upsertPathAlias(alias, rules)
}
async upsertPathAlias(alias, rules) {
return this.retry(async (bail, attempt) => {
if (this._debug) {
console.time(`> [debug] /now/aliases #${attempt}`)
}
const rulesData = this.readRulesFile(rules)
const ruleCount = rulesData.rules.length
const res = await this._fetch(`/now/aliases`, {
method: 'POST',
body: { alias, rules: rulesData.rules }
})
const body = await res.json()
body.ruleCount = ruleCount
if (this._debug) {
console.timeEnd(`> [debug] /now/aliases #${attempt}`)
}
// 409 conflict is returned if it already exists
if (res.status === 409) {
return { uid: body.error.uid }
}
if (res.status === 422) {
return body
}
// No retry on authorization problems
if (res.status === 403) {
const code = body.error.code
if (code === 'custom_domain_needs_upgrade') {
const err = new Error(
`Custom domains are only enabled for premium accounts. Please upgrade by running ${chalk.gray('`')}${chalk.cyan('now upgrade')}${chalk.gray('`')}.`
)
err.userError = true
return bail(err)
}
if (code === 'alias_in_use') {
const err = new Error(
`The alias you are trying to configure (${chalk.underline(chalk.bold(alias))}) is already in use by a different account.`
)
err.userError = true
return bail(err)
}
if (code === 'forbidden') {
const err = new Error(
'The domain you are trying to use as an alias is already in use by a different account.'
)
err.userError = true
return bail(err)
}
return bail(new Error('Authorization error'))
}
// All other errors
if (body.error) {
const code = body.error.code
if (code === 'cert_missing') {
console.log(`> Provisioning certificate for ${chalk.underline(chalk.bold(alias))}`)
try {
await this.createCert(alias)
} catch (err) {
// We bail to avoid retrying the whole process
// of aliasing which would involve too many
// retries on certificate provisioning
return bail(err)
}
// Try again, but now having provisioned the certificate
return this.upsertPathAlias(alias, rules)
}
if (code === 'cert_expired') {
console.log(`> Renewing certificate for ${chalk.underline(chalk.bold(alias))}`)
try {
await this.createCert(alias, { renew: true })
} catch (err) {
return bail(err)
}
}
return bail(new Error(body.error.message))
}
// The two expected succesful cods are 200 and 304
if (res.status !== 200 && res.status !== 304) {
throw new Error('Unhandled error')
}
return body
})
}
readRulesFile(rules) {
try {
const rulesJson = readFileSync(rules, 'utf8')
return JSON.parse(rulesJson)
} catch (err) {
console.error(`Reading rules file ${rules} failed: ${err}`)
}
}
async set(deployment, alias, domains, currentTeam, user) {
const depl = await this.findDeployment(deployment)
if (!depl) {
const err = new Error(
`Deployment not found by "${deployment}". Run ${chalk.dim('`now ls`')} to see your deployments.`
)
err.userError = true
throw err
}
const aliasDepl = (await this.listAliases()).find(e => e.alias === alias)
if (aliasDepl && aliasDepl.rules) {
if (isTTY) {
try {
const msg =
`> Path alias exists with ${aliasDepl.rules.length} rule${aliasDepl.rules.length > 1 ? 's' : ''}.\n` +
`> Are you sure you want to update ${alias} to be a normal alias?\n`
const confirmation = await promptBool(msg, {
trailing: '\n'
})
if (!confirmation) {
console.log('\n> Aborted')
return exit(1)
}
} catch (err) {
console.log(err)
}
} else {
console.log(`Overwriting path alias with ${aliasDepl.rules.length} rule${aliasDepl.rules.length > 1 ? 's' : ''} to be a normal alias.`)
}
}
let aliasedDeployment = null
let shouldScaleDown = false
if (aliasDepl) {
aliasedDeployment = await this.findDeployment(aliasDepl.deploymentId)
if (
aliasedDeployment &&
aliasedDeployment.scale.current >= depl.scale.current &&
(aliasedDeployment.scale.min > depl.scale.min ||
aliasedDeployment.scale.max > depl.scale.max)
) {
shouldScaleDown = true
console.log(`> Alias ${alias} points to ${chalk.bold(aliasedDeployment.url)} (${chalk.bold(aliasedDeployment.scale.current + ' instances')})`)
console.log(`> Scaling ${depl.url} to ${chalk.bold(aliasedDeployment.scale.current + ' instances')} atomically`)
if (depl.scale.max < 1) {
if (this._debug) {
console.log(
'Updating max scale to 1 so that deployment may be unfrozen.'
)
}
await this.setScale(depl.uid, {
min: depl.scale.min,
max: Math.max(aliasedDeployment.scale.max, 1)
})
}
if (depl.scale.current < 1) {
if (this._debug) {
console.log(`> Deployment ${depl.url} is frozen, unfreezing...`)
}
await this.unfreeze(depl)
if (this._debug) {
console.log(`> Deployment is now unfrozen, scaling it to match current instance count`)
}
}
// Scale it to current limit
if (this._debug) {
console.log(`> Scaling deployment to match current scale.`)
}
await this.setScale(depl.uid, {
min: aliasedDeployment.scale.current,
max: aliasedDeployment.scale.current
})
await scaleInfo(this, depl.url)
if (this._debug) {
console.log(`> Updating scaling rules for deployment.`)
}
await this.setScale(depl.uid, {
min: Math.max(aliasedDeployment.scale.min, depl.scale.min),
max: Math.max(aliasedDeployment.scale.max, depl.scale.max)
})
}
}
alias = await this.maybeSetUpDomain(alias, domains, currentTeam, user)
const aliasTime = Date.now()
const newAlias = await this.createAlias(depl, alias)
if (!newAlias) {
throw new Error(
`Unexpected error occurred while setting up alias: ${JSON.stringify(newAlias)}`
)
}
const { created, uid } = newAlias
if (created) {
const output = `${chalk.cyan('> Success!')} ${alias} now points to ${chalk.bold(depl.url)}! ${chalk.grey('[' + ms(Date.now() - aliasTime) + ']')}`
if (isTTY && clipboard) {
try {
await copy(depl.url)
} catch (err) {
} finally {
console.log(output)
}
} else {
console.log(output)
}
} else {
console.log(`${chalk.cyan('> Success!')} Alias already exists ${chalk.dim(`(${uid})`)}.`)
}
if (aliasedDeployment && shouldScaleDown) {
const scaleDown = Date.now()
await this.setScale(aliasedDeployment.uid, { min: 0, max: 1 })
console.log(`> Scaled ${chalk.gray(aliasedDeployment.url)} down to 1 instance ${chalk.gray('[' + ms(Date.now() - scaleDown) + ']')}`)
}
}
createAlias(depl, alias) {
return this.retry(async (bail, attempt) => {
if (this._debug) {
console.time(`> [debug] /now/deployments/${depl.uid}/aliases #${attempt}`)
}
const res = await this._fetch(`/now/deployments/${depl.uid}/aliases`, {
method: 'POST',
body: { alias }
})
const body = await res.json()
if (this._debug) {
console.timeEnd(`> [debug] /now/deployments/${depl.uid}/aliases #${attempt}`)
}
// 409 conflict is returned if it already exists
if (res.status === 409) {
return { uid: body.error.uid }
}
// No retry on authorization problems
if (res.status === 403) {
const code = body.error.code
if (code === 'custom_domain_needs_upgrade') {
const err = new Error(
`Custom domains are only enabled for premium accounts. Please upgrade by running ${chalk.gray('`')}${chalk.cyan('now upgrade')}${chalk.gray('`')}.`
)
err.userError = true
return bail(err)
}
if (code === 'alias_in_use') {
const err = new Error(
`The alias you are trying to configure (${chalk.underline(chalk.bold(alias))}) is already in use by a different account.`
)
err.userError = true
return bail(err)
}
if (code === 'forbidden') {
const err = new Error(
'The domain you are trying to use as an alias is already in use by a different account.'
)
err.userError = true
return bail(err)
}
return bail(new Error('Authorization error'))
}
// All other errors
if (body.error) {
const code = body.error.code
if (code === 'deployment_not_found') {
return bail(new Error('Deployment not found'))
}
if (code === 'cert_missing') {
console.log(`> Provisioning certificate for ${chalk.underline(chalk.bold(alias))}`)
try {
await this.createCert(alias)
} catch (err) {
// We bail to avoid retrying the whole process
// of aliasing which would involve too many
// retries on certificate provisioning
return bail(err)
}
// Try again, but now having provisioned the certificate
return this.createAlias(depl, alias)
}
if (code === 'cert_expired') {
console.log(`> Renewing certificate for ${chalk.underline(chalk.bold(alias))}`)
try {
await this.createCert(alias, { renew: true })
} catch (err) {
return bail(err)
}
}
return bail(new Error(body.error.message))
}
// The two expected succesful cods are 200 and 304
if (res.status !== 200 && res.status !== 304) {
throw new Error('Unhandled error')
}
return body
})
}
async setupRecord(domain, name) {
await this.setupDomain(domain)
if (this._debug) {
console.log(`> [debug] Setting up record "${name}" for "${domain}"`)
}
const type = name === '' ? 'ALIAS' : 'CNAME'
return this.retry(async (bail, attempt) => {
if (this._debug) {
console.time(`> [debug] /domains/${domain}/records #${attempt}`)
}
const res = await this._fetch(`/domains/${domain}/records`, {
method: 'POST',
body: { type, name: name === '' ? name : '*', value: 'alias.zeit.co' }
})
if (this._debug) {
console.timeEnd(`> [debug] /domains/${domain}/records #${attempt}`)
}
if (res.status === 403) {
return bail(new Error('Unauthorized'))
}
const body = await res.json()
if (res.status !== 200) {
throw new Error(body.error.message)
}
return body
})
}
async maybeSetUpDomain(alias, domains, currentTeam, user) {
const gracefulExit = () => {
this.close()
domains.close()
// eslint-disable-next-line unicorn/no-process-exit
process.exit()
}
// Make alias lowercase
alias = alias.toLowerCase()
// Trim leading and trailing dots
// for example: `google.com.` => `google.com`
alias = alias.replace(/^\.+/, '').replace(/\.+$/, '')
// Evaluate the alias
if (/\./.test(alias)) {
alias = toHost(alias)
} else {
if (this._debug) {
console.log(`> [debug] suffixing \`.now.sh\` to alias ${alias}`)
}
alias = `${alias}.now.sh`
}
if (!domainRegex.test(alias)) {
const err = new Error(`Invalid alias "${alias}"`)
err.userError = true
throw err
}
if (!/\.now\.sh$/.test(alias)) {
console.log(`> ${chalk.bold(chalk.underline(alias))} is a custom domain.`)
let stopSpinner = wait('Fetching domain info')
let elapsed = stamp()
const parsed = publicSuffixList.parse(alias)
const pricePromise = domains.price(parsed.domain)
const canBePurchased = await domains.status(parsed.domain)
const aliasParam = param(parsed.domain)
stopSpinner()
if (canBePurchased) {
const price = await pricePromise
info(`The domain ${aliasParam} is ${chalk.bold('available for purchase')}! ${elapsed()}`)
const confirmation = await promptBool(`Buy now for ${chalk.bold(`$${price}`)} (${chalk.bold((currentTeam && currentTeam.slug) || user.username || user.email)})?`)
eraseLines(1)
if (!confirmation) {
info('Aborted')
gracefulExit()
}
elapsed = stamp()
stopSpinner = wait('Purchasing')
let domain
try {
domain = await domains.buy(alias)
} catch (err) {
stopSpinner()
treatBuyError(err)
gracefulExit()
}
stopSpinner()
success(`Domain purchased and created ${uid(domain.uid)} ${elapsed()}`)
stopSpinner = wait('Verifying nameservers')
let domainInfo
try {
domainInfo = await this.setupDomain(alias)
} catch (err) {
if (this._debug) {
console.log('> [debug] Error while trying to setup the domain', err)
}
}
stopSpinner()
if (!domainInfo.verified) {
let { tld } = publicSuffixList.parse(alias)
tld = param(`.${tld}`)
error(
'The nameservers are pending propagation. Please try again shortly'
)
info(`The ${tld} servers might take some extra time to reflect changes`)
gracefulExit()
}
}
console.log(`> Verifying the DNS settings for ${chalk.bold(chalk.underline(alias))} (see ${chalk.underline('https://zeit.world')} for help)`)
const _domain = publicSuffixList.parse(alias).domain
const _domainInfo = await this.getDomain(_domain)
const domainInfo = _domainInfo && !_domainInfo.error
? _domainInfo
: undefined
const { domain, nameservers } = domainInfo
? { domain: _domain }
: await this.getNameservers(alias)
const usingZeitWorld = domainInfo
? !domainInfo.isExternal
: isZeitWorld(nameservers)
let skipDNSVerification = false
if (this._debug) {
if (domainInfo) {
console.log(`> [debug] Found domain ${domain} with verified:${domainInfo.verified}`)
} else {
console.log(`> [debug] Found domain ${domain} and nameservers ${nameservers}`)
}
}
if (!usingZeitWorld && domainInfo) {
if (domainInfo.verified) {
skipDNSVerification = true
} else if (domainInfo.uid) {
const { verified, created } = await this.setupDomain(domain, {
isExternal: true
})
if (!(created && verified)) {
const e = new Error(
`> Failed to verify the ownership of ${domain}, please refer to 'now domain --help'.`
)
e.userError = true
throw e
}
console.log(`${chalk.cyan('> Success!')} Domain ${chalk.bold(chalk.underline(domain))} verified`)
}
}
try {
if (!skipDNSVerification) {
await this.verifyOwnership(alias)
}
} catch (err) {
if (err.userError) {
// A user error would imply that verification failed
// in which case we attempt to correct the dns
// configuration (if we can!)
try {
if (usingZeitWorld) {
console.log(`> Detected ${chalk.bold(chalk.underline('zeit.world'))} nameservers! Configuring records.`)
const record = alias.substr(0, alias.length - domain.length)
// Lean up trailing and leading dots
const _record = record.replace(/^\./, '').replace(/\.$/, '')
const _domain = domain.replace(/^\./, '').replace(/\.$/, '')
if (_record === '') {
await this.setupRecord(_domain, '*')
}
await this.setupRecord(_domain, _record)
this.recordSetup = true
console.log('> DNS Configured! Verifying propagation…')
try {
await this.retry(() => this.verifyOwnership(alias), {
retries: 10,
maxTimeout: 8000
})
} catch (err2) {
const e = new Error(
'> We configured the DNS settings for your alias, but we were unable to ' +
"verify that they've propagated. Please try the alias again later."
)
e.userError = true
throw e
}
} else {
console.log(`> Resolved IP: ${err.ip ? `${chalk.underline(err.ip)} (unknown)` : chalk.dim('none')}`)
console.log(`> Nameservers: ${nameservers && nameservers.length ? nameservers
.map(ns => chalk.underline(ns))
.join(', ') : chalk.dim('none')}`)
throw err
}
} catch (e) {
if (e.userError) {
throw e
}
throw err
}
} else {
throw err
}
}
if (!usingZeitWorld && !skipDNSVerification) {
if (this._debug) {
console.log(`> [debug] Trying to register a non-ZeitWorld domain ${domain} for the current user`)
}
const { uid, verified, created } = await this.setupDomain(domain, {
isExternal: true
})
if (!(created && verified)) {
const e = new Error(
`> Failed to verify the ownership of ${domain}, please refer to 'now domain --help'.`
)
e.userError = true
throw e
}
console.log(`${chalk.cyan('> Success!')} Domain ${chalk.bold(chalk.underline(domain))} ${chalk.dim(`(${uid})`)} added`)
}
console.log(`> Verification ${chalk.bold('OK')}!`)
}
return alias
}
verifyOwnership(domain) {
return this.retry(
async bail => {
const targets = await resolve4('alias.zeit.co')
if (targets.length <= 0) {
return bail(new Error('Unable to resolve alias.zeit.co'))
}
let ips = []
try {
ips = await resolve4(domain)
} catch (err) {
if (
err.code === 'ENODATA' ||
err.code === 'ESERVFAIL' ||
err.code === 'ENOTFOUND'
) {
// Not errors per se, just absence of records
if (this._debug) {
console.log(`> [debug] No records found for "${domain}"`)
}
const err = new Error(DOMAIN_VERIFICATION_ERROR)
err.userError = true
return bail(err)
}
throw err
}
if (ips.length <= 0) {
const err = new Error(DOMAIN_VERIFICATION_ERROR)
err.userError = true
return bail(err)
}
for (const ip of ips) {
if (targets.indexOf(ip) === -1) {
const err = new Error(
`The domain ${domain} has an A record ${chalk.bold(ip)} that doesn't resolve to ${chalk.bold(chalk.underline('alias.zeit.co'))}.\n> ` +
DOMAIN_VERIFICATION_ERROR
)
err.ip = ip
err.userError = true
return bail(err)
}
}
},
{ retries: 5 }
)
}
}