|
1 |
| -import type { ReverseProxyOption } from '../src/types' |
2 |
| -import os from 'node:os' |
3 |
| -import { CAC, log } from '@stacksjs/cli' |
4 |
| -import { readFileSync, writeFileSync } from '@stacksjs/storage' |
| 1 | +import { CAC } from '@stacksjs/cli' |
5 | 2 | import { version } from '../package.json'
|
6 | 3 | import { config } from '../src/config'
|
| 4 | +import { httpsConfig } from '../src/https' |
7 | 5 | import { startProxy } from '../src/start'
|
| 6 | +import { caCertPath, certPath, keyPath } from '../src/utils' |
8 | 7 |
|
9 | 8 | const cli = new CAC('reverse-proxy')
|
10 | 9 |
|
| 10 | +interface ReverseProxyOption { |
| 11 | + from: string |
| 12 | + to: string |
| 13 | + keyPath: string |
| 14 | + certPath: string |
| 15 | + caCertPath: string |
| 16 | + etcHostsCleanup: boolean |
| 17 | + verbose: boolean |
| 18 | +} |
| 19 | + |
11 | 20 | cli
|
12 | 21 | .command('start', 'Start the Reverse Proxy Server')
|
13 | 22 | .option('--from <from>', 'The URL to proxy from', { default: config.from })
|
14 | 23 | .option('--to <to>', 'The URL to proxy to', { default: config.to })
|
15 |
| - .option('--key-path <path>', 'Absolute path to the SSL key', { default: config.keyPath }) |
16 |
| - .option('--cert-path <path>', 'Absolute path to the SSL certificate', { default: config.certPath }) |
| 24 | + .option('--key-path <path>', 'Absolute path to the SSL key', { default: keyPath() }) |
| 25 | + .option('--cert-path <path>', 'Absolute path to the SSL certificate', { default: certPath() }) |
| 26 | + .option('--ca-cert-path <path>', 'Absolute path to the SSL CA certificate', { default: caCertPath() }) |
| 27 | + .option('--etc-hosts-cleanup', 'Cleanup /etc/hosts on exit', { default: config.etcHostsCleanup }) |
17 | 28 | .option('--verbose', 'Enable verbose logging', { default: config.verbose })
|
18 | 29 | .example('reverse-proxy start --from localhost:5173 --to my-project.localhost')
|
19 | 30 | .example('reverse-proxy start --from localhost:3000 --to my-project.localhost/api')
|
20 | 31 | .example('reverse-proxy start --from localhost:3000 --to localhost:3001')
|
21 | 32 | .example('reverse-proxy start --from localhost:5173 --to my-project.test --key-path /absolute/path/to/key --cert-path /absolute/path/to/cert')
|
22 | 33 | .action(async (options?: ReverseProxyOption) => {
|
| 34 | + const https = { |
| 35 | + ...httpsConfig(), |
| 36 | + keyPath: options?.keyPath || keyPath(), |
| 37 | + certPath: options?.certPath || certPath(), |
| 38 | + caCertPath: options?.caCertPath || caCertPath(), |
| 39 | + } |
| 40 | + |
23 | 41 | startProxy({
|
24 | 42 | from: options?.from,
|
25 | 43 | to: options?.to,
|
26 |
| - keyPath: options?.keyPath, |
27 |
| - certPath: options?.certPath, |
| 44 | + https, |
| 45 | + etcHostsCleanup: options?.etcHostsCleanup, |
28 | 46 | verbose: options?.verbose,
|
29 | 47 | })
|
30 | 48 | })
|
31 | 49 |
|
32 |
| -cli |
33 |
| - .command( |
34 |
| - 'update:etc-hosts', |
35 |
| - 'Update the /etc/hosts file with the proxy domains. Please note, this command requires sudo/admin permissions.', |
36 |
| - ) |
37 |
| - .alias('update-etc-hosts') |
38 |
| - .example('sudo reverse-proxy update:etc-hosts') |
39 |
| - .example('sudo reverse-proxy update-etc-hosts') |
40 |
| - .action(async () => { |
41 |
| - log.info('Ensuring /etc/hosts file covers the proxy domain/s...') |
42 |
| - |
43 |
| - const hostsFilePath = os.platform() === 'win32' ? 'C:\\Windows\\System32\\drivers\\etc\\hosts' : '/etc/hosts' |
44 |
| - |
45 |
| - if (config && typeof config === 'object') { |
46 |
| - const entriesToAdd = Object.entries(config).map( |
47 |
| - ([from, to]) => `127.0.0.1 ${to} # reverse-proxy mapping for ${from}`, |
48 |
| - ) |
49 |
| - // Ensure "127.0.0.1 localhost" is in the array |
50 |
| - entriesToAdd.push('127.0.0.1 localhost # essential localhost mapping') |
51 |
| - |
52 |
| - try { |
53 |
| - let currentHostsContent = readFileSync(hostsFilePath, 'utf8') |
54 |
| - let updated = false |
55 |
| - |
56 |
| - for (const entry of entriesToAdd) { |
57 |
| - const [ip, host] = entry.split(' ', 2) |
58 |
| - // Use a regex to match the line with any amount of whitespace between IP and host |
59 |
| - const regex = new RegExp(`^${ip}\\s+${host.split(' ')[0]}(\\s|$)`, 'm') |
60 |
| - // Check if the entry (domain) is already in the file |
61 |
| - if (!regex.test(currentHostsContent)) { |
62 |
| - // If not, append it |
63 |
| - currentHostsContent += `\n${entry}` |
64 |
| - updated = true |
65 |
| - } |
66 |
| - else { |
67 |
| - log.info(`Entry for ${host} already exists in the hosts file.`) |
68 |
| - } |
69 |
| - } |
70 |
| - |
71 |
| - if (updated) { |
72 |
| - writeFileSync(hostsFilePath, currentHostsContent, 'utf8') |
73 |
| - log.success('Hosts file updated with latest proxy domains.') |
74 |
| - } |
75 |
| - else { |
76 |
| - log.info('No new entries were added to the hosts file.') |
77 |
| - } |
78 |
| - } |
79 |
| - catch (error: unknown) { |
80 |
| - if ((error as NodeJS.ErrnoException).code === 'EACCES') |
81 |
| - console.error('Permission denied. Please run this command with administrative privileges.') |
82 |
| - else console.error(`An error occurred: ${(error as NodeJS.ErrnoException).message}`) |
83 |
| - } |
84 |
| - } |
85 |
| - else { |
86 |
| - console.log('No proxies found. Is your config configured properly?') |
87 |
| - } |
88 |
| - }) |
89 |
| - |
90 | 50 | cli.command('version', 'Show the version of the Reverse Proxy CLI').action(() => {
|
91 | 51 | console.log(version)
|
92 | 52 | })
|
|
0 commit comments