Improved UTF8 support on the server

This commit is contained in:
Ylian Saint-Hilaire 2018-07-06 10:13:19 -07:00
parent 3dafa39e79
commit b41eb7fb55
14 changed files with 41 additions and 111 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -438,9 +438,7 @@ function createMeshCore(agent) {
} else {
// List all the files and folders in this path
if (reqpath == '') { reqpath = '/'; }
var xpath = obj.path.join(reqpath, '*');
var results = null;
var results = null, xpath = obj.path.join(reqpath, '*');
try { results = fs.readdirSync(xpath); } catch (e) { }
if (results != null) {
for (var i = 0; i < results.length; ++i) {

View File

@ -411,7 +411,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
function processAgentData(msg) {
var str = msg.toString('utf8');
if (str[0] == '{') {
try { command = JSON.parse(str) } catch (e) { console.log('Unable to parse JSON (' + obj.remoteaddr + ').'); return; } // If the command can't be parsed, ignore it.
try { command = JSON.parse(str) } catch (e) { console.log('Unable to parse agent JSON (' + obj.remoteaddr + '): ' + str); return; } // If the command can't be parsed, ignore it.
switch (command.action) {
case 'msg':
{

View File

@ -182,7 +182,7 @@ module.exports.CreateMeshRelay = function (parent, ws, req, domain) {
ws.on('message', function (data) {
//console.log(typeof data, data.length);
if (this.peer != null) {
//if (typeof data == 'string') { console.log('Relay: ' + data); }
//if (typeof data == 'string') { console.log('Relay: ' + data); } else { console.log('Relay:' + data.length + ' byte(s)'); }
try { this.pause(); this.peer.send(data, ws.flushSink); } catch (e) { }
}
});

View File

@ -173,7 +173,7 @@ module.exports.CreateMultiServer = function (parent, args) {
function processServerData(msg) {
var str = msg.toString('utf8');
if (str[0] == '{') {
try { command = JSON.parse(str) } catch (e) { obj.parent.parent.debug(1, 'Unable to parse JSON (' + obj.remoteaddr + ').'); return; } // If the command can't be parsed, ignore it.
try { command = JSON.parse(str) } catch (e) { obj.parent.parent.debug(1, 'Unable to parse server JSON (' + obj.remoteaddr + ').'); return; } // If the command can't be parsed, ignore it.
if (command.action == 'info') {
if (obj.authenticated != 3) {
// We get the peer's serverid and database identifier.
@ -337,7 +337,7 @@ module.exports.CreateMultiServer = function (parent, args) {
function processServerData(msg) {
var str = msg.toString('utf8');
if (str[0] == '{') {
try { command = JSON.parse(str) } catch (e) { obj.parent.parent.debug(1, 'Unable to parse JSON (' + obj.remoteaddr + ').'); return; } // If the command can't be parsed, ignore it.
try { command = JSON.parse(str) } catch (e) { obj.parent.parent.debug(1, 'Unable to parse server JSON (' + obj.remoteaddr + ').'); return; } // If the command can't be parsed, ignore it.
if (command.action == 'info') {
if (obj.authenticated != 3) {
// We get the peer's serverid and database identifier.

View File

@ -1,6 +1,6 @@
{
"name": "meshcentral",
"version": "0.1.8-k",
"version": "0.1.8-l",
"keywords": [
"Remote Management",
"Intel AMT",

View File

@ -167,6 +167,11 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort) {
return obj.m.ProcessData(data);
}
obj.sendText = function (x) {
if (typeof x != 'string') { x = JSON.stringify(x); } // Turn into a string if needed
obj.send(encode_utf8(x)); // Encode UTF8 correctly
}
obj.send = function (x) {
//obj.debug("Agent Redir Send(" + obj.webRtcActive + ", " + x.length + "): " + rstr2hex(x));
//console.log("Agent Redir Send(" + obj.webRtcActive + ", " + x.length + "): " + ((typeof x == 'string')?x:rstr2hex(x)));

View File

@ -35,14 +35,6 @@ var CreateAmtRemoteTerminal = function (divid) {
var _tscreen = [];
var _VTUNDERLINE = 1;
var _VTREVERSE = 2;
// ###BEGIN###{Terminal-Enumation-All}
var _utf8accumulator = 0;
var _utf8accumulatorCount = 0;
// ###END###{Terminal-Enumation-All}
// ###BEGIN###{Terminal-Enumation-UTF8}
var _utf8accumulator = 0;
var _utf8accumulatorCount = 0;
// ###END###{Terminal-Enumation-UTF8}
obj.Start = function () { }
@ -60,7 +52,15 @@ var CreateAmtRemoteTerminal = function (divid) {
obj.xxStateChange = function(newstate) { }
obj.ProcessData = function (str) { if (obj.capture != null) obj.capture += str; _ProcessVt100EscString(str); obj.TermDraw(); }
obj.ProcessData = function (str) {
// ###BEGIN###{Terminal-Enumation-UTF8}
//str = decode_utf8(str);
// ###END###{Terminal-Enumation-UTF8}
// ###BEGIN###{Terminal-Enumation-All}
if (obj.terminalEmulation == 0) { str = decode_utf8(str); }
// ###END###{Terminal-Enumation-All}
if (obj.capture != null) obj.capture += str; _ProcessVt100EscString(str); obj.TermDraw();
}
function _ProcessVt100EscString(str) { for (var i = 0; i < str.length; i++) _ProcessVt100EscChar(String.fromCharCode(str.charCodeAt(i)), str.charCodeAt(i)); }
@ -363,48 +363,11 @@ var CreateAmtRemoteTerminal = function (divid) {
function _ProcessVt100Char(c) {
if (c == '\0' || c.charCodeAt() == 7) return; // Ignore null & bell
var ch = c.charCodeAt();
// ###BEGIN###{Terminal-Enumation-All}
// UTF8 Terminal
if (obj.terminalEmulation == 0) {
// VT100 - UTF-8 emulation
var fallout = true;
if ((ch & 0x80) == 0) {
// Sub 127 char.
_utf8accumulator = ch;
_utf8accumulatorCount = 0;
fallout = false;
}
else if ((ch & 0xE0) == 0xC0) {
// 2 byte char
_utf8accumulator = (ch & 0x1F);
_utf8accumulatorCount = 1;
fallout = true;
}
else if ((ch & 0xF0) == 0xE0) {
// 3 byte char
_utf8accumulator = (ch & 0x0F);
_utf8accumulatorCount = 2;
fallout = true;
}
else if ((ch & 0xC0) == 0x80) {
if (_utf8accumulatorCount > 0) {
_utf8accumulator = (_utf8accumulator << 6);
_utf8accumulator += (ch & 0x3F);
_utf8accumulatorCount--;
fallout = (_utf8accumulatorCount != 0);
}
else {
_utf8accumulator = 0;
_utf8accumulatorCount = 0;
fallout = true;
}
}
if (fallout == true) return;
c = String.fromCharCode(_utf8accumulator);
} else if (obj.terminalEmulation == 1) {
if (obj.terminalEmulation == 1) {
// ANSI - Extended ASCII emulation.
if ((ch & 0x80) != 0) { c = String.fromCharCode(AsciiToUnicode[ch & 0x7F]); }
} else if (obj.terminalEmulation == 2) {
@ -413,52 +376,14 @@ var CreateAmtRemoteTerminal = function (divid) {
}
// ###END###{Terminal-Enumation-All}
// ###BEGIN###{Terminal-Enumation-UTF8}
// VT100 - UTF-8 emulation
var fallout = true;
if ((ch & 0x80) == 0) {
// Sub 127 char.
_utf8accumulator = ch;
_utf8accumulatorCount = 0;
fallout = false;
}
else if ((ch & 0xE0) == 0xC0) {
// 2 byte char
_utf8accumulator = (ch & 0x1F);
_utf8accumulatorCount = 1;
fallout = true;
}
else if ((ch & 0xF0) == 0xE0) {
// 3 byte char
_utf8accumulator = (ch & 0x0F);
_utf8accumulatorCount = 2;
fallout = true;
}
else if ((ch & 0xC0) == 0x80) {
if (_utf8accumulatorCount > 0) {
_utf8accumulator = (_utf8accumulator << 6);
_utf8accumulator += (ch & 0x3F);
_utf8accumulatorCount--;
fallout = (_utf8accumulatorCount != 0);
}
else {
_utf8accumulator = 0;
_utf8accumulatorCount = 0;
fallout = true;
}
}
if (fallout == true) return;
c = String.fromCharCode(_utf8accumulator);
// ###END###{Terminal-Enumation-UTF8}
// ###BEGIN###{Terminal-Enumation-ASCII}
// ANSI - Extended ASCII emulation.
if ((ch & 0x80) != 0) { c = String.fromCharCode(AsciiToUnicode[ch & 0x7F]); }
//if ((ch & 0x80) != 0) { c = String.fromCharCode(AsciiToUnicode[ch & 0x7F]); }
// ###END###{Terminal-Enumation-ASCII}
// ###BEGIN###{Terminal-Enumation-Intel}
// ANSI - Intel Extended ASCII emulation.
if ((ch & 0x80) != 0) { c = String.fromCharCode(AsciiToUnicodeIntel[ch & 0x7F]); }
//if ((ch & 0x80) != 0) { c = String.fromCharCode(AsciiToUnicodeIntel[ch & 0x7F]); }
// ###END###{Terminal-Enumation-Intel}
//if (ch < 32 && ch != 10 && ch != 13) alert(ch);

View File

@ -2,7 +2,7 @@
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<meta name="viewport" content="user-scalable=1.0,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="format-detection" content="telephone=no" />
@ -3767,6 +3767,7 @@
terminal.onStateChanged = onTerminalStateChange;
terminal.Start(terminalNode._id, 16994, '*', '*', 0);
terminal.contype = 2;
Q('id_ttypebutton').value = terminalEmulations[terminal.m.terminalEmulation];
} else {
// Setup a mesh agent terminal
terminal = CreateAgentRedirect(meshserver, CreateAmtRemoteTerminal('Term'), serverPublicNamePort);
@ -3777,6 +3778,8 @@
terminal.onStateChanged = onTerminalStateChange;
terminal.Start(terminalNode._id);
terminal.contype = 1;
terminal.m.terminalEmulation = 0;
Q('id_ttypebutton').value = terminalEmulations[0];
}
} else {
//QH('Term', '');
@ -3860,7 +3863,7 @@
break;
case 3:
p13targetpath = '';
files.send(JSON.stringify({ action: 'ls', reqid: 1, path: '' }));
files.sendText({ action: 'ls', reqid: 1, path: '' });
break;
}
}
@ -3901,10 +3904,9 @@
var p13filetreelocation = [];
function p13gotFiles(data) {
//console.log('p13gotFiles', data);
if ((data.length > 0) && (data.charCodeAt(0) != 123)) { p13gotDownloadBinaryData(data); return; }
//console.log('p13gotFiles', data);
data = JSON.parse(data);
data = JSON.parse(decode_utf8(data));
if (data.action == 'download') { p13gotDownloadCommand(data); return; }
data.path = data.path.replace(/\//g, "\\");
if ((p13filetree != null) && (data.path == p13filetree.path)) {
@ -3990,13 +3992,13 @@
function p13folderset(x) {
p13targetpath = joinPaths(p13filetree.path, p13filetree.dir[x].n).split('\\').join('/');
files.send(JSON.stringify({ action: 'ls', reqid: 1, path: p13targetpath }));
files.sendText({ action: 'ls', reqid: 1, path: p13targetpath });
}
function p13folderup(x) {
if (x == null) { p13filetreelocation.pop(); } else { while (p13filetreelocation.length > x) { p13filetreelocation.pop(); } }
p13targetpath = p13filetreelocation.join('/');
files.send(JSON.stringify({ action: 'ls', reqid: 1, path: p13targetpath }));
files.sendText({ action: 'ls', reqid: 1, path: p13targetpath });
}
var p13sortorder;
@ -4047,11 +4049,11 @@
function p13getFileCount() { var cc = 0; var checkboxes = document.getElementsByName('fd'); return checkboxes.length; }
function p13selectallfile() { var nv = (p13getFileSelCount() == 0), checkboxes = document.getElementsByName('fd'); for (var i = 0; i < checkboxes.length; i++) { checkboxes[i].checked = nv; } p13setActions(); }
function p13createfolder() { setDialogMode(2, "New Folder", 3, p13createfolderEx, '<input type=text id=p13renameinput maxlength=64 onkeyup=p13fileNameCheck(event) style=width:100% />'); focusTextBox('p13renameinput'); p13fileNameCheck(); }
function p13createfolderEx() { files.send(JSON.stringify({ action: 'mkdir', reqid: 1, path: p13filetreelocation.join('/') + '/' + Q('p13renameinput').value })); p13folderup(999); }
function p13createfolderEx() { files.sendText({ action: 'mkdir', reqid: 1, path: p13filetreelocation.join('/') + '/' + Q('p13renameinput').value }); p13folderup(999); }
function p13deletefile() { var cc = getFileSelCount(); setDialogMode(2, "Delete", 3, p13deletefileEx, (cc > 1)?('Delete ' + cc + ' selected items?'):('Delete selected item?')); }
function p13deletefileEx() { var delfiles = [], checkboxes = document.getElementsByName('fd'); for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i].checked) { delfiles.push(p13filetree.dir[checkboxes[i].value].n); } } files.send(JSON.stringify({ action: 'rm', reqid: 1, path: p13filetreelocation.join('/'), delfiles: delfiles })); p13folderup(999); }
function p13deletefileEx() { var delfiles = [], checkboxes = document.getElementsByName('fd'); for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i].checked) { delfiles.push(p13filetree.dir[checkboxes[i].value].n); } } files.sendText({ action: 'rm', reqid: 1, path: p13filetreelocation.join('/'), delfiles: delfiles }); p13folderup(999); }
function p13renamefile() { var renamefile, checkboxes = document.getElementsByName('fd'); for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i].checked) { renamefile = p13filetree.dir[checkboxes[i].value].n; } } setDialogMode(2, "Rename", 3, p13renamefileEx, '<input type=text id=p13renameinput maxlength=64 onkeyup=p13fileNameCheck(event) style=width:100% value="' + renamefile + '" />', { action: 'rename', path: p13filetreelocation.join('/'), oldname: renamefile}); focusTextBox('p13renameinput'); p13fileNameCheck(); }
function p13renamefileEx(b, t) { t.newname = Q('p13renameinput').value; files.send(JSON.stringify(t)); p13folderup(999); }
function p13renamefileEx(b, t) { t.newname = Q('p13renameinput').value; files.sendText(t); p13folderup(999); }
function p13fileNameCheck(e) { var x = isFilenameValid(Q('p13renameinput').value); QE('idx_dlgOkButton', x); if ((x == true) && (e != null) && (e.keyCode == 13)) { dialogclose(1); } }
function p13uploadFile() { setDialogMode(2, "Upload File", 3, p13uploadFileEx, '<input type=file name=files id=p13uploadinput style=width:100% multiple=multiple onchange="updateUploadDialogOk(\'p13uploadinput\')" />'); updateUploadDialogOk('p13uploadinput'); }
function p13uploadFileEx() { p13doUploadFiles(Q('p13uploadinput').files); }
@ -4059,7 +4061,7 @@
var p13clipboard = null, p13clipboardFolder = null, p13clipboardCut = 0;
function p13copyFile(cut) { var checkboxes = document.getElementsByName('fd'); p13clipboard = []; p13clipboardCut = cut, p13clipboardFolder = p13targetpath; for (var i = 0; i < checkboxes.length; i++) { if ((checkboxes[i].checked) && (checkboxes[i].attributes.file.value == "3")) { p13clipboard.push(p13filetree.dir[checkboxes[i].value].n); } } p13updateClipview(); }
function p13pasteFile() { var x = ''; if ((p13clipboard != null) && (p13clipboard.length > 0)) { x = 'Confim ' + (p13clipboardCut == 0?'copy':'move') + ' of ' + p13clipboard.length + ' entrie' + ((p13clipboard.length > 1)?'s':'') + ' to this location?' } setDialogMode(2, "Paste", 3, p13pasteFileEx, x); }
function p13pasteFileEx() { files.send(JSON.stringify({ action: (p13clipboardCut == 0?'copy':'move'), reqid: 1, scpath: p13clipboardFolder, dspath: p13targetpath, names: p13clipboard })); p13folderup(999); if (p13clipboardCut == 1) { p13clipboard = null, p13clipboardFolder = null, p13clipboardCut = 0; p13updateClipview(); } }
function p13pasteFileEx() { files.sendText({ action: (p13clipboardCut == 0?'copy':'move'), reqid: 1, scpath: p13clipboardFolder, dspath: p13targetpath, names: p13clipboard }); p13folderup(999); if (p13clipboardCut == 1) { p13clipboard = null, p13clipboardFolder = null, p13clipboardCut = 0; p13updateClipview(); } }
function p13updateClipview() { var x = ''; if ((p13clipboard != null) && (p13clipboard.length > 0)) { x = 'Holding ' + p13clipboard.length + ' entrie' + ((p13clipboard.length > 1)?'s':'') + ' for ' + (p13clipboardCut == 0?'copy':'move') + ', <a onclick=p13clearClip() style=cursor:pointer>Clear</a>.' } QH('p13bottomstatus', x); p13setActions(); }
function p13clearClip() { p13clipboard = null; p13clipboardFolder = null; p13clipboardCut = 0; p13updateClipview(); }
@ -4101,18 +4103,18 @@
if (xxdialogMode || downloadFile || !files) return;
downloadFile = { path: decodeURIComponent(x), file: decodeURIComponent(y), size: z, tsize: 0, data: '', state: 0, id: Math.random() }
//console.log('p13downloadFileCancel', downloadFile);
files.send(JSON.stringify({ action: 'download', sub: 'start', id: downloadFile.id, path: downloadFile.path }));
files.sendText({ action: 'download', sub: 'start', id: downloadFile.id, path: downloadFile.path });
setDialogMode(2, "Download File", 10, p13downloadFileCancel, '<div>' + downloadFile.file + '</div><br /><progress id=d2progressBar style=width:100% value=0 max=' + z + ' />');
}
// Called by the html page to cancel the download
function p13downloadFileCancel() { setDialogMode(0); files.send(JSON.stringify({ action: 'download', sub: 'cancel', id: downloadFile.id })); downloadFile = null; }
function p13downloadFileCancel() { setDialogMode(0); files.sendText({ action: 'download', sub: 'cancel', id: downloadFile.id }); downloadFile = null; }
// Called by the transport when download control command is received
function p13gotDownloadCommand(cmd) {
//console.log('p13gotDownloadCommand', cmd);
if ((downloadFile == null) || (cmd.id != downloadFile.id)) return;
if (cmd.sub == 'start') { downloadFile.state = 1; files.send(JSON.stringify({ action: 'download', sub: 'startack', id: downloadFile.id })); }
if (cmd.sub == 'start') { downloadFile.state = 1; files.sendText({ action: 'download', sub: 'startack', id: downloadFile.id }); }
else if (cmd.sub == 'cancel') { downloadFile = null; setDialogMode(0); }
}
@ -4127,7 +4129,7 @@
if ((ReadInt(data, 0) & 1) != 0) { // Check end flag
saveAs(data2blob(downloadFile.data), downloadFile.file); downloadFile = null; setDialogMode(0); // Save the file
} else {
files.send(JSON.stringify({ action: 'download', sub: 'ack', id: downloadFile.id })); // Send the ACK
files.sendText({ action: 'download', sub: 'ack', id: downloadFile.id }); // Send the ACK
}
}