Improved UDP tunneling, but does not work yet.

This commit is contained in:
Ylian Saint-Hilaire 2019-05-06 18:44:23 -07:00
parent 348617ae6d
commit 187a2e92c0
2 changed files with 25 additions and 11 deletions

View File

@ -418,7 +418,7 @@ function createMeshCore(agent) {
var woptions = http.parseUri(xurl); var woptions = http.parseUri(xurl);
woptions.rejectUnauthorized = 0; woptions.rejectUnauthorized = 0;
//sendConsoleText(JSON.stringify(woptions)); //sendConsoleText(JSON.stringify(woptions));
sendConsoleText('TUNNELX: ', JSON.stringify(data)); sendConsoleText('TUNNEL: ' + JSON.stringify(data));
var tunnel = http.request(woptions); var tunnel = http.request(woptions);
tunnel.upgrade = onTunnelUpgrade; tunnel.upgrade = onTunnelUpgrade;
tunnel.on('error', function (e) { sendConsoleText('ERROR: ' + JSON.stringify(e)); }); tunnel.on('error', function (e) { sendConsoleText('ERROR: ' + JSON.stringify(e)); });
@ -609,7 +609,7 @@ function createMeshCore(agent) {
s.end = onTunnelClosed; s.end = onTunnelClosed;
s.tunnel = this; s.tunnel = this;
//sendConsoleText('onTunnelUpgrade'); //sendConsoleText('onTunnelUpgrade - ' + this.tcpport + ' - ' + this.udpport);
if (this.tcpport != null) { if (this.tcpport != null) {
// This is a TCP relay connection, pause now and try to connect to the target. // This is a TCP relay connection, pause now and try to connect to the target.
@ -621,11 +621,15 @@ function createMeshCore(agent) {
s.tcprelay.peerindex = this.index; s.tcprelay.peerindex = this.index;
} if (this.udpport != null) { } if (this.udpport != null) {
// This is a UDP relay connection, get the UDP socket setup. // TODO: *************** // This is a UDP relay connection, get the UDP socket setup. // TODO: ***************
sendConsoleText('UDP relay connection');
s.data = onUdpRelayServerTunnelData; s.data = onUdpRelayServerTunnelData;
s.udprelay = dgram.createSocket({ type: 'udp4' }); s.udprelay = require('dgram').createSocket({ type: 'udp4' });
s.udprelay.bind({ port: 0 }); s.udprelay.bind({ port: 0 });
s.udprelay.peerindex = this.index; s.udprelay.peerindex = this.index;
s.udprelay.on('message', onUdpRelayTargetTunnelConnect); s.udprelay.on('message', onUdpRelayTargetTunnelConnect);
s.udprelay.udpport = this.udpport;
s.udprelay.udpaddr = this.udpaddr;
s.udprelay.first = true;
} else { } else {
// This is a normal connect for KVM/Terminal/Files // This is a normal connect for KVM/Terminal/Files
s.data = onTunnelData; s.data = onTunnelData;
@ -634,16 +638,19 @@ function createMeshCore(agent) {
// Called when UDP relay data is received // TODO**** // Called when UDP relay data is received // TODO****
function onUdpRelayTargetTunnelConnect(data) { function onUdpRelayTargetTunnelConnect(data) {
sendConsoleText('UDP1: ', data); // TODO!!!
sendConsoleText('onUdpRelayTargetTunnelConnect: ' + data);
var peerTunnel = tunnels[this.peerindex]; var peerTunnel = tunnels[this.peerindex];
peerTunnel.send(data); peerTunnel.send(data);
} }
// Called when we get data from the server for a TCP relay (We have to skip the first received 'c' and pipe the rest) // Called when we get data from the server for a TCP relay (We have to skip the first received 'c' and pipe the rest)
function onUdpRelayServerTunnelData(data) { function onUdpRelayServerTunnelData(data) {
sendConsoleText('UDP2: ', data, this.udpport, this.udpaddr); if (this.udprelay.first === true) {
this.udprelay.send(data, this.udpport, this.udpaddr ? this.udpaddr : '127.0.0.1'); delete this.udprelay.first; // Skip the first 'c' that is received.
//if (this.first == true) { this.first = false; this.pipe(this.tcprelay); } // Pipe Server --> Target } else {
this.udprelay.send(data, parseInt(this.udprelay.udpport), this.udprelay.udpaddr ? this.udprelay.udpaddr : '127.0.0.1');
}
} }
// Called when the TCP relay target is connected // Called when the TCP relay target is connected

View File

@ -274,7 +274,7 @@ module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie
performRelay(); performRelay();
}); });
return obj; return obj;
} else if ((req.query.nodeid != null) && (req.query.tcpport != null)) { } else if ((req.query.nodeid != null) && ((req.query.tcpport != null) || (req.query.udpport != null))) {
// We have routing instructions in the URL arguments, but first, check user access for this node. // We have routing instructions in the URL arguments, but first, check user access for this node.
parent.db.Get(req.query.nodeid, function (err, docs) { parent.db.Get(req.query.nodeid, function (err, docs) {
if (docs.length == 0) { console.log('ERR: Node not found'); try { obj.close(); } catch (e) { } return; } // Disconnect websocket if (docs.length == 0) { console.log('ERR: Node not found'); try { obj.close(); } catch (e) { } return; } // Disconnect websocket
@ -286,9 +286,16 @@ module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie
// Send connection request to agent // Send connection request to agent
if (obj.id == null) { obj.id = ('' + Math.random()).substring(2); } // If there is no connection id, generate one. if (obj.id == null) { obj.id = ('' + Math.random()).substring(2); } // If there is no connection id, generate one.
if (req.query.tcpport != null) {
var command = { nodeid: req.query.nodeid, action: 'msg', type: 'tunnel', value: '*/meshrelay.ashx?id=' + obj.id, tcpport: req.query.tcpport, tcpaddr: ((req.query.tcpaddr == null) ? '127.0.0.1' : req.query.tcpaddr) }; var command = { nodeid: req.query.nodeid, action: 'msg', type: 'tunnel', value: '*/meshrelay.ashx?id=' + obj.id, tcpport: req.query.tcpport, tcpaddr: ((req.query.tcpaddr == null) ? '127.0.0.1' : req.query.tcpaddr) };
parent.parent.debug(1, 'Relay: Sending agent tunnel command: ' + JSON.stringify(command)); parent.parent.debug(1, 'Relay: Sending agent TCP tunnel command: ' + JSON.stringify(command));
if (obj.sendAgentMessage(command, user._id, domain.id) == false) { delete obj.id; parent.parent.debug(1, 'Relay: Unable to contact this agent (' + cleanRemoteAddr(ws._socket.remoteAddress) + ')'); } if (obj.sendAgentMessage(command, user._id, domain.id) == false) { delete obj.id; parent.parent.debug(1, 'Relay: Unable to contact this agent (' + cleanRemoteAddr(ws._socket.remoteAddress) + ')'); }
} else if (req.query.udpport != null) {
var command = { nodeid: req.query.nodeid, action: 'msg', type: 'tunnel', value: '*/meshrelay.ashx?id=' + obj.id, udpport: req.query.udpport, udpaddr: ((req.query.udpaddr == null) ? '127.0.0.1' : req.query.udpaddr) };
parent.parent.debug(1, 'Relay: Sending agent UDP tunnel command: ' + JSON.stringify(command));
if (obj.sendAgentMessage(command, user._id, domain.id) == false) { delete obj.id; parent.parent.debug(1, 'Relay: Unable to contact this agent (' + cleanRemoteAddr(ws._socket.remoteAddress) + ')'); }
}
performRelay(); performRelay();
}); });
return obj; return obj;