Code Monkey home page Code Monkey logo

straightforward's Introduction

🏴 straightforward npm bundle size

A straightforward forward-proxy written in Node.js

Goals

  • Extremely focused (~200 SLOC), no-fuzz forward proxy
  • Support HTTP, HTTPS, CONNECT & Websockets (wss)
  • Performant: By default all requests/responses are streamed
  • No external dependencies, small, self-contained, tested
  • Support both cli and extensible programmatic usage
  • Straightforward: no implicit magic or abstractions

What you can do with it

  • Start an explicit forwarding proxy in seconds that just works
  • Optionally use authentication
  • Mock responses to test code using a proxy
  • Allow others to surf with your IP address
  • Use it programmatically to do whatever you want

What this is not

Installation

# Use directly with no installation (npx is part of npm):
❯❯❯ npx straightforward --port 9191

# Or install globally:
❯❯❯ npm install -g straightforward

Usage (cli)

❯❯❯ straightforward --help

Usage: straightforward --port 9191 [options]

Options:
      --version        Show version number                             [boolean]
  -p, --port           Port to bind on                  [number] [default: 9191]
  -a, --auth           Enable proxy authentication                      [string]
  -e, --echo           Enable echo mode (mock all http responses)      [boolean]
  -d, --debug          Enabled debug output                            [boolean]
  -c, --cluster        Run a cluster of proxies (using number of CPUs) [boolean]
      --cluster-count  Specify how many cluster workers to spawn        [number]
  -q, --quiet          Suppress request logs                           [boolean]
  -s, --silent         Don't print anything to stdout                  [boolean]
  -h, --help           Show help                                       [boolean]

Examples:
  straightforward --auth "user:pass"  Require authentication
  straightforward --echo              Mock responses for all http requests

Use with cURL:
  curl --proxy https://localhost:9191 'http://example.com' -v
  curl --proxy https://user:pass@localhost:9191 'http://example.com' -v

Usage (code)

// ESM/TS: import { Straightforward, middleware } from "straightforward"
const { Straightforward, middleware } = require("straightforward")

;(async () => {
  // Start proxy server
  const sf = new Straightforward()
  await sf.listen(9191)
  console.log(`Proxy listening on http://localhost:9191`)

  // Log http requests
  sf.onRequest.use(async ({ req, res }, next) => {
    console.log(`http request: ${req.url}`)
    // Note the common middleware pattern, use `next()`
    // to pass the request to the next handler.
    return next()
  })

  // Log connect (https) requests
  sf.onConnect.use(async ({ req }, next) => {
    console.log(`connect request: ${req.url}`)
    return next()
  })

  // Use built-in middleware for authentication
  sf.onRequest.use(middleware.auth({ user: "bob", pass: "alice" }))
  sf.onConnect.use(middleware.auth({ user: "bob", pass: "alice" }))

  // Use built-in middleware to mock responses for all http requests
  sf.onRequest.use(middleware.echo)
})()

In action

❯❯❯ straightforward --port 9191

foobar

Example: Secure proxy on fresh server in 30 seconds

Let's say you have a fresh linux server and want to use it as an authenticated forward proxy quickly.

  • Make sure nvm is installed:
    • curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
  • Make sure a recent version of Node.js is installed:
    • nvm install node && nvm use node && node --version
  • Add forever (process manager) and straightforward:
    • npm install -g forever straightforward
  • Start proxy daemon:
    • forever start --id "proxy1" $( which straightforward ) --port 9191 --quiet --auth 'user:foobar'
  • Test your proxy from a different machine:
    • curl --proxy http://user:foobar@SERVER:9191/ http://canhazip.com
  • List all running forever services:
    • forever list
  • Stop our proxy service daemon:
    • forever stop proxy1

API

onRequest

Middlewares triggered when http requests occur

sf.onRequest.use(async ({ req, res }, next) => {
  console.log(`http request: ${req.url}`)
  // Note the common middleware pattern, use `next()`
  // to pass the request to the next handler.
  return next()
})

Middlwares can be chained:

sf.onRequest.use(
  async ({ req, res }, next) => {
    console.log(`middleware1`)
    return next()
  },
  async ({ req, res }, next) => {
    console.log(`middleware2`)
    res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" })
    res.end("Hello world")
  }
)

onResponse

Middlewares triggered when http request responses are available

sf.onResponse.use(async ({ req, res, proxyRes }, next) => {
  console.log(`http response`)
  return next()
})

onConnect

Middlewares triggered when https and wss requests occur

sf.onConnect.use(async ({ req, clientSocket, head }, next) => {
  console.log(`connect request`)
  return next()
})

License

MIT

straightforward's People

Contributors

berstend avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

straightforward's Issues

[RFE] Allow modify HTTPS requests

Allow modify HTTPS requests as man-in-the-middle proxy.

One of the reasons to use forward-proxy is to be able to modify the request before reaching the website and it would be valuable to provide this ability also to HTTPS requests (by performing SSL inspection).

Maximum call stack size exceeded

After a while, the server starts throwing the below error and stops handling requests.

RangeError: Maximum call stack size exceeded
(node:1) UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded
    at exports.basics (/usr/local/lib/node_modules/straightforward/lib/middleware.js:9:18)
    at dispatch (/usr/local/lib/node_modules/straightforward/lib/utils.js:42:32)
    at exports.basics (/usr/local/lib/node_modules/straightforward/lib/middleware.js:17:10)
    at dispatch (/usr/local/lib/node_modules/straightforward/lib/utils.js:42:32)
    at exports.basics (/usr/local/lib/node_modules/straightforward/lib/middleware.js:17:10)
    at dispatch (/usr/local/lib/node_modules/straightforward/lib/utils.js:42:32)
    at exports.basics (/usr/local/lib/node_modules/straightforward/lib/middleware.js:17:10)
    at dispatch (/usr/local/lib/node_modules/straightforward/lib/utils.js:42:32)
    at exports.basics (/usr/local/lib/node_modules/straightforward/lib/middleware.js:17:10)
    at dispatch (/usr/local/lib/node_modules/straightforward/lib/utils.js:42:32)
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 18870)
Exception in PromiseRejectCallback:
/usr/local/lib/node_modules/straightforward/lib/utils.js:44
        return Promise.reject(err)

How about config?

Found this CLI - seems to be quite an amazing tool eveing being so tiny.

Funny thing - but was not able to set up similar thing in nginx because of some internal network issues - but this works as a charm.

However without configuration file it still makes this utility very limited.

It would be great to think of kind of a config file which can support the most common use cases which Nginx supports. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.