proxied-socket
v1.2.2
Published
proxied-socket ---
Downloads
5
Readme
proxied-socket
Proxies are commonly used to keep undisclosed the Server original IP address to the clients. Normal TCP level proxies have a big issue, they do even mask the Client IP address to the Server that just sees the Proxy IP address.
proxied-socket is meant to solve this issue. To do so, proxied-socket provides unobtrusive IP address forwarding support for Node.js sockets.
It is composed of two components the Client and the Server.
Client
The Client side API allows to add an header that contains an IP address. Proxies can use it to forward the remote Client IP address.
var ps = require('proxied-socket'),
net = require('net');
var remote = {port: 1234};
var server = net.wrapServer(function (source) {
var target = net.connect(options, forwarded);
ps.sendHeader(source, target); // you just need this
source.pipe(target).pipe(source);
});
server.listen(80);
Server
The Server side API allows to intercept the header present at the beginning of every connection and to attach the IP address present in it to relative socket.
The are two methods:
- attach which attaches the original socket information as the properties originalRemoteFamily, originalRemoteAddress, originalRemotePort, originalLocalAddress and originalLocalPort
- override (default) which moves the original socket information remoteFamily, remoteAddress, remotePort, localAddress and remotePorts to the respective property maskedRemoteFamily, maskedRemoteAddress, ... while replacing them with the information obtained from the header
And two supported formats:
- default is compatible with the
proxied-socket.Client
. If you are writing both the proxy and server, this is the format to use. It is also the default format. - haproxy and haproxy-v2 are compatible with the HAProxy PROXY protocol V1 and v2.If you are writing a server that will be load balanced by HAProxy, this is the format you will want. More information about the PROXY protcol can be found at the following link. https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
In both cases the full parsed header is attached to the proxyHeader property of the socket.
var ps = require('proxied-socket'),
net = require('net');
var server = ps.wrapServer(net.Server(function (client) { // you just need to wrap your server
console.log(client.remoteAddress);
}),
{
method: 'override'
format: 'default',
});
server.listen(1234);
Without proxied-socket the server would always log the same address. With proxied-socket the server instead logs the real address of the remote client.