Add recording feature. Ref #249
This commit is contained in:
parent
141ba0088e
commit
21d6e57cc2
|
|
@ -28,18 +28,35 @@ 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;
|
||||||
console.log(req ? req.url : client.upgradeReq.url);
|
var start_time = new Date().getTime();
|
||||||
|
|
||||||
|
console.log(client.upgradeReq.url);
|
||||||
log = function (msg) {
|
log = function (msg) {
|
||||||
console.log(' ' + clientAddr + ': '+ msg);
|
console.log(' ' + clientAddr + ': '+ msg);
|
||||||
};
|
};
|
||||||
log('WebSocket connection');
|
log('WebSocket connection');
|
||||||
log('Version ' + client.protocolVersion + ', subprotocol: ' + client.protocol);
|
log('Version ' + client.protocolVersion + ', subprotocol: ' + client.protocol);
|
||||||
|
|
||||||
|
if (argv.dir) {
|
||||||
|
var rs = fs.createWriteStream(argv.dir + '/' + new Date().toISOString());
|
||||||
|
rs.write('var VNC_frame_encoding = \'binary\';\n');
|
||||||
|
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\',\n';
|
||||||
|
rs.write(rsdata);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
client.send(data);
|
client.send(data);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
|
@ -59,6 +76,13 @@ new_client = function(client, req) {
|
||||||
|
|
||||||
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\',\n');
|
||||||
|
~ rs.write(rsdata);
|
||||||
|
}
|
||||||
|
|
||||||
target.write(msg);
|
target.write(msg);
|
||||||
});
|
});
|
||||||
client.on('close', function(code, reason) {
|
client.on('close', function(code, reason) {
|
||||||
|
|
@ -71,6 +95,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) {
|
||||||
|
|
@ -140,7 +186,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 dir] [source_addr:]source_port target_addr:target_port");
|
||||||
process.exit(2);
|
process.exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue