libetl / local-traffic

tiny reverse-proxy made with typescript
The Unlicense
7 stars 1 forks source link

🖧 local-traffic

That is a secure http/2 (or insecure http1.1) reverse-proxy installed on your machine

How simple is that ?

pre-requisite

node.js >= 8

how to start in less than one minute

npx local-traffic

sudo prefix only required for Linux, when port number < 1024

how to use it

  1. Change the mapping object in the .local-traffic.json file:
{
  "mapping": {
    "/npm/": "https://www.npmjs.com/",
    "/my-static-webapp/(.*)": "file:///home/user/projects/my-static-webapp/$$1",
    "/welcome/": "data:text/html,<a href=\"https://ac.me/acme.js\">See my hobby project</a>",
    "/(see-this-example|yet-another-example)": "http://example.com/$$1",
    "/config/": "config://",
    "/logs/": "logs://",
    "/recorder/": "recorder://",
    "/jquery-local/jquery.js": {
      "replaceBody": "https://ac.me/acme.js",
      "downstreamUrl": "file:///home/user/projects/zepto/dist/zepto.js"
    },
    "": "https://github.com/"
  }
}

if you need to deactivate a mapping entry, move it below the "" key

  1. Go to http://localhost:8080/prettier with your browser
  2. Go to http://localhost:8080/npm/ with your browser
  3. Go to http://localhost:8080/my-static-webapp/index.html with your browser (given your project name is my-static-webapp, but I am not 100% sure)
  4. Go to http://localhost:8080/see-this-example or to http://localhost:8080/yet-another-example with your browser. Starting 0.0.89 and above, it supports regular expressions, and it is able to match them against the destination through string interpolation. Start with a double dollar sign ($$) followed by the index of the value in the match array
  5. Go to http://localhost:8080/welcome/ with your browser (data urls work with version >= 0.0.95)
  6. Go to http://localhost:8080/logs/ to watch the request logs
  7. Go to http://localhost:8080/config/ to change the config in a web editor
  8. You can use the http://localhost:8080/recorder/ to turn your proxy into a mock server. There is a user interface and also an API (documented here)
  9. From the web config editor, create a SSL keypair and start working with a self signed SSL certificate right away
  10. Your page will use /jquery-local/jquery.js instead of the CDN asset, and will serve the file from your hard drive
  11. Your server now proxies the mapping that you have configured

usage

from your terminal, using the command line

npx local-traffic [location-of-the-local-traffic-config-file]

When not specified, the location of the config file will be $HOME/.local-traffic.json

from a node.js application (>= 0.0.72)

 node -e 'const { start } = require("local-traffic"); start({ /* configuration goes here */ })'

how to change mappings to local / non-local

  1. Open .local-traffic.json while running it, or use the config web editor
  2. Edit the mapping keys and downstream urls
  3. See the status update in the terminal, that's it.

all the options

All boolean settings default to false when unspecified.

recorder API

(>= 0.0.86) The recorder can be used programmatically with an API. This can be used if someone needs to automatically record mocks during instance provisioning (when the machine boots up using a cloud provider for example)

The API always matches the route targetting recorder://.

post, put

Arguments : parameter Type Description Defaults
mode "proxy" or "mock" server mode "proxy"
strict boolean errors when no mock is found false
autoRecord boolean adds mocks from server false
mocks {uniqueHash,response}[] mocks definition []

The recorder webapp can take care of the mocks by itself, so autoRecord is only necessary when using local-traffic headless or without human intervention

delete

The mock config will be reset to empty :

get

Retrieves the current mock configuration. use Accept: application/json to use the API mode.

$ curl https://localhost:8443/recorder/ -XGET -k -H'Accept: application/json'
{"mocks":[],"strict":false,"autoRecord":false,"mode":"proxy"}