nodejs 几行代码实现tcp端口转发
有时候需要进行一些端口转发,安装第三的软件 耗时耗力
直接用短小精悍的nodejs写一个 最简单
javascript
var net = require('net');
//目标host
var target = '192.168.1.180';
//目标端口
var port = 80;
net.createServer(socket => {
let client = new net.Socket();
client.connect({
port: port,
host: target
});
client.on('connect', () => {
client.pipe(socket);
socket.pipe(client);
});
}).listen(3307);
console.log(`listen 3307`)
var net = require('net');
//目标host
var target = '192.168.1.180';
//目标端口
var port = 80;
net.createServer(socket => {
let client = new net.Socket();
client.connect({
port: port,
host: target
});
client.on('connect', () => {
client.pipe(socket);
socket.pipe(client);
});
}).listen(3307);
console.log(`listen 3307`)
将上面代码保存为forward.js
然后运行:
shell
node forward.js
node forward.js
记得把防火墙的端口打开,不然不能访问