1.5.4 • Published 18 days ago

transparent-proxy v1.5.4

Weekly downloads
20
License
ISC
Repository
-
Last release
18 days ago

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...

It supports Basic Proxy-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

ParamTypeDescription
optionsObjectThe options object.
options.authFunctionActivate/Handle Proxy-Authentication. Returns or solves to Boolean.
options.upstreamFunctionThe proxy to be used to upstreaming requests. Returns String.
options.tcpOutgoingAddressFunctionThe localAddress to use while sending requests. Returns String
options.verboseBooleanActivate verbose mode.

upstream & tcpOutgoingAddress Options

The options are functions having follow parameters:

ParamTypeDescription
dataBufferThe received data.
bridgedConnectionObjectObject containing info/data about Tunnel
  • upstream-Function need to return a String with format -> IP:PORT or USER:PWD@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.

Note: 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());
});

The auth Function

This activate basic authorization mechanism. The Auth-function will be executed while handling Proxy-Authentications.

ParamTypeDescription
usernameStringThe client username.
passwordStringThe client password
bridgedConnectionObjectObject containing info/data about Tunnel

Note: It needs to return True/False or a Promise that resolves to boolean (isAuthenticated).

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) {
        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

For more examples look here.

1.5.4

18 days ago

1.5.3

24 days ago

1.4.6

24 days ago

1.4.5

25 days ago

1.5.2

24 days ago

1.5.1

24 days ago

1.5.0

24 days ago

1.4.4

27 days ago

1.4.3

28 days ago

1.4.2

28 days ago

1.4.1

28 days ago

1.4.0

28 days ago

1.3.2

1 month ago

1.3.1

1 month ago

1.2.1

1 month ago

1.2.0

1 month ago

1.1.4

1 month ago

1.1.3

1 month ago

1.1.2

1 month ago

1.1.1

1 month ago

1.0.2

9 months ago

1.1.0

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago