JSPM

  • Created
  • Published
  • Downloads 269
  • Score
    100M100P100Q68577F
  • License ISC

Transparent HTTP-Proxy-Server. Upstream your requests whatever you want

Package Exports

  • transparent-proxy

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (transparent-proxy) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Intro

transparent-proxy extends the native net.createServer and it acts as a real transparent http-proxy.

This module was built on top of TCP-level to avoid header-stripping problem of nodejs http(s)-modules.

It allows to upstream client-request dynamically to other proxies, or to certain iFace, and more... supporting Proxy-Authentication.

Note: It only supports Basic authentication!

Quick Start

Install

npm i transparent-proxy

Use

const ProxyServer = require('transparent-proxy');

//init ProxyServer
const server = new ProxyServer();

//starting server on port 8080
server.listen(8080, '0.0.0.0', function () {
    console.log('TCP-Proxy-Server started!', server.address());
});

Options Object

Param Type Description
options Object The options object.
[options.verbose] Boolean Activate verbose mode
[options.upstream] Function The proxy to be used to upstreaming requests.
[options.tcpOutgoingAddress] Function The localAddress to use while sending requests
[options.auth] Function Activate Proxy-Authentication.

upstream & tcpOutgoingAddress Options

The options are functions having follow parameters:

Param Type Description
data Buffer The received data.
bridgedConnection Object Object containing info/data about Tunnel
bridgedConnectionId String The id of connection IP:PORT.
  • upstream-Function need to return a String with format -> IP:PORT of used http-proxy. If 'localhost' is returned, then the host-self will be used as proxy.
  • tcpOutgoingAddress-Function need to return a String with format -> IP.

These functions will be executed before first tcp-socket-connection is established.

Upstream to other proxies

If you don't want to use the host of active instance self, then you need to upstream connections to another http-proxy. This can be done with upstream attribute.

const ProxyServer = require('transparent-proxy');

const server = new ProxyServer({
    upstream: function () {
          return 'x.x.x.x:3128'; // upstream to other proxy
    }
});

//starting server on port 8080
server.listen(8080, '0.0.0.0', function () {
    console.log('TCP-Proxy-Server started!', server.address());
});

auth option

This activate basic authorization mechanism. The auth-function will be emit while handling Proxy-Authentications.

Param Type Description
username String The client username.
password String The client password

Note: It must return true or false.

const ProxyServer = require('transparent-proxy');

const server = new ProxyServer({
    auth: function (username, password) {
        return username === 'bar' && password === 'foo';
    }
});

//starting server on port 8080
server.listen(8080, '0.0.0.0', function () {
    console.log('TCP-Proxy-Server started!', server.address());
});

.getBridgedConnections()

const ProxyServer = require('transparent-proxy');
const server = new ProxyServer();

//starting server on port 8080
server.listen(8080, '0.0.0.0', function () {
    console.log('Proxy-Server started!', server.address());
});

setInterval(function showOpenSockets() {
    const bridgedConnections = server.getBridgedConnections();
    console.log([new Date()], 'OPEN =>', Object.keys(bridgedConnections).length)
}, 2000);

Examples

This example upstreams only requests for ifconfig.me to another proxy, for all other requests will be used localhost.

const ProxyServer = require('transparent-proxy');

const server = new ProxyServer({
    upstream: function (data, bridgedConnection, bridgedConnectionId) {
        if (~(data.toString().indexOf('ifconfig.me'))) {
            return 'x.x.x.x:3128'; // upstream to other proxy
        } else {
            return 'localhost'; //upstream to localhost
        }
    },
});

//starting server on port 8080
server.listen(8080, '0.0.0.0', function () {
    console.log('TCP-Proxy-Server started!', server.address());
});

Testing with curl:

curl -x 127.0.0.1:8080 https://ifconfig.me
x.x.x.x
curl -x 127.0.0.1:8080 https://ifconfig.co
y.y.y.y