Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Mar 7, 2024
1 parent f28b118 commit 712a5bf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.idea
*.log
*.tgz
*.pem
coverage
dist
lib-cov
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
## Features

- Reverse Proxy
- SSL Support
- Custom Domains
- Auto HTTP to HTTPS Redirection
- `/etc/hosts` Management
- Dependency-free Binary
- Zero-config Setup
- SSL Support _(HTTPS by default)_
- Custom Domains _(with wildcard support)_
- Auto HTTP-to-HTTPS Redirection
- `/etc/hosts` Management _(auto-updating)_
- Dependency-Free Binary
- Zero-Config Setup

## Install

Expand Down
Binary file modified bun.lockb
Binary file not shown.
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
],
"scripts": {
"build": "bun build.ts && bun run compile",
"compile": "bun build ./bin/cli.ts --compile --minify --sourcemap --outfile dist/reverse-proxy",
"compile": "bun build ./bin/cli.ts --compile --minify --sourcemap --outfile dist/reverse-proxy && mv ./bin/reverse-proxy ./dist/reverse-proxy",
"lint": "eslint .",
"lint:fix": "bunx eslint . --fix",
"fresh": "bunx rimraf node_modules/ bun.lock && bun i",
Expand All @@ -61,13 +61,12 @@
"docs:preview": "vitepress preview docs"
},
"dependencies": {
"@stacksjs/cli": "^0.59.6",
"@stacksjs/path": "^0.59.6",
"@stacksjs/storage": "^0.59.6",
"@stacksjs/cli": "^0.59.9",
"@stacksjs/storage": "^0.59.9",
"c12": "^1.9.0"
},
"devDependencies": {
"@stacksjs/development": "^0.59.6",
"@stacksjs/development": "^0.59.9",
"@types/bun": "^1.0.8",
"@types/node": "^20.11.24",
"bun-plugin-dts-auto": "^0.10.0",
Expand Down
36 changes: 25 additions & 11 deletions src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import * as net from 'node:net'
import * as http from 'node:http'
import * as https from 'node:https'
import * as fs from 'node:fs'
import process from 'node:process'
import type { Buffer } from 'node:buffer'
import { bold, green, log } from '@stacksjs/cli'
import { path } from '@stacksjs/path'
import path from 'node:path'
import { bold, green, log, runCommand } from '@stacksjs/cli'
import { version } from '../package.json'

interface Option {
Expand All @@ -17,29 +18,42 @@ interface Option {

type Options = Option | Option[]

export function startServer(option: Option = { from: 'localhost:3000', to: 'stacks.localhost' }): void {
export async function startServer(option: Option = { from: 'localhost:3000', to: 'stacks.localhost' }): Promise<void> {
log.debug('Starting Reverse Proxy Server')

let key: Buffer | undefined
let cert: Buffer | undefined

const keyPath = option.keyPath ?? path.projectStoragePath(`keys/localhost-key.pem`)
const keyPath = option.keyPath ?? path.resolve(import.meta.dir, `keys/localhost-key.pem`)
if (fs.existsSync(keyPath))
key = fs.readFileSync(option.keyPath ?? path.projectStoragePath(`keys/localhost-key.pem`))
key = fs.readFileSync(keyPath)
else
log.debug('No SSL key found')


const certPath = option.certPath ?? path.projectStoragePath(`certs/localhost.pem`)
const certPath = option.certPath ?? path.resolve(import.meta.dir, `keys/localhost.pem`)
if (fs.existsSync(certPath))
cert = fs.readFileSync(option.certPath ?? path.projectStoragePath(`certs/localhost.pem`))
cert = fs.readFileSync(certPath)
else
log.debug('No SSL certificate found')

if (!fs.existsSync(keyPath) || fs.existsSync(certPath)) {
log.info('Because no valid SSL key or certificate was found, creating a self-signed certificate')
// wip using mkcert
log.info('A valid SSL key & certificate was not found')
log.info('Creating a self-signed certificate...')

// self-sign a certificate using mkcert
const keysPath = path.resolve(import.meta.dir, 'keys')
if (!fs.existsSync(keysPath))
fs.mkdirSync(keysPath)

await runCommand('mkcert -install', {
cwd: keysPath,
})

await runCommand(`mkcert *.${option.to}`, {
cwd: keysPath,
})
}

// Parse the option.from URL to dynamically set hostname and port
const fromUrl = new URL(option.from ? (option.from.startsWith('http') ? option.from : `http://${option.from}`) : 'http://localhost:3000')
const hostname = fromUrl.hostname
Expand Down Expand Up @@ -115,7 +129,7 @@ function startHttpRedirectServer(): void {
}

export function startProxy(option?: Option): void {
startProxies(option)
startServer(option)
}

export function startProxies(options?: Options): void {
Expand Down

0 comments on commit 712a5bf

Please sign in to comment.