Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ngrok service for sharing the local environment #335

Open
3 tasks
roborourke opened this issue Sep 16, 2021 · 1 comment
Open
3 tasks

Add an ngrok service for sharing the local environment #335

roborourke opened this issue Sep 16, 2021 · 1 comment

Comments

@roborourke
Copy link
Contributor

roborourke commented Sep 16, 2021

We have a working proof of concept courtesy of @kadamwhite so far:

Instructions:

  • Create the current two files in a new directory.
  • run npm install
  • run TARGET_HOST=dev.altis.dev npm start while replacing dev.altis.dev with your local env domain
  • Figure out your machine network local IP
  • View the site at http://LOCAL-IP-HERE:3000

server.js:

const express = require( 'express' );
const morgan = require( 'morgan' );
const proxy = require( 'http-proxy-middleware' );
const ngrok = require( 'ngrok' );

const createProxyMiddleware = proxy.createProxyMiddleware;
const responseInterceptor = proxy.responseInterceptor;

const PORT = 3000;
const HOST = 'localhost';
const TARGET_HOST = process.env.TARGET_HOST;

( async () => {
    const externalUrl = await ngrok.connect( PORT );

    const externalHost = externalUrl.replace( /https?:\/\//, '' );

    const app = express();

    app.use( morgan( 'dev' ) );

    // Proxy everything.
    app.use( '/', createProxyMiddleware( {
        target: TARGET_HOST,

        changeOrigin: true,

        // Defer res.end() to be handled by responseInterceptor.
        selfHandleResponse: true,

        /**
         * Intercept response and replace Snopes URL with ngrok URL.
         * Remove integrity hashes from markup so static assets load.
         **/
        onProxyRes: responseInterceptor( async ( responseBuffer ) => {
            return responseBuffer.toString( 'utf8' )
                // Point Tachyon URLs at a web-accessible bucket.
                .replace( /TARGET_HOST\/tachyon/ig, 'LIVE_HOST/tachyon' )
                // Everything else, rewrite so it will load that resource
                // over the ngrok connection.
                .replace( /TARGET_HOST/ig, externalHost )
                // Remove integrity hashes entirely :( or things won't work.
                .replace( /integrity=('[^']+'|"[^"]+")/ig, '' );
        } ),
    } ) );

    app.listen( PORT, HOST, () => {
        console.log( `Starting local Proxy at ${HOST}:${PORT}` );
        console.log( `Local environment is now available at ${ externalUrl }` );
    } );

} )();

package.json:

{
  "name": "local-server-proxy",
  "private": true,
  "version": "1.0.0",
  "description": "Relay web traffic from an external URL to Local Server",
  "main": "index.js",
  "scripts": {
    "proxy": "node server.js",
    "start": "node server.js"
  },
  "author": "Human Made",
  "license": "MIT",
  "dependencies": {
    "express": "^4.17.1",
    "http-proxy-middleware": "^2.0.1",
    "morgan": "^1.10.0",
    "ngrok": "^4.1.0"
  }
}

Acceptance criteria:

  • Create a containerised version of the above node app
  • Add a command called composer server share that starts the service
  • A SIGQUIT input should stop the service eg. ctrl+c
@joehoyle
Copy link
Member

@rmccue mentioned https://github.com/beyondcode/expose which I think could make a quite interesting implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants