vite-plugin-caddy-multiple-tls 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.js +207 -173
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -115,7 +115,7 @@ api.app.localhost
|
|
|
115
115
|
> - `nip.io`: dynamic parsing of the IP in the hostname (e.g. `app.192.168.1.50.nip.io`) so you can target LAN devices.
|
|
116
116
|
> Why use them: subdomains behave like real domains, no `/etc/hosts` edits, and closer parity for cookies/CORS rules.
|
|
117
117
|
>
|
|
118
|
-
> When using loopback domains, ensure your Vite config allows the Host header, e.g. `server: { allowedHosts: true }`.
|
|
118
|
+
> When using loopback domains, ensure your Vite config allows the Host header and binds to all interfaces, e.g. `server: { allowedHosts: true, host: true }`.
|
|
119
119
|
>
|
|
120
120
|
> For a permanent fix that handles all `*.localhost` domains automatically, install dnsmasq:
|
|
121
121
|
> ```bash
|
package/dist/index.js
CHANGED
|
@@ -366,206 +366,240 @@ function viteCaddyTlsPlugin({
|
|
|
366
366
|
serverName,
|
|
367
367
|
internalTls
|
|
368
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
|
-
|
|
369
|
+
function isPreviewServer(server) {
|
|
370
|
+
return server.config.isProduction;
|
|
371
|
+
}
|
|
372
|
+
function getPreviewPort(config) {
|
|
373
|
+
if (typeof config.preview?.port === "number") {
|
|
374
|
+
return config.preview.port;
|
|
375
|
+
}
|
|
376
|
+
return null;
|
|
377
|
+
}
|
|
378
|
+
function getPreviewHost(config) {
|
|
379
|
+
if (config.preview && "host" in config.preview) {
|
|
380
|
+
return resolveUpstreamHost(config.preview.host);
|
|
381
|
+
}
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
function hasListen(server) {
|
|
385
|
+
return !!server && typeof server === "object" && "listen" in server && typeof server.listen === "function";
|
|
386
|
+
}
|
|
387
|
+
function setupServer(server) {
|
|
388
|
+
const { httpServer, config } = server;
|
|
389
|
+
const previewMode = isPreviewServer(server);
|
|
390
|
+
const fallbackPort = previewMode ? getPreviewPort(config) ?? 4173 : config.server.port || 5173;
|
|
391
|
+
const resolvedDomain = resolveDomain({
|
|
392
|
+
domain,
|
|
393
|
+
baseDomain,
|
|
394
|
+
loopbackDomain,
|
|
395
|
+
repo,
|
|
396
|
+
branch
|
|
397
|
+
});
|
|
398
|
+
const domainArray = resolvedDomain ? [resolvedDomain] : [];
|
|
399
|
+
const routeId = `vite-proxy-${Date.now()}-${Math.floor(Math.random() * 1e3)}`;
|
|
400
|
+
const shouldUseInternalTls = internalTls ?? (baseDomain !== void 0 || loopbackDomain !== void 0 || domain !== void 0);
|
|
401
|
+
const tlsPolicyId = shouldUseInternalTls ? `${routeId}-tls` : null;
|
|
402
|
+
let cleanupStarted = false;
|
|
403
|
+
let resolvedPort = null;
|
|
404
|
+
let resolvedHost = null;
|
|
405
|
+
let setupStarted = false;
|
|
406
|
+
if (domainArray.length === 0) {
|
|
407
|
+
console.error(
|
|
408
|
+
"No domain resolved. Provide domain, or run inside a git repo, or pass repo/branch."
|
|
409
|
+
);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
let tlsPolicyAdded = false;
|
|
413
|
+
function getPortFromAddress(address) {
|
|
414
|
+
if (address && typeof address === "object" && "port" in address) {
|
|
415
|
+
const port = address.port;
|
|
416
|
+
if (typeof port === "number") {
|
|
417
|
+
return port;
|
|
418
|
+
}
|
|
394
419
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
420
|
+
return null;
|
|
421
|
+
}
|
|
422
|
+
function updateResolvedTarget() {
|
|
423
|
+
if (resolvedPort !== null && resolvedHost !== null) return;
|
|
424
|
+
const resolvedUrl = server.resolvedUrls?.local?.[0];
|
|
425
|
+
if (resolvedUrl) {
|
|
426
|
+
try {
|
|
427
|
+
const url = new URL(resolvedUrl);
|
|
428
|
+
if (resolvedHost === null && url.hostname) {
|
|
429
|
+
resolvedHost = url.hostname === "localhost" ? "127.0.0.1" : url.hostname;
|
|
430
|
+
}
|
|
431
|
+
const port = Number(url.port);
|
|
432
|
+
if (resolvedPort === null && !Number.isNaN(port)) {
|
|
433
|
+
resolvedPort = port;
|
|
401
434
|
}
|
|
435
|
+
} catch (e) {
|
|
402
436
|
}
|
|
403
|
-
return null;
|
|
404
437
|
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
if (resolvedHost === null && url.hostname) {
|
|
412
|
-
resolvedHost = url.hostname === "localhost" ? "127.0.0.1" : url.hostname;
|
|
413
|
-
}
|
|
414
|
-
const port = Number(url.port);
|
|
415
|
-
if (resolvedPort === null && !Number.isNaN(port)) {
|
|
416
|
-
resolvedPort = port;
|
|
417
|
-
}
|
|
418
|
-
} catch (e) {
|
|
438
|
+
if (httpServer) {
|
|
439
|
+
const address = httpServer.address();
|
|
440
|
+
if (address && typeof address === "object") {
|
|
441
|
+
const port = getPortFromAddress(address);
|
|
442
|
+
if (resolvedPort === null && port !== null) {
|
|
443
|
+
resolvedPort = port;
|
|
419
444
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
const port = getPortFromAddress(address);
|
|
425
|
-
if (resolvedPort === null && port !== null) {
|
|
426
|
-
resolvedPort = port;
|
|
427
|
-
}
|
|
428
|
-
if (resolvedHost === null && "address" in address) {
|
|
429
|
-
const host = address.address;
|
|
430
|
-
if (typeof host === "string" && host !== "0.0.0.0" && host !== "::") {
|
|
431
|
-
resolvedHost = host;
|
|
432
|
-
}
|
|
445
|
+
if (resolvedHost === null && "address" in address) {
|
|
446
|
+
const host = address.address;
|
|
447
|
+
if (typeof host === "string" && host !== "0.0.0.0" && host !== "::") {
|
|
448
|
+
resolvedHost = host;
|
|
433
449
|
}
|
|
434
450
|
}
|
|
435
451
|
}
|
|
436
|
-
|
|
437
|
-
|
|
452
|
+
}
|
|
453
|
+
if (resolvedPort === null && typeof config.server.port === "number") {
|
|
454
|
+
resolvedPort = config.server.port;
|
|
455
|
+
}
|
|
456
|
+
if (previewMode && resolvedPort === null) {
|
|
457
|
+
const previewPort = getPreviewPort(config);
|
|
458
|
+
if (previewPort !== null) {
|
|
459
|
+
resolvedPort = previewPort;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (resolvedHost === null) {
|
|
463
|
+
if (previewMode) {
|
|
464
|
+
resolvedHost = getPreviewHost(config);
|
|
438
465
|
}
|
|
439
466
|
if (resolvedHost === null) {
|
|
440
467
|
resolvedHost = resolveUpstreamHost(config.server.host);
|
|
441
468
|
}
|
|
442
469
|
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
470
|
+
}
|
|
471
|
+
function getServerPort() {
|
|
472
|
+
updateResolvedTarget();
|
|
473
|
+
return resolvedPort ?? fallbackPort;
|
|
474
|
+
}
|
|
475
|
+
function getUpstreamHost() {
|
|
476
|
+
updateResolvedTarget();
|
|
477
|
+
return resolvedHost ?? "127.0.0.1";
|
|
478
|
+
}
|
|
479
|
+
async function cleanupRoute() {
|
|
480
|
+
if (cleanupStarted) return;
|
|
481
|
+
cleanupStarted = true;
|
|
482
|
+
if (tlsPolicyId) {
|
|
483
|
+
await removeTlsPolicy(tlsPolicyId);
|
|
446
484
|
}
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
485
|
+
await removeRoute(routeId);
|
|
486
|
+
}
|
|
487
|
+
function onServerClose() {
|
|
488
|
+
void cleanupRoute();
|
|
489
|
+
}
|
|
490
|
+
function handleSignal(signal) {
|
|
491
|
+
process.off("SIGINT", onSigint);
|
|
492
|
+
process.off("SIGTERM", onSigterm);
|
|
493
|
+
void cleanupRoute().finally(() => {
|
|
494
|
+
process.kill(process.pid, signal);
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
function onSigint() {
|
|
498
|
+
handleSignal("SIGINT");
|
|
499
|
+
}
|
|
500
|
+
function onSigterm() {
|
|
501
|
+
handleSignal("SIGTERM");
|
|
502
|
+
}
|
|
503
|
+
function registerProcessCleanup() {
|
|
504
|
+
process.once("SIGINT", onSigint);
|
|
505
|
+
process.once("SIGTERM", onSigterm);
|
|
506
|
+
}
|
|
507
|
+
async function setupRoute() {
|
|
508
|
+
if (!validateCaddyIsInstalled()) {
|
|
509
|
+
return;
|
|
450
510
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
if (
|
|
455
|
-
|
|
511
|
+
let running = await isCaddyRunning();
|
|
512
|
+
if (!running) {
|
|
513
|
+
running = await startCaddy();
|
|
514
|
+
if (!running) {
|
|
515
|
+
console.error("Failed to start Caddy server.");
|
|
516
|
+
return;
|
|
456
517
|
}
|
|
457
|
-
await removeRoute(routeId);
|
|
458
|
-
}
|
|
459
|
-
function onServerClose() {
|
|
460
|
-
void cleanupRoute();
|
|
461
|
-
}
|
|
462
|
-
function handleSignal(signal) {
|
|
463
|
-
process.off("SIGINT", onSigint);
|
|
464
|
-
process.off("SIGTERM", onSigterm);
|
|
465
|
-
void cleanupRoute().finally(() => {
|
|
466
|
-
process.kill(process.pid, signal);
|
|
467
|
-
});
|
|
468
|
-
}
|
|
469
|
-
function onSigint() {
|
|
470
|
-
handleSignal("SIGINT");
|
|
471
|
-
}
|
|
472
|
-
function onSigterm() {
|
|
473
|
-
handleSignal("SIGTERM");
|
|
474
518
|
}
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
519
|
+
try {
|
|
520
|
+
await ensureBaseConfig(serverName);
|
|
521
|
+
} catch (e) {
|
|
522
|
+
console.error("Failed to configure Caddy base settings.", e);
|
|
523
|
+
return;
|
|
478
524
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
}
|
|
483
|
-
let running = await isCaddyRunning();
|
|
484
|
-
if (!running) {
|
|
485
|
-
running = await startCaddy();
|
|
486
|
-
if (!running) {
|
|
487
|
-
console.error("Failed to start Caddy server.");
|
|
488
|
-
return;
|
|
489
|
-
}
|
|
490
|
-
}
|
|
525
|
+
const port = getServerPort();
|
|
526
|
+
const upstreamHost = getUpstreamHost();
|
|
527
|
+
if (tlsPolicyId) {
|
|
491
528
|
try {
|
|
492
|
-
await
|
|
529
|
+
await addTlsPolicy(tlsPolicyId, domainArray);
|
|
530
|
+
tlsPolicyAdded = true;
|
|
493
531
|
} catch (e) {
|
|
494
|
-
console.error("Failed to
|
|
532
|
+
console.error("Failed to add TLS policy to Caddy.", e);
|
|
495
533
|
return;
|
|
496
534
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
} catch (e) {
|
|
504
|
-
console.error("Failed to add TLS policy to Caddy.", e);
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
try {
|
|
509
|
-
await addRoute(routeId, domainArray, port, cors, serverName, upstreamHost);
|
|
510
|
-
} catch (e) {
|
|
511
|
-
if (tlsPolicyAdded && tlsPolicyId) {
|
|
512
|
-
await removeTlsPolicy(tlsPolicyId);
|
|
513
|
-
}
|
|
514
|
-
console.error("Failed to add route to Caddy.", e);
|
|
515
|
-
return;
|
|
535
|
+
}
|
|
536
|
+
try {
|
|
537
|
+
await addRoute(routeId, domainArray, port, cors, serverName, upstreamHost);
|
|
538
|
+
} catch (e) {
|
|
539
|
+
if (tlsPolicyAdded && tlsPolicyId) {
|
|
540
|
+
await removeTlsPolicy(tlsPolicyId);
|
|
516
541
|
}
|
|
517
|
-
console.
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
542
|
+
console.error("Failed to add route to Caddy.", e);
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
console.log("\n\u{1F512} Caddy is proxying your traffic on https");
|
|
546
|
+
console.log(
|
|
547
|
+
`
|
|
548
|
+
\u{1F517} Access your local ${domainArray.length > 1 ? "servers" : "server"}!`
|
|
549
|
+
);
|
|
550
|
+
domainArray.forEach((domain2) => {
|
|
551
|
+
console.log(`\u{1F30D} https://${domain2}`);
|
|
552
|
+
});
|
|
553
|
+
if (process.platform === "linux" && !loopbackDomain) {
|
|
554
|
+
console.log("\n\u{1F427} Linux users: if the domain doesn't resolve, run:");
|
|
523
555
|
domainArray.forEach((domain2) => {
|
|
524
|
-
console.log(
|
|
556
|
+
console.log(` echo "127.0.0.1 ${domain2}" | sudo tee -a /etc/hosts`);
|
|
525
557
|
});
|
|
526
|
-
if (process.platform === "linux" && !loopbackDomain) {
|
|
527
|
-
console.log();
|
|
528
|
-
console.log("\u{1F427} Linux users: if the domain doesn't resolve, run:");
|
|
529
|
-
domainArray.forEach((domain2) => {
|
|
530
|
-
console.log(` echo "127.0.0.1 ${domain2}" | sudo tee -a /etc/hosts`);
|
|
531
|
-
});
|
|
532
|
-
}
|
|
533
|
-
console.log();
|
|
534
|
-
registerProcessCleanup();
|
|
535
|
-
httpServer?.once("close", onServerClose);
|
|
536
558
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
}
|
|
557
|
-
function onListening() {
|
|
558
|
-
updateResolvedTarget();
|
|
559
|
-
runSetupOnce();
|
|
560
|
-
}
|
|
561
|
-
const listenWrapped = wrapServerListen();
|
|
562
|
-
if (httpServer?.listening) {
|
|
563
|
-
runSetupOnce();
|
|
564
|
-
} else if (httpServer) {
|
|
565
|
-
httpServer.once("listening", onListening);
|
|
566
|
-
} else if (!listenWrapped) {
|
|
559
|
+
console.log();
|
|
560
|
+
registerProcessCleanup();
|
|
561
|
+
httpServer?.once("close", onServerClose);
|
|
562
|
+
}
|
|
563
|
+
function runSetupOnce() {
|
|
564
|
+
if (setupStarted) return;
|
|
565
|
+
setupStarted = true;
|
|
566
|
+
void setupRoute();
|
|
567
|
+
}
|
|
568
|
+
function wrapServerListen() {
|
|
569
|
+
if (!hasListen(server)) return false;
|
|
570
|
+
const originalListen = server.listen.bind(server);
|
|
571
|
+
server.listen = async function(port, isRestart) {
|
|
572
|
+
const result = await originalListen(port, isRestart);
|
|
573
|
+
if (typeof port === "number") {
|
|
574
|
+
resolvedPort = port;
|
|
575
|
+
} else {
|
|
576
|
+
updateResolvedTarget();
|
|
577
|
+
}
|
|
567
578
|
runSetupOnce();
|
|
568
|
-
|
|
579
|
+
return result;
|
|
580
|
+
};
|
|
581
|
+
return true;
|
|
582
|
+
}
|
|
583
|
+
function onListening() {
|
|
584
|
+
updateResolvedTarget();
|
|
585
|
+
runSetupOnce();
|
|
586
|
+
}
|
|
587
|
+
const listenWrapped = wrapServerListen();
|
|
588
|
+
if (httpServer?.listening) {
|
|
589
|
+
runSetupOnce();
|
|
590
|
+
} else if (httpServer) {
|
|
591
|
+
httpServer.once("listening", onListening);
|
|
592
|
+
} else if (!listenWrapped) {
|
|
593
|
+
runSetupOnce();
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
return {
|
|
597
|
+
name: "vite:caddy-tls",
|
|
598
|
+
configureServer(server) {
|
|
599
|
+
setupServer(server);
|
|
600
|
+
},
|
|
601
|
+
configurePreviewServer(server) {
|
|
602
|
+
setupServer(server);
|
|
569
603
|
}
|
|
570
604
|
};
|
|
571
605
|
}
|