1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-24 04:04:31 +00:00

MeshAgent for MeshCentral2 Beta2 with improved crypto.

This commit is contained in:
Ylian Saint-Hilaire
2017-10-25 21:08:41 -07:00
parent 0a7e84849d
commit 34e09c2304
53 changed files with 1778 additions and 551 deletions

View File

@@ -1,14 +1,39 @@
var http = require('http');
var agent = require('MeshAgent');
var server = "";
var req = "";
var gtunnel = "";
var digest = require('http-digest').create("bryan", "roe");
agent.on('Connected', function (connectState)
console.log("Starting Digest Test (Agent Connected)");
//server = http.createServer({ "MeshAgent": agent, "requestCert": true, "checkClientIdentity": onVerifyClient }, OnRequest);
server = http.createServer(OnRequest);
server.listen(9093);
server.on('upgrade', OnServerUpgrade);
function OnServerUpgrade(imsg, sck, head)
{
console.log("Connection State = " + connectState.toString());
console.log("Connected: " + this.ServerUrl);
if(imsg.Digest_IsAuthenticated('www.meshcentral.com')==1)
{
var uname = imsg.Digest_GetUsername();
console.log("Digest Username was: " + uname);
if(uname == 'bryan' && imsg.Digest_ValidatePassword('roe')==1)
{
sck.upgradeWebSocket();
}
else
{
console.log("Bad Username/Password");
sck.end();
}
}
else
{
console.log("Sending Unauthorized");
imsg.Digest_SendUnauthorized('www.meshcentral.com', 'oops');
}
}
//gtunnel = require('global-tunnel');
//gtunnel.initialize({ host: "proxy.jf.intel.com", port: 911 });
@@ -25,11 +50,12 @@ agent.on('Connected', function (connectState)
digest.http = require('http');
//digest.get("http://127.0.0.1:9093/", function (imsg) { console.log(imsg.statusCode == 200 ? "SUCCESS!" : "FAIL!"); });
//digest.request({ protocol: "http:", method: "GET", host: "127.0.0.1", path: "/", port: 9093 }, function (imsg) { console.log(imsg.statusCode == 200 ? "SUCCESS!" : "FAIL!"); }).end();
var req = digest.request({ MeshAgent: agent, protocol: "wss:", method: "GET", host: "127.0.0.1", path: "/", port: 9093, checkServerIdentity:onVerifyServer }, function (imsg) { console.log(imsg.statusCode == 200 ? "SUCCESS!" : "FAIL!"); });
var req = digest.request({ protocol: "ws:", method: "GET", host: "127.0.0.1", path: "/", port: 9093 }, function (imsg) { console.log(imsg.statusCode == 200 ? "SUCCESS!" : "FAIL!"); });
req.on('upgrade', function (res, sk, h) { console.log("Upgraded to WebSocket!"); });
req.on('error', function () { console.log("Error occured"); });
req.end();
});
function OnAlt(imsg)
{
@@ -39,17 +65,7 @@ function OnGoogle(imsg)
{
console.log("Response Code = " + imsg.statusCode);
}
agent.Ready = function()
{
console.log("Starting Digest Test (Agent Connected)");
//server = http.createServer({ "MeshAgent": agent, "requestCert": true, "checkClientIdentity": onVerifyClient }, OnRequest);
server = http.createServer({ "MeshAgent": agent }, OnRequest);
server.listen(9093);
//req = http.request({ "protocol":"ws:", "hostname": "127.0.0.1", "port": 9093, "method": "GET", "path": "/", "MeshAgent": agent }, OnResponse);
//req.upgrade = OnWebSocket;
//req.end();
}
function OnWebSocket(msg, s, head)
{

121
Debug/PostDigest.js Normal file
View File

@@ -0,0 +1,121 @@
var http = require('http');
var server = "";
var req = "";
var gtunnel = "";
var digest = require('http-digest').create("bryan", "roe");
console.log("Starting POST Digest Test");
server = http.createServer(OnRequest);
server.listen(9093);
server.on('upgrade', OnServerUpgrade);
server.on('checkContinue', OnCheckContinue);
//server.on('checkContinue', OnCheckContinue_NoDigest);
digest.http = require('http');
//var req = digest.request({ protocol: "ws:", method: "GET", host: "127.0.0.1", path: "/", port: 9093 }, function (imsg) { console.log(imsg.statusCode == 200 ? "SUCCESS!" : "FAIL!"); });
//var req = http.request({ protocol: "http:", method: "POST", host: "127.0.0.1", path: "/", port: 9093, headers: { Expect: '100-Continue' } }, function (imsg) { console.log(imsg.statusCode == 200 ? "SUCCESS!" : "FAIL!"); });
var req = digest.request({ protocol: "http:", method: "POST", host: "127.0.0.1", path: "/", port: 9093, headers: { Expect: '100-Continue' } }, function (imsg) { console.log(imsg.statusCode == 200 ? "SUCCESS!" : "FAIL!"); });
req.on('upgrade', function (res, sk, h) { console.log("Upgraded to WebSocket!"); });
req.on('error', function () { console.log("Error occured"); });
req.on('continue', function () { console.log("Received Continue"); this.write("test"); this.end(); });
function OnCheckContinue(imsg, resp)
{
console.log("Recevied: Expect-100 Continue");
if (imsg.Digest_IsAuthenticated('www.meshcentral.com') == 1)
{
var uname = imsg.Digest_GetUsername();
console.log("Digest Username was: " + uname);
if (uname == 'bryan' && imsg.Digest_ValidatePassword('roe') == 1)
{
console.log("Validated");
imsg.on('data', function (chunk) { console.log('Received: ' + chunk.toString()); });
imsg.on('end', function () { console.log('Received Complete'); });
resp.writeContinue();
}
else
{
console.log("Bad Username/Password");
resp.statusCode = "500";
resp.statusMessage = "Error";
resp.end();
}
}
else
{
console.log("Sending Unauthorized");
imsg.Digest_SendUnauthorized('www.meshcentral.com', 'oops');
}
}
function OnCheckContinue_NoDigest(imsg, resp)
{
console.log("Recevied: Expect-100 Continue");
imsg.on('data', function (chunk) { console.log('Received: ' + chunk.toString()); });
imsg.on('end', function () { console.log('Received Complete'); });
resp.writeContinue();
}
function OnServerUpgrade(imsg, sck, head)
{
if(imsg.Digest_IsAuthenticated('www.meshcentral.com')==1)
{
var uname = imsg.Digest_GetUsername();
console.log("Digest Username was: " + uname);
if(uname == 'bryan' && imsg.Digest_ValidatePassword('roe')==1)
{
console.log("Upgrading to WebSocket");
sck.upgradeWebSocket();
}
else
{
console.log("Bad Username/Password");
sck.end();
}
}
else
{
console.log("Sending Unauthorized");
imsg.Digest_SendUnauthorized('www.meshcentral.com', 'oops');
}
}
function onVerifyServer(clientName, certs) {
console.log("Server Name = " + clientName + "\n");
for (var i = 0; i < certs.length; ++i) {
console.log(" Fingerprint = " + certs[i].fingerprint + "\n");
}
//throw ("Not Valid");
}
function onVerifyClient(clientName, certs)
{
console.log("Client Name = " + clientName + "\n");
for (var i = 0; i < certs.length; ++i) {
console.log(" Fingerprint = " + certs[i].fingerprint + "\n");
}
//throw ("Not Valid");
}
function onVerify(serverName, certs)
{
console.log("ServerName = " + serverName + "\n");
for (var i = 0; i < certs.length;++i)
{
console.log(" Fingerprint = " + certs[i].fingerprint + "\n");
}
//throw ("Not Valid");
}
function OnRequest(req, res)
{
console.log("Received Request for: " + req.url);
}

120
Debug/WebRTC_Test2.html Normal file
View File

@@ -0,0 +1,120 @@
<html>
<head>
<title>WebRTC Test Application</title>
</head>
<body onload="start()">
<span id="statustext"></span>
<script type="text/javascript">
var configuration = { "iceServers": [] };
var connection = null;
var datachannel = null;
var currentanswer = null;
var wsocket = null;
var decoder = new TextDecoder('utf-8');
var sdp = null;
function start()
{
debug("Connecting signaling channel...");
wsocket = new WebSocket("ws://127.0.0.1:8585/control");
wsocket.binaryType = "arraybuffer";
wsocket.onopen = function (evt)
{
debug("Web Socket Connection established...");
startWebRTC();
}
wsocket.onmessage = function (evt)
{
//var cmd = JSON.parse(decoder.decode(new Uint8Array(evt.data)));
var cmd = JSON.parse(evt.data);
if (cmd.cmd == 'offer')
{
debug("Received WebRTC Offer...");
var ax = null;
if (typeof mozRTCSessionDescription !== 'undefined') { ax = new mozRTCSessionDescription({ type: "answer", sdp: cmd.data }) } else { ax = new RTCSessionDescription({ type: "answer", sdp: cmd.data }) }
connection.setRemoteDescription(ax, onSetRemoteDescriptionDone, onError);
}
}
}
function onSetRemoteDescriptionDone()
{
//connection.createAnswer(onAnswerDone, onError);
}
function onAnswerDone(answer)
{
//wsocket.send(JSON.stringify({ cmd: 'offer', data: answer.sdp }));
sdp = answer.sdp;
connection.setLocalDescription(answer, onSetLocalDescriptionDone, onError);
}
function startWebRTC()
{
debug("Initiating WebRTC...");
if (connection != null) { debug("Error!"); return; }
if (typeof mozRTCPeerConnection !== 'undefined') { connection = new mozRTCPeerConnection(configuration); }
else if (typeof RTCPeerConnection !== 'undefined') { connection = new RTCPeerConnection(configuration); }
else if (typeof webkitRTCPeerConnection !== 'undefined') { connection = new webkitRTCPeerConnection(configuration); }
else return false;
connection.ondatachannel = onDataChannel
connection.onicecandidate = onIceCandidate;
datachannel = connection.createDataChannel("browserDataChannel", {});
datachannel.onmessage = function (event) { debug("Remote: " + event.data); };
datachannel.onopen = function () { debug("browserDataChannel Connected."); };
datachannel.onclose = function (event) { debug("DataChannel was closed by remote"); }
connection.createOffer(onOfferDone, onError, { mandatory: { OfferToReceiveAudio: false, OfferToReceiveVideo: false } });
}
function onOfferDone(offer)
{
// debug(offer.sdp);
currentoffer = offer;
connection.setLocalDescription(offer, onSetLocalDescriptionDone, onError);
sdp = currentoffer.sdp;
}
function onDataChannel(event)
{
debug("Data Channel ("+ event.channel.label + ") connected");
datachannel = event.channel;
datachannel.binaryType = "arraybuffer";
datachannel.onmessage = function (msg)
{
try
{
datachannel.send(msg.data.byteLength.toString());
}
catch(e)
{
debug(e.toString());
debug(msg.data.toString());
}
};
}
function onIceCandidate(e)
{
if (e.candidate == null) {
if (sdp == null) { debug('error'); return; }
wsocket.send(JSON.stringify({ cmd: 'offer', data: sdp }));
}
else
{
sdp += ("a=" + e.candidate.candidate + "\r\n");
}
}
function onSetLocalDescriptionDone() { }
function onError(e) { if (e.message) debug(e.message); else debug(e); }
function debug(msg) { document.getElementById("statustext").innerHTML += (msg + "\r\n"); }
</script>
</body>
</html>

134
Debug/WebRTC_Test2.js Normal file
View File

@@ -0,0 +1,134 @@
var http = require('http');
var rtc = require('ILibWebRTC');
var peerConnection;
var signalingChannel;
var dc;
var webServer = http.createServer(OnLocalWebRequest);
var processMgr = require('ILibProcessPipe');
var p;
webServer.on('upgrade', OnUpgrade);
webServer.listen(8585);
//p = processMgr.CreateProcess("c:\\windows\\system32\\cmd.exe", "/c", "start", "http://localhost:8585/start.html");
function OnUpgrade(imsg, sck, head)
{
console.log("WebSocket Connected");
signalingChannel = sck.upgradeWebSocket();
signalingChannel.on('data', OnSignalData);
peerConnection = rtc.createConnection();
peerConnection.on('connected', OnWebRTC_Connected);
peerConnection.on('dataChannel', OnWebRTC_DataChannel);
//console.log("Generating WebRTC Offer...");
//signalingChannel.write({ cmd: "offer", data: peerConnection.generateOffer() });
}
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) });
}
}
function OnLocalWebRequest(request, response)
{
if(request.method == 'GET' && request.url == '/start.html')
{
var fs = require('fs');
try
{
var stream = fs.createReadStream('WebRTC_Test2.html');
response.statusCode = 200;
response.statusMessage = "OK";
stream.pipe(response);
}
catch(e)
{
response.statusCode = 404;
response.statusMessage = "Not Found";
response.end();
}
}
else
{
response.statusCode = 404;
response.statusMessage = "Not Found";
response.end();
}
}
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);
//}
}
function OnTestChannel()
{
console.log("Successfully established Data Channel");
}
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('end', function () { this.kvm.end(); console.log("Closing KVM Session"); });
dc.kvm.pipe(dc);
}
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('end', function () { console.log("Data Channel: " + this.name + " was closed"); });
}
function OnTunnelData(buffer)
{
if (buffer == 'c')
{
console.log("Tunnel Established");
peerConnection = rtc.createConnection();
peerConnection.on('connected', OnWebRTC_Connected);
peerConnection.on('dataChannel', OnWebRTC_DataChannel);
if(mesh!=null)
{
console.log("Generating WebRTC Offer...");
this.write({ cmd: "offer", data: peerConnection.generateOffer() });
}
}
else
{
ProcessCommand(JSON.parse(buffer.toString()));
}
}
function ProcessCommand(cmd)
{
console.log("Received Command: " + cmd.cmd);
if(cmd.cmd == 'offer')
{
console.log("setting offer...");
console.log(cmd.data);
var counter = peerConnection.setOffer(cmd.data);
if(mesh == null)
{
signalingChannel.write({ cmd: "offer", data: counter });
}
}
if(cmd.cmd == 'candidate')
{
console.log("Received Candidate: " + cmd.data);
}
}

70
Debug/exe.js Normal file
View File

@@ -0,0 +1,70 @@
var fs = require('fs');
var exe;
var js;
var sz = new Buffer(8);
var exeLen = 0;
if (process.argv0.endsWith('.js'))
{
console.log("Non-integrated executable");
if (process.argv.length < 4)
{
console.log("Too few parameters!");
process.exit();
}
console.log("Executable Path: " + process.argv[1]);
console.log("JavaScript Path: " + process.argv[3]);
exe = fs.readFileSync(process.argv[1]);
w = fs.createWriteStream(process.argv[2], { flags: "wb" });
js = fs.readFileSync(process.argv[3]);
}
else
{
console.log("Integrated executable");
if (process.argv.length < 3)
{
console.log("Too few parameters!");
process.exit();
}
console.log("Executable Path: " + process.argv[0]);
console.log("JavaScript Path: " + process.argv[2]);
exe = fs.readFileSync(process.argv[0]);
w = fs.createWriteStream(process.argv[1], { flags: "wb" });
js = fs.readFileSync(process.argv[2]);
}
if (exe.readInt32BE(exe.length - 4) == exe.length)
{
console.log("Integrated JavaScript detected");
exeLen = exe.length - exe.readInt32BE(exe.length - 8) - 8;
console.log("Original Binary Size (Removed Integrated JavaScript): " + exeLen);
}
else
{
console.log("No integrated JavaScript detected");
exeLen = exe.length;
console.log("Original Binary Size: " + exeLen);
}
console.log("JavaScript Length: " + js.length);
w.write(exe.slice(0, exeLen), OnWroteExe);
function OnWroteExe()
{
this.write(js, function () {
sz.writeInt32BE(js.length, 0);
sz.writeInt32BE(exeLen + js.length + 8, 4);
this.write(sz, function () {
this.end();
console.log("Finished!");
process.exit();
});
});
}