在Node.js中进行TCP打洞

7
我正在尝试在node.js中穿过两个NAT打通TCP隧道。问题是我无法确定连接应该使用哪个本地端口?

找到了一个适用于这个特定问题的解决方法。发布答案。 - Friend of Kim
2个回答

5

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

我将您的测试代码扩展为完整的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


3

套接字被分配了本地端口。为了重用相同的端口,您可以使用与与服务器通信时使用的套接字相同的套接字连接到客户端。这对您有好处,因为您正在进行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});
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接