udp-proxy
UDP-proxy for node.js version >= 0.10.x (for eariler node versions, use version 0.2.1)
Supports both IPv6 and IPv4, and bridging in between (see example below).
Installation
npm install udp-proxy
udp-proxy has no dependencies beyond node.js itself
Usage
Example:
// Let's create a DNS-proxy that proxies IPv4 udp-requests to googles IPv6 DNS-servervar proxy = options = address: '2001:4860:4860::8888' port: 53 ipv6: true localaddress: '0.0.0.0' localport: 53535 localipv6: false proxyaddress: '::0' timeOutTime: 10000 ; // This is the function that creates the server, each connection is handled internallyvar server = proxy; // this should be obviousserver; // 'bound' means the connection to server has been made and the proxying is in actionserver; // 'message' is emitted when the server gets a messageserver; // 'proxyMsg' is emitted when the bound socket gets a message and it's send back to the peer the socket was bound toserver; // 'proxyClose' is emitted when the socket closes (from a timeout) without new messagesserver; server; server;
Methods
var proxy = require('udp-proxy');
- requires the proxy-module
var server = proxy.createServer( options );
- .createServer( options ) creates an instance of udp-proxy with the given options
- options must be an object consisting of:
address
: string (the address you want to proxy to)- default: 'localhost'
port
: number (the port you want to proxy to)- default: 41234
ipv6
: boolean (if the target uses IPv6)- default: false
localaddress
: string (the interface-addresses to use for the server)- default: '0.0.0.0' ( ::0 if
localipv6
is set to true)
- default: '0.0.0.0' ( ::0 if
localport
: number (the port for the server to listen on)- default: 0 (random)
localipv6
: boolean (if you want the server to use IPv6)- default: false
proxyaddress
: string (if you want to set on which interface the proxy connects out)- default: 0.0.0.0 ( ::0 if
ipv6
is set to true)
- default: 0.0.0.0 ( ::0 if
timeOutTime
: number the time it takes for socket to time out (in ms)- default: 10000 (10s)
timeOutTime
: number the time it takes for socket to time out (in ms)- default: 10000 (10s)
middleware
: object apply a middleware to the proxy, see Middleware section below.- default: none
- options must be an object consisting of:
the proxy always connects outwards with a random port
server.close(callback) closes proxy server.
Events
server.on( 'event'
, function ( args ) { });
'listening'
, details- details is an object with two objects:
- target address
- server address
- details is an object with two objects:
'bound'
, details- details is an object with two objects:
- route address
- peer address
- details is an object with two objects:
'message'
, message, sender- message is the payload from user using the proxy
- sender is the user address
'proxyMsg'
, message, sender, peer- message is the answer to the message from the user
- sender is the answerer address
- peer is the requesting address
'error'
, err- in case of an error err has the error-messages
'proxyError'
, err- if the message could not be proxied err has the error-messages
'proxyClose'
, peer- when a socket is closed after no new messages in set timeout
- peer is the address of the disconnected client
'close'
- self-explanatory
address object contains:
address
: string ip-addressfamily
: string IPv6 or IPv4port
: number udp-port
Middleware
Add a middleware object to the proxy to intercept any incoming or outgoing message. Use this if you need to potentially change the message content before it is relayed, or prevent it from sending altogether.
The middleware
object must contain the following functions:
message( msg
, sender
, function next
( msg, sender ) { });
- will be invoked with every message from a peer
sender
to the server. - proxy will only relay the message when
next
is invoked.
proxyMsg( msg
, sender
, peer
, function next
( msg, sender, peer ) { });
- will be invoked with every message from the server
sender
to apeer
. - proxy will only relay the message when
next
is invoked.
Example:
The following example will block any message going from the client to the server that has length > 120.
// Following the first example, let's create a DNS-proxy that proxies IPv4 udp-requests to googles IPv6 DNS-server and provide a middleware.var proxy = options = address: '2001:4860:4860::8888' port: 53 ipv6: true localaddress: '0.0.0.0' localport: 53535 localipv6: false proxyaddress: '::0' timeOutTime: 10000 middleware: { // messages with longer length will not be relayed, because 'next' will not be invoked. if msglength <= 120 ; } { ; } ; var server = proxy; // ..
Tests
Run node testIPv4
or node testIPv6
to run the tests.
License
MIT