Add recording feature to websockify.js. Closes GH-249
This commit is contained in:
parent
6b1d42e643
commit
46406c76e7
|
@ -29,6 +29,8 @@ var argv = require('optimist').argv,
|
||||||
// Handle new WebSocket client
|
// Handle new WebSocket client
|
||||||
new_client = function(client, req) {
|
new_client = function(client, req) {
|
||||||
var clientAddr = client._socket.remoteAddress, log;
|
var clientAddr = client._socket.remoteAddress, log;
|
||||||
|
var start_time = new Date().getTime();
|
||||||
|
|
||||||
console.log(req ? req.url : client.upgradeReq.url);
|
console.log(req ? req.url : client.upgradeReq.url);
|
||||||
log = function (msg) {
|
log = function (msg) {
|
||||||
console.log(' ' + clientAddr + ': '+ msg);
|
console.log(' ' + clientAddr + ': '+ msg);
|
||||||
|
@ -36,11 +38,25 @@ new_client = function(client, req) {
|
||||||
log('WebSocket connection');
|
log('WebSocket connection');
|
||||||
log('Version ' + client.protocolVersion + ', subprotocol: ' + client.protocol);
|
log('Version ' + client.protocolVersion + ', subprotocol: ' + client.protocol);
|
||||||
|
|
||||||
|
if (argv.record) {
|
||||||
|
var rs = fs.createWriteStream(argv.record + '/' + new Date().toISOString());
|
||||||
|
rs.write('var VNC_frame_data = [\n');
|
||||||
|
} else {
|
||||||
|
var rs = null;
|
||||||
|
}
|
||||||
|
|
||||||
var target = net.createConnection(target_port,target_host, function() {
|
var target = net.createConnection(target_port,target_host, function() {
|
||||||
log('connected to target');
|
log('connected to target');
|
||||||
});
|
});
|
||||||
target.on('data', function(data) {
|
target.on('data', function(data) {
|
||||||
//log("sending message: " + data);
|
//log("sending message: " + data);
|
||||||
|
|
||||||
|
if (rs) {
|
||||||
|
var tdelta = Math.floor(new Date().getTime()) - start_time;
|
||||||
|
var rsdata = '\'{' + tdelta + '{' + decodeBuffer(data) + '\',\n';
|
||||||
|
rs.write(rsdata);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
client.send(data);
|
client.send(data);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
@ -51,15 +67,28 @@ new_client = function(client, req) {
|
||||||
target.on('end', function() {
|
target.on('end', function() {
|
||||||
log('target disconnected');
|
log('target disconnected');
|
||||||
client.close();
|
client.close();
|
||||||
|
if (rs) {
|
||||||
|
rs.end('\'EOF\'];\n');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
target.on('error', function() {
|
target.on('error', function() {
|
||||||
log('target connection error');
|
log('target connection error');
|
||||||
target.end();
|
target.end();
|
||||||
client.close();
|
client.close();
|
||||||
|
if (rs) {
|
||||||
|
rs.end('\'EOF\'];\n');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('message', function(msg) {
|
client.on('message', function(msg) {
|
||||||
//log('got message: ' + msg);
|
//log('got message: ' + msg);
|
||||||
|
|
||||||
|
if (rs) {
|
||||||
|
var rdelta = Math.floor(new Date().getTime()) - start_time;
|
||||||
|
var rsdata = ('\'}' + rdelta + '}' + decodeBuffer(msg) + '\',\n');
|
||||||
|
~ rs.write(rsdata);
|
||||||
|
}
|
||||||
|
|
||||||
target.write(msg);
|
target.write(msg);
|
||||||
});
|
});
|
||||||
client.on('close', function(code, reason) {
|
client.on('close', function(code, reason) {
|
||||||
|
@ -72,6 +101,28 @@ new_client = function(client, req) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function decodeBuffer(buf) {
|
||||||
|
var returnString = '';
|
||||||
|
for (var i = 0; i < buf.length; i++) {
|
||||||
|
if (buf[i] >= 48 && buf[i] <= 90) {
|
||||||
|
returnString += String.fromCharCode(buf[i]);
|
||||||
|
} else if (buf[i] === 95) {
|
||||||
|
returnString += String.fromCharCode(buf[i]);
|
||||||
|
} else if (buf[i] >= 97 && buf[i] <= 122) {
|
||||||
|
returnString += String.fromCharCode(buf[i]);
|
||||||
|
} else {
|
||||||
|
var charToConvert = buf[i].toString(16);
|
||||||
|
if (charToConvert.length === 0) {
|
||||||
|
returnString += '\\x00';
|
||||||
|
} else if (charToConvert.length === 1) {
|
||||||
|
returnString += '\\x0' + charToConvert;
|
||||||
|
} else {
|
||||||
|
returnString += '\\x' + charToConvert;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return returnString;
|
||||||
|
}
|
||||||
|
|
||||||
// Send an HTTP error response
|
// Send an HTTP error response
|
||||||
http_error = function (response, code, msg) {
|
http_error = function (response, code, msg) {
|
||||||
|
@ -147,7 +198,7 @@ try {
|
||||||
throw("illegal port");
|
throw("illegal port");
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error("websockify.js [--web web_dir] [--cert cert.pem [--key key.pem]] [source_addr:]source_port target_addr:target_port");
|
console.error("websockify.js [--web web_dir] [--cert cert.pem [--key key.pem]] [--record dir] [source_addr:]source_port target_addr:target_port");
|
||||||
process.exit(2);
|
process.exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue