Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
141
rated 0 times [  147] [ 6]  / answers: 1 / hits: 29311  / 13 Years ago, mon, june 27, 2011, 12:00:00

I'm trying to forward all traffic from port 6999 to port 7000 (I know I could use iptables, but the idea is to use Node.js to do some packet inspection).



Here is the code I have sofar:



var net=require('net');
var compress=require('./node-compress/compress');

var ip='172.16.1.224';
var ipPort=6999;
var opPort=7000;

var output=net.createServer(function(connOut){
var input=net.createServer(function(connIn){
connIn.pipe(connOut);
});
input.listen(ipPort,ip);
});
output.listen(opPort,ip);


It just does not seem to work. When I do a tcpdump on port 7000, nothing shows up. Anyone have any suggestions?



Many thanks in advance,


More From » node.js

 Answers
7

Here's my go at it:



Supports giving the from and to from command line, and supports remote machines.



var net = require('net');

// parse 80 and localhost:80 or even 42mEANINg-life.com:80
var addrRegex = /^(([a-zA-Z-.0-9]+):)?(d+)$/;

var addr = {
from: addrRegex.exec(process.argv[2]),
to: addrRegex.exec(process.argv[3])
};

if (!addr.from || !addr.to) {
console.log('Usage: <from> <to>');
return;
}

net.createServer(function(from) {
var to = net.createConnection({
host: addr.to[2],
port: addr.to[3]
});
from.pipe(to);
to.pipe(from);
}).listen(addr.from[3], addr.from[2]);


(save as proxy.js)



To forward from localhost:9001 => localhost:80



$ node proxy.js 9001 80


Or localhost:9001 => otherhost:80



$ node proxy.js 9001 otherhost:80


(This was based on Andrey's answer, thanks!)


[#91486] Friday, June 24, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
berenices

Total Points: 104
Total Questions: 106
Total Answers: 106

Location: Spain
Member since Thu, Dec 23, 2021
3 Years ago
berenices questions
;