Improved UDP tunneling, but does not work yet.
Ylian Saint-Hilaire committed
May 6, 2019 at 18:44 UTC
187a2e92c0acbc5a6f12b75045933003a1f39236
2 files changed
+25
-11
agents/meshcore.js
+14
-7
@@ -418,7 +418,7 @@ function createMeshCore(agent) {
418
var woptions = http.parseUri(xurl);
419
woptions.rejectUnauthorized = 0;
420
//sendConsoleText(JSON.stringify(woptions));
421
- sendConsoleText('TUNNELX: ', JSON.stringify(data));
421
+ sendConsoleText('TUNNEL: ' + JSON.stringify(data));
422
var tunnel = http.request(woptions);
423
tunnel.upgrade = onTunnelUpgrade;
424
tunnel.on('error', function (e) { sendConsoleText('ERROR: ' + JSON.stringify(e)); });
@@ -609,7 +609,7 @@ function createMeshCore(agent) {
609
s.end = onTunnelClosed;
610
s.tunnel = this;
611
612
- //sendConsoleText('onTunnelUpgrade');
612
+ //sendConsoleText('onTunnelUpgrade - ' + this.tcpport + ' - ' + this.udpport);
613
614
if (this.tcpport != null) {
615
// This is a TCP relay connection, pause now and try to connect to the target.
@@ -621,11 +621,15 @@ function createMeshCore(agent) {
621
s.tcprelay.peerindex = this.index;
622
} if (this.udpport != null) {
623
// This is a UDP relay connection, get the UDP socket setup. // TODO: ***************
624
+ sendConsoleText('UDP relay connection');
625
s.data = onUdpRelayServerTunnelData;
625
- s.udprelay = dgram.createSocket({ type: 'udp4' });
626
+ s.udprelay = require('dgram').createSocket({ type: 'udp4' });
627
s.udprelay.bind({ port: 0 });
628
s.udprelay.peerindex = this.index;
629
s.udprelay.on('message', onUdpRelayTargetTunnelConnect);
630
+ s.udprelay.udpport = this.udpport;
631
+ s.udprelay.udpaddr = this.udpaddr;
632
+ s.udprelay.first = true;
633
} else {
634
// This is a normal connect for KVM/Terminal/Files
635
s.data = onTunnelData;
@@ -634,16 +638,19 @@ function createMeshCore(agent) {
638
639
// Called when UDP relay data is received // TODO****
640
function onUdpRelayTargetTunnelConnect(data) {
637
- sendConsoleText('UDP1: ', data);
641
+ // TODO!!!
642
+ sendConsoleText('onUdpRelayTargetTunnelConnect: ' + data);
643
var peerTunnel = tunnels[this.peerindex];
644
peerTunnel.send(data);
645
}
646
647
// Called when we get data from the server for a TCP relay (We have to skip the first received 'c' and pipe the rest)
648
function onUdpRelayServerTunnelData(data) {
644
- sendConsoleText('UDP2: ', data, this.udpport, this.udpaddr);
645
- this.udprelay.send(data, this.udpport, this.udpaddr ? this.udpaddr : '127.0.0.1');
646
- //if (this.first == true) { this.first = false; this.pipe(this.tcprelay); } // Pipe Server --> Target
649
+ if (this.udprelay.first === true) {
650
+ delete this.udprelay.first; // Skip the first 'c' that is received.
651
+ } else {
652
+ this.udprelay.send(data, parseInt(this.udprelay.udpport), this.udprelay.udpaddr ? this.udprelay.udpaddr : '127.0.0.1');
653
+ }
654
}
655
656
// Called when the TCP relay target is connected
meshrelay.js
+11
-4
@@ -274,7 +274,7 @@ module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie
274
performRelay();
275
});
276
return obj;
277
- } else if ((req.query.nodeid != null) && (req.query.tcpport != null)) {
277
+ } else if ((req.query.nodeid != null) && ((req.query.tcpport != null) || (req.query.udpport != null))) {
278
// We have routing instructions in the URL arguments, but first, check user access for this node.
279
parent.db.Get(req.query.nodeid, function (err, docs) {
280
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
286
287
// Send connection request to agent
288
if (obj.id == null) { obj.id = ('' + Math.random()).substring(2); } // If there is no connection id, generate one.
289
- 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) };
290
- parent.parent.debug(1, 'Relay: Sending agent tunnel command: ' + JSON.stringify(command));
291
- 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) + ')'); }
289
+
290
+ if (req.query.tcpport != null) {
291
+ 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) };
292
+ parent.parent.debug(1, 'Relay: Sending agent TCP tunnel command: ' + JSON.stringify(command));
293
+ 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) + ')'); }
294
+ } else if (req.query.udpport != null) {
295
+ 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) };
296
+ parent.parent.debug(1, 'Relay: Sending agent UDP tunnel command: ' + JSON.stringify(command));
297
+ 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) + ')'); }
298
+ }
299
performRelay();
300
});
301
return obj;