Started work on UDP tunneling.

Ylian Saint-Hilaire committed May 4, 2019 at 22:53 UTC 348617ae6d8432436ef63ec77281eb969948dcee
1 file changed +26 -2
agents/meshcore.js
+26 -2
@@ -418,6 +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));
422 var tunnel = http.request(woptions);
423 tunnel.upgrade = onTunnelUpgrade;
424 tunnel.on('error', function (e) { sendConsoleText('ERROR: ' + JSON.stringify(e)); });
@@ -430,6 +431,8 @@ function createMeshCore(agent) {
431 tunnel.protocol = 0;
432 tunnel.tcpaddr = data.tcpaddr;
433 tunnel.tcpport = data.tcpport;
434 + tunnel.udpaddr = data.udpaddr;
435 + tunnel.udpport = data.udpport;
436 tunnel.end();
437 // Put the tunnel in the tunnels list
438 var index = nextTunnelIndex++;
@@ -616,12 +619,33 @@ function createMeshCore(agent) {
619 if (this.tcpaddr != null) { connectionOptions.host = this.tcpaddr; } else { connectionOptions.host = '127.0.0.1'; }
620 s.tcprelay = net.createConnection(connectionOptions, onTcpRelayTargetTunnelConnect);
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 + s.data = onUdpRelayServerTunnelData;
625 + s.udprelay = dgram.createSocket({ type: 'udp4' });
626 + s.udprelay.bind({ port: 0 });
627 + s.udprelay.peerindex = this.index;
628 + s.udprelay.on('message', onUdpRelayTargetTunnelConnect);
629 } else {
630 // This is a normal connect for KVM/Terminal/Files
631 s.data = onTunnelData;
632 }
633 }
624 -
634 +
635 + // Called when UDP relay data is received // TODO****
636 + function onUdpRelayTargetTunnelConnect(data) {
637 + sendConsoleText('UDP1: ', data);
638 + var peerTunnel = tunnels[this.peerindex];
639 + peerTunnel.send(data);
640 + }
641 +
642 + // Called when we get data from the server for a TCP relay (We have to skip the first received 'c' and pipe the rest)
643 + 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
647 + }
648 +
649 // Called when the TCP relay target is connected
650 function onTcpRelayTargetTunnelConnect() {
651 var peerTunnel = tunnels[this.peerindex];
@@ -634,7 +658,7 @@ function createMeshCore(agent) {
658 function onTcpRelayServerTunnelData(data) {
659 if (this.first == true) { this.first = false; this.pipe(this.tcprelay); } // Pipe Server --> Target
660 }
637 -
661 +
662 function onTunnelClosed() {
663 if (tunnels[this.httprequest.index] == null) return; // Stop duplicate calls.
664 //sendConsoleText("Tunnel #" + this.httprequest.index + " closed.", this.httprequest.sessionid);