Node.js中的TCP打孔

时间:2014-02-10 16:16:23

标签: node.js tcp ip hole-punching

我正在尝试通过node.js中的两个NAT打一个TCP漏洞。问题是我无法弄清楚如何选择连接应该使用哪个本地端口?

2 个答案:

答案 0 :(得分:1)

为套接字分配了一个本地端口。要重用相同的端口,您可以使用与服务器通信所使用的相同套接字连接到客户端。这对你有用,因为你正在进行TCP打孔。但是,您无法自己选择端口。

以下是一些测试代码:

// server.js
require('net').createServer(function(c) {
    c.write(c.remotePort.toString(10));
}).listen(4434);


//client.js
var c = require('net').createConnection({host : '127.0.0.1', port : 4434});
c.on('data', function(data) {
    console.log(data.toString(), c.localPort);
    c.end();
});

c.on('end', function() {
    c.connect({host : '127.0.0.1', port : 4434});
});

答案 1 :(得分:1)

在与公共服务器建立连接后,您还需要侦听用于建立该连接的完全相同的本地(!!)端口。

我将你的测试代码扩展到一个完整的tcp漏洞,打破概念验证:

// server.js
var server = require('net').createServer(function (socket) {
    console.log('> Connect to this public endpoint with clientB:', socket.remoteAddress + ':' + socket.remotePort);
}).listen(4434, function (err) {
    if(err) return console.log(err);
    console.log('> (server) listening on:', server.address().address + ':' + server.address().port)
});

// clientA.js
var c = require('net').createConnection({host : 'PUBLIC_IP_OF_SERVER', port : 4434}, function () {
    console.log('> connected to public server via local endpoint:', c.localAddress + ':' + c.localPort);

    // do not end the connection, keep it open to the public server
    // and start a tcp server listening on the ip/port used to connected to server.js
    var server = require('net').createServer(function (socket) {
        console.log('> (clientA) someone connected, it\s:', socket.remoteAddress, socket.remotePort);
        socket.write("Hello there NAT traversal man, this is a message from a client behind a NAT!");
    }).listen(c.localPort, c.localAddress, function (err) {
        if(err) return console.log(err);
        console.log('> (clientA) listening on:', c.localAddress + ':' + c.localPort);
    });
});

// clientB.js
// read the server's output to find the public endpoint of A:
var c = require('net').createConnection({host : 'PUBLIC_IP_OF_CLIENT_A', port : PUBLIC_PORT_OF_CLIENT_A},function () {
    console.log('> (clientB) connected to clientA!');

    c.on('data', function (data) {
        console.log(data.toString());
    });
});

有关在服务器上发生信号的更完整版本,请参阅我的代码:https://github.com/SamDecrock/node-tcp-hole-punching

相关问题