Verified Commit d1348f69 authored by Aral Balkan's avatar Aral Balkan
Browse files

Add HTTP → HTTPS and WS → WSS reverse proxy (for local mode only)

parents 508247ce 1a85b3ba
......@@ -8,12 +8,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Nothing yet.
## [8.0.0] - TBR
## [8.1.0] - 2019-04-18
## TODO
## Added
- HTTP → HTTPS and WS → WSS reverse proxy (for local mode only).
- [ ] Test 8.0.0 on macOS
- [ ] Test 8.0.0 on clean/new VPS instances
## [8.0.0] - 2019-04-16
## Added
......
......@@ -16,16 +16,14 @@ __Indie Web Server is a secure and seamless [Small Tech](https://ar.al/2019/03/0
Copy and paste the following commands into your terminal:
### Linux
### Linux and macOS
```
wget https://ind.ie/web-server/linux/8.0.0.zip && unzip 8.0.0.zip && chmod +x web-server && sudo mv web-server /usr/local/bin/
```
Install the native binaries:
### macOS
__Before you pipe any script into your computer, always [view the source code](https://ind.ie/web-server/install.sh) and make sure you understand what it does.__
```
wget https://ind.ie/web-server/macos/8.0.0.zip && unzip 8.0.0.zip && chmod +x web-server && sudo mv web-server /usr/local/bin/
wget -qO- https://ind.ie/web-server/install.sh | bash
```
### Node.js
......@@ -34,6 +32,8 @@ wget https://ind.ie/web-server/macos/8.0.0.zip && unzip 8.0.0.zip && chmod +x we
npm i -g @ind.ie/web-server
```
There is currently no native binary support for Windows. Please use the npm installation method on that platform.
## Use
### Local
......@@ -44,6 +44,14 @@ Start serving the current directory at https://localhost as a regular process us
$ web-server
```
You can also use Indie Web Server as a development-time reverse proxy for HTTP and WebSocket connections. For example, if you use [Hugo](https://gohugo.io/) and you’re running `hugo server` on the default HTTP port 1313. You can run a HTTPS reverse proxy at https://localhost [with LiveReload support]() with:
```shell
$ web-server http://localhost:1313
```
The reverse proxy feature is currently not available for use with the `global` or `enable` features.
### Global (ephemeral)
__Available on Linux and macOS only*__
......
......@@ -54,7 +54,7 @@ const firstPositionalArgumentDidMatchCommand = ['version', 'help', 'global', 'en
// Help / usage instructions.
if (command.isHelp) {
const usageCommand = `${clr('command', 'green')}`
const usageFolderToServe = clr('folder', 'cyan')
const usageFolderToServe = `${clr('folder', 'cyan')}${clr('|url', 'darkgrey')}`
const usageOptions = clr('options', 'yellow')
const usageVersion = `${clr('version', 'green')}`
......@@ -75,7 +75,7 @@ if (command.isHelp) {
${clr('web-server', 'bold')} [${usageCommand}] [${usageFolderToServe}] [${usageOptions}]
${usageCommand}\t${usageVersion} | ${usageHelp} | ${usageLocal} | ${usageGlobal} | ${usageEnable} | ${usageDisable} | ${usageLogs} | ${usageStatus}
${usageFolderToServe}\tPath of folder to serve (defaults to current folder).
${usageFolderToServe}\tPath of folder to serve (defaults to current folder) or HTTP URL to reverse proxy.
${usageOptions}\tSettings that alter server characteristics.
${clr('Commands:', 'underline')}
......@@ -197,15 +197,84 @@ switch (true) {
global = true
}
if (!fs.existsSync(pathToServe)) {
console.error(`\n 🤔 Error: could not find path ${pathToServe}\n`)
process.exit(1)
let isProxy = false
if (pathToServe.startsWith('http://')) {
isProxy = true
} else {
if (!fs.existsSync(pathToServe)) {
console.error(`\n 🤔 Error: could not find path ${pathToServe}\n`)
process.exit(1)
}
}
//
// Launch as startup daemon or regular process?
// Launch as a reverse proxy (local mode), startup daemon, or regular process?
//
if (command.isEnable) {
if (isProxy) {
//
// Proxy HTTP → HTTPS and WS → WSS.
//
const proxy = require('http-proxy-middleware')
const express = require('express')
const app = express()
console.log(webServer.version())
const server = webServer.createServer({}, app).listen(port, () => {
console.log(`\n 🚚 Proxying: HTTPS/WSS on localhost:${port} ←→ HTTP/WS on ${pathToServe.replace('http://', '')}\n`)
const webSocketProxy = proxy(pathToServe.replace('http://', 'ws://'), {
ws: true,
changeOrigin:false,
logLevel: 'info'
})
const httpsProxy = proxy({
target: pathToServe,
changeOrigin: true,
logLevel: 'info',
//
// Special handling of LiveReload implementation bug in Hugo
// (https://github.com/gohugoio/hugo/issues/2205#issuecomment-484443057)
// to work around the port being hardcoded to the Hugo server
// port (instead of the port that the page is being served from).
//
// This enables you to use Indie Web Server as a reverse proxy
// for Hugo during development time and test your site from https://localhost
//
// All other content is left as-is.
//
onProxyRes: (proxyResponse, request, response) => {
const _write = response.write
// As we’re going to change it.
delete proxyResponse.headers['content-length']
response.write = function (data) {
let output = data.toString('utf-8')
if (output.match(/livereload.js\?port=1313/) !== null) {
console.log('Rewriting Hugo LiveReload URL')
output = output.replace('livereload.js?port=1313', `livereload.js?port=${port}`)
_write.call(response, output)
} else {
_write.call(response, data)
}
}
}
})
app.use(httpsProxy)
app.use(webSocketProxy)
// As we’re using a custom server, manually listen for the http upgrade event
// and upgrade the web socket proxy also.
// (See https://github.com/chimurai/http-proxy-middleware#external-websocket-upgrade)
server.on('upgrade', webSocketProxy.upgrade)
})
} else if (command.isEnable) {
//
// Launch as startup daemon.
//
......
This diff is collapsed.
{
"name": "@ind.ie/web-server",
"version": "8.0.0",
"version": "8.1.0",
"description": "A secure and seamless Small Tech personal web server.",
"main": "index.js",
"bin": "bin/web-server.js",
......@@ -26,6 +26,7 @@
"copy-concurrently": "^1.0.5",
"express": "^4.16.4",
"helmet": "^3.16.0",
"http-proxy-middleware": "^0.19.1",
"minimist": "^1.2.0",
"morgan": "^1.9.1",
"recursive-copy": "^2.0.10",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment