1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-22 19:23:31 +00:00

Many improvements.

This commit is contained in:
Ylian Saint-Hilaire
2018-02-11 21:11:58 -08:00
parent 508646044e
commit 4b5c77b4fd
127 changed files with 51725 additions and 35030 deletions

View File

@@ -1,3 +1,20 @@
console.enableWebLog(9595);
var mesh = null;
var kvmStream = null;
var Readable = require('stream').Readable;
var RS = new Readable({
read: function (options)
{
var min = 62000;
var max = 65535;
var size = Math.floor(Math.random() * (max - min)) + min;
var retVal = Buffer.alloc(size).fill(this.val++);
retVal.writeUInt32BE(size, 0);
if (this.val == 10) { this.val = 0; }
return (retVal);
}
});
RS.val = 0;
var http = require('http');
var rtc = require('ILibWebRTC');
@@ -6,12 +23,30 @@ var signalingChannel;
var dc;
var webServer = http.createServer(OnLocalWebRequest);
var processMgr = require('ILibProcessPipe');
var childprocess = require('child_process');
var p;
webServer.on('upgrade', OnUpgrade);
webServer.listen(8585);
//p = processMgr.CreateProcess("c:\\windows\\system32\\cmd.exe", "/c", "start", "http://localhost:8585/start.html");
if (process.platform == 'win32') {
p = childprocess.execFile("c:\\windows\\system32\\cmd.exe", ["/c", "start", "http://localhost:8585/start.html"]);
}
else {
console.log('Manually point your browser to http://localhost:8585/start.html');
}
try
{
mesh = require('MeshAgent');
}
catch(e)
{
}
function OnUpgrade(imsg, sck, head)
{
@@ -20,20 +55,102 @@ function OnUpgrade(imsg, sck, head)
signalingChannel.on('data', OnSignalData);
peerConnection = rtc.createConnection();
peerConnection.on('disconnected', function () { console.log('SCTP was disconnected'); });
peerConnection.on('connected', OnWebRTC_Connected);
peerConnection.on('dataChannel', OnWebRTC_DataChannel);
//console.log("Generating WebRTC Offer...");
//signalingChannel.write({ cmd: "offer", data: peerConnection.generateOffer() });
try
{
// peerConnection.on('_hold', function (val) { console.log('Holding Count: ' + val);});
peerConnection.on('_congestionWindowSizeChange', function (val) { console.log('Congestion Window: ' + val); });
// peerConnection.on('_receiverCredits', function (val) { console.log('Receiver Credits: ' + val); });
peerConnection.on('_t3tx', function (val) { console.log('T3TX: ' + val); });
//peerConnection.on('_fastRecovery', function (val) { console.log('Fast Recovery: ' + val);});
//peerConnection.on('_rttCalculated', function (val) { console.log('Calculated RTT Value = ' + val); });
peerConnection.on('_lastSackTime', function (val) { console.log('Last SACK Time = ' + val); });
peerConnection.on('_retransmit', function (val) { console.log('Retransmit: ' + (new Uint32Array([val]))[0]); });
//peerConnection.on('_retransmitPacket', function (val) { DebugPacket(val); console.log('RetransmitPacket: ' + val.toString('hex')); });
//peerConnection.on('_lastSentTime', function (val) { console.log('Last Sent Time = ' + val); });
peerConnection.on('_sackReceived', function (val) { console.log('SACK: ' + (new Uint32Array([val]))[0]); });
}
catch(e)
{
}
console.log("Generating WebRTC Offer...");
signalingChannel.write({ cmd: "offer", data: peerConnection.generateOffer() });
}
function FOURBYTEBOUNDARY(a)
{
return ((a) + ((4 - ((a) % 4)) % 4));
}
function crc32c(crc, bytes)
{
var POLY = 0x82f63b78;
var n;
crc ^= 0xffffffff;
for (n = 0; n < bytes.length; n++) {
crc ^= bytes[n];
crc = crc & 1 ? (crc >>> 1) ^ POLY : crc >>> 1;
crc = crc & 1 ? (crc >>> 1) ^ POLY : crc >>> 1;
crc = crc & 1 ? (crc >>> 1) ^ POLY : crc >>> 1;
crc = crc & 1 ? (crc >>> 1) ^ POLY : crc >>> 1;
crc = crc & 1 ? (crc >>> 1) ^ POLY : crc >>> 1;
crc = crc & 1 ? (crc >>> 1) ^ POLY : crc >>> 1;
crc = crc & 1 ? (crc >>> 1) ^ POLY : crc >>> 1;
crc = crc & 1 ? (crc >>> 1) ^ POLY : crc >>> 1;
}
return crc ^ 0xffffffff;
}
function DebugPacket(val)
{
console.log('BufferLen: ' + val.length);
console.log('CRC32C: ' + val.readInt32LE(8))
val.writeUInt32BE(0, 8);
console.log('CRC32C/Calc: ' + crc32c(0, val));
console.log('VTAG: ' + val.readUInt32LE(4));
var ptr = 12;
while (ptr + 4 <= val.length)
{
var hdr = val.slice(ptr);
var chunkType = hdr[0];
var chunkFlags = hdr[1];
var chunkSize = hdr.readUInt16BE(2);
switch (chunkType) {
case 0:
console.log('DATA Chunk');
console.log('...chunkFlags: ' + chunkFlags);
console.log('...chunkLength: ' + chunkSize);
console.log('...TSN: ' + hdr.readUInt32BE(4));
console.log('...StreamID: ' + hdr.readUInt16BE(8));
console.log('...Seq: ' + hdr.readUInt16BE(10));
console.log('...ProtocolID: ' + hdr.readUInt32BE(12));
break;
default:
console.log('UNKNOWN Chunk');
console.log('...chunkFlags: ' + chunkFlags);
console.log('...chunkLength: ' + chunkSize);
break;
}
ptr += FOURBYTEBOUNDARY(chunkSize);
}
}
function OnSignalData(chunk)
{
var j = JSON.parse(chunk);
if (j.cmd == 'offer')
{
console.log("Received Offer");
signalingChannel.write({ cmd: "offer", data: peerConnection.setOffer(j.data) });
//signalingChannel.write({ cmd: "offer", data: peerConnection.setOffer(j.data) });
peerConnection.setOffer(j.data);
}
}
function OnLocalWebRequest(request, response)
@@ -66,24 +183,30 @@ function OnLocalWebRequest(request, response)
function OnWebRTC_Connected()
{
console.log("WebRTC Session Established");
//this.dc = this.createDataChannel("testChannel", OnTestChannel);
//if(mesh != null)
//{
// // Let create a data channel
// this.dc = this.createDataChannel("remoteDesktop", OnKVMChannel)
// this.tempTimeout = setTimeout(function (dc) { console.log("sending: 'test'"); dc.write("test"); }, 10000, this.dc);
//}
this.jsdc = this.createDataChannel("testChannel", OnTestChannel);
}
function OnTestChannel()
{
console.log("Successfully established Data Channel");
console.log("Successfully established JavaScript Data Channel");
if (mesh == null) {
RS.pipe(this);
}
else {
kvmStream = mesh.getRemoteDesktopStream();
kvmStream.pipe(this);
}
}
function OnKVMChannel()
{
console.log("Successfully established Data Channel to test Data throughput");
dc = this;
dc.kvm = mesh.getRemoteDesktopStream();
dc.on('data', function (buffer) { console.log("Peer Received: " + buffer.toString() + " bytes"); });
dc.on('data', function (buffer)
{
console.log("Peer Received: " + buffer.toString() + " bytes");
});
dc.on('end', function () { this.kvm.end(); console.log("Closing KVM Session"); });
dc.kvm.pipe(dc);
}
@@ -91,8 +214,21 @@ function OnWebRTC_DataChannel(dataChannel)
{
console.log("Data Channel (" + dataChannel.name + ") was created");
dc = dataChannel;
dc.on('data', function (buffer) { console.log("Received: " + buffer.length + " bytes"); dc.write(buffer.length.toString()); });
dc.on('data', function (buffer)
{
//console.log("Received: " + buffer);
});
dc.on('end', function () { console.log("Data Channel: " + this.name + " was closed"); });
//if (mesh == null)
//{
// RS.pipe(dc);
//}
//else
//{
// kvmStream = mesh.getRemoteDesktopStream();
// kvmStream.pipe(dc);
//}
}
function OnTunnelData(buffer)
{