Skip to content

Commit

Permalink
### __WORK IN PROGRESS__
Browse files Browse the repository at this point in the history
* (bluefox) Improved the GUI for enumerations
  • Loading branch information
GermanBluefox committed May 26, 2021
1 parent 19c48bc commit a1d8e4c
Show file tree
Hide file tree
Showing 25 changed files with 563 additions and 255 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ The icons may not be reused in other projects without proper flaticon subscripti
-->

## Changelog
### __WORK IN PROGRESS__
* (bluefox) Improved the GUI for enumerations

### 5.1.2 (2021-05-26)
* (bluefox) Admin5: Fixed logs by the changing of host

Expand Down
113 changes: 58 additions & 55 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -1086,13 +1086,9 @@ function IOSocket(server, settings, adapter, objects, store) {

socket.on('sendTo', function (adapterInstance, command, message, callback) {
if (updateSession(socket) && checkPermissions(socket, 'sendTo', callback, command)) {
adapter.sendTo(adapterInstance, command, message, function (res) {
if (typeof callback === 'function') {
setTimeout(function () {
callback(res);
}, 0);
}
});
adapter.sendTo(adapterInstance, command, message, res =>
typeof callback === 'function' && setImmediate(() =>
callback(res), 0));
}
});

Expand All @@ -1104,7 +1100,7 @@ function IOSocket(server, settings, adapter, objects, store) {
// delLogs, readDirAsZip, writeDirAsZip, readObjectsAsZip, writeObjectsAsZip, checkLogging, updateMultihost
if (updateSession(socket) && checkPermissions(socket, protectedCommands.includes(command) ? 'cmdExec' : 'sendToHost', callback, command)) {
adapter.sendToHost(host, command, message, res =>
typeof callback === 'function' && setTimeout(() => callback(res), 0));
typeof callback === 'function' && setImmediate(() => callback(res)));
}
});

Expand Down Expand Up @@ -1267,59 +1263,67 @@ function IOSocket(server, settings, adapter, objects, store) {
}
});

socket.on('readLogs', function (callback) {
socket.on('readLogs', function (host, callback) {
if (updateSession(socket) && checkPermissions(socket, 'readLogs', callback)) {
let result = {list: []};

// deliver file list
try {
const config = adapter.systemConfig;
// detect file log
if (config && config.log && config.log.transport) {
for (const transport in config.log.transport) {
if (config.log.transport.hasOwnProperty(transport) && config.log.transport[transport].type === 'file') {
let filename = config.log.transport[transport].filename || 'log/';
const parts = filename.replace(/\\/g, '/').split('/');
parts.pop();
filename = parts.join('/');
if (filename[0] !== '/' && !filename.match(/^\W:/)) {
const _filename = path.normalize(__dirname + '/../../../') + filename;
if (!fs.existsSync(_filename)) {
filename = path.normalize(__dirname + '/../../') + filename;
} else {
filename = _filename;
}
}
if (fs.existsSync(filename)) {
const files = fs.readdirSync(filename);

for (let f = 0; f < files.length; f++) {
try {
if (!files[f].endsWith('-audit.json')) {
const stat = fs.lstatSync(filename + '/' + files[f]);
if (!stat.isDirectory()) {
result.list.push({fileName: 'log/' + transport + '/' + files[f], size: stat.size});
let timeout = setTimeout(() => {
if (timeout) {
let result = {list: []};

// deliver file list
try {
const config = adapter.systemConfig;
// detect file log
if (config && config.log && config.log.transport) {
for (const transport in config.log.transport) {
if (config.log.transport.hasOwnProperty(transport) && config.log.transport[transport].type === 'file') {
let filename = config.log.transport[transport].filename || 'log/';
const parts = filename.replace(/\\/g, '/').split('/');
parts.pop();
filename = parts.join('/');
if (filename[0] !== '/' && !filename.match(/^\W:/)) {
const _filename = path.normalize(__dirname + '/../../../') + filename;
if (!fs.existsSync(_filename)) {
filename = path.normalize(__dirname + '/../../') + filename;
} else {
filename = _filename;
}
}
if (fs.existsSync(filename)) {
const files = fs.readdirSync(filename);

for (let f = 0; f < files.length; f++) {
try {
if (!files[f].endsWith('-audit.json')) {
const stat = fs.lstatSync(filename + '/' + files[f]);
if (!stat.isDirectory()) {
result.list.push({fileName: 'log/' + transport + '/' + files[f], size: stat.size});
}
}
} catch (e) {
// push unchecked
// result.list.push('log/' + transport + '/' + files[f]);
adapter.log.error(`Cannot check file: ${filename}/${files[f]}`);
}
}
} catch (e) {
// push unchecked
// result.list.push('log/' + transport + '/' + files[f]);
adapter.log.error(`Cannot check file: ${filename}/${files[f]}`);
}
}
}
} else {
result = {error: 'no file loggers'};
}
} catch (e) {
adapter.log.error(e);
result = {error: e};
}
} else {
result = {error: 'no file loggers'};
typeof callback === 'function' && callback(result.error, result.list);
}
} catch (e) {
adapter.log.error(e);
result = {error: e};
}
if (typeof callback === 'function') {
callback(result.error, result.list);
}
}, 500);

adapter.sendToHost(host, 'getLogFiles', null, result => {
clearTimeout(timeout);
timeout = null;
typeof callback === 'function' && callback(result.error, result.list);
});
}
});

Expand Down Expand Up @@ -1747,7 +1751,7 @@ function IOSocket(server, settings, adapter, objects, store) {
eventsThreshold.timeActivated = 0;
adapter.log.info('Subscribe on all states again');

setTimeout(function () {
setTimeout(() => {
/*if (readAll) {
adapter.getForeignStates('*', function (err, res) {
adapter.log.info('received all states');
Expand All @@ -1765,7 +1769,6 @@ function IOSocket(server, settings, adapter, objects, store) {

Object.keys(that.subscribes.stateChange).forEach(pattern =>
adapter.subscribeForeignStates(pattern));

}, 50);
}
}
Expand All @@ -1774,7 +1777,7 @@ function IOSocket(server, settings, adapter, objects, store) {
if (!eventsThreshold.active) {
eventsThreshold.active = true;

setTimeout(function () {
setTimeout(() => {
adapter.log.info('Unsubscribe from all states, except system\'s, because over ' + eventsThreshold.repeatSeconds + ' seconds the number of events is over ' + eventsThreshold.value + ' (in last second ' + eventsThreshold.count + ')');
eventsThreshold.timeActivated = Date.now();

Expand Down
151 changes: 100 additions & 51 deletions lib/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const util = require('util');
const path = require('path');
const stream = require('stream');

let zlib = null;
let zlib = null;

let session;
let bodyParser;
Expand Down Expand Up @@ -254,6 +254,26 @@ function Web(settings, adapter, onReady, options) {
}
}

function unzipFile(filename, data, res) {
zlib = zlib || require('zlib');

// extract the file
try {
const text = zlib.gunzipSync(data).toString('utf8');
if (text.length > 2 * 1024 * 1024) {
res.header('Content-Type', 'text/plain');
res.send(text);
} else {
res.header('Content-Type', 'text/html');
res.send(decorateLogFile(null, text));
}
} catch (e) {
res.header('Content-Type', 'application/gzip');
res.send(data);
adapter.log.error(`Cannot extract file ${filename}: ${e}`);
}
}

// settings: {
// "port": 8080,
// "auth": false,
Expand Down Expand Up @@ -554,72 +574,101 @@ function Web(settings, adapter, onReady, options) {
// send log files
server.app.get('/log/*', (req, res) => {
let parts = decodeURIComponent(req.url).split('/');
parts = parts.splice(2);
const transport = parts.shift();
let filename = parts.join('/');
const config = adapter.systemConfig;
// detect file log
if (config && config.log && config.log.transport) {
if (config.log.transport.hasOwnProperty(transport) && config.log.transport[transport].type === 'file') {
let logFolder;
if (config.log.transport[transport].filename) {
parts = config.log.transport[transport].filename.replace(/\\/g, '/').split('/');
parts.pop();
logFolder = path.normalize(parts.join('/'));
if (parts.length === 5) {
parts.shift();
parts.shift();
const [host, transport] = parts;
parts = parts.splice(2);
let filename = parts.join('/');
adapter.sendToHost('system.host.' + host, 'getLogFile', {filename, transport}, result => {
if (!result || result.error) {
res.status(404).send(`File ${escapeHtml(filename)} not found`);
} else {
logFolder = path.join(process.cwd(), 'log');
}

if (logFolder[0] !== '/' && logFolder[0] !== '\\' && !logFolder.match(/^[a-zA-Z]:/)) {
const _logFolder = path.normalize(path.join(__dirname + '/../../../', logFolder).replace(/\\/g, '/')).replace(/\\/g, '/');
if (!fs.existsSync(_logFolder)) {
logFolder = path.normalize(path.join(__dirname + '/../../', logFolder).replace(/\\/g, '/')).replace(/\\/g, '/');
} else {
logFolder = _logFolder;
}
}

filename = path.normalize(path.join(logFolder, filename).replace(/\\/g, '/')).replace(/\\/g, '/');

if (filename.startsWith(logFolder) && fs.existsSync(filename)) {
const stat = fs.lstatSync(filename);
// if file is archive
if (filename.toLowerCase().endsWith('.gz')) {
// try to not process to big files
if (stat.size > 1024 * 1024/* || !fs.existsSync('/dev/null')*/) {
if (result.gz) {
if (result.size > 1024 * 1024) {
res.header('Content-Type', 'application/gzip');
res.sendFile(filename);
res.send(result.data);
} else {
zlib = zlib || require('zlib');

// extract the file
try {
const text = zlib.gunzipSync(fs.readFileSync(filename)).toString('utf8');
if (text.length > 2 * 1024 * 1024) {
res.header('Content-Type', 'text/plain');
res.send(text);
} else {
res.header('Content-Type', 'text/html');
res.send(decorateLogFile(null, text));
}
unzipFile(filename, result.data, res);
} catch (e) {
res.sendFile(filename);
res.header('Content-Type', 'application/gzip');
res.send(result.data);
adapter.log.error(`Cannot extract file ${filename}: ${e}`);
}
}
} else if (stat.size > 2 * 1024 * 1024) {
} else if (result.data === undefined || result.data === null) {
res.status(404).send(`File ${escapeHtml(filename)} not found`);
} else if (result.size > 2 * 1024 * 1024) {
res.header('Content-Type', 'text/plain');
res.sendFile(filename);
res.send(result.data);
} else {
res.header('Content-Type', 'text/html');
res.send(decorateLogFile(filename));
res.send(decorateLogFile(null, result.data));
}
}
});
} else {
parts = parts.splice(2);
const transport = parts.shift();
let filename = parts.join('/');
const config = adapter.systemConfig;

// detect file log
if (config && config.log && config.log.transport) {
if (config.log.transport.hasOwnProperty(transport) && config.log.transport[transport].type === 'file') {
let logFolder;
if (config.log.transport[transport].filename) {
parts = config.log.transport[transport].filename.replace(/\\/g, '/').split('/');
parts.pop();
logFolder = path.normalize(parts.join('/'));
} else {
logFolder = path.join(process.cwd(), 'log');
}

return;
if (logFolder[0] !== '/' && logFolder[0] !== '\\' && !logFolder.match(/^[a-zA-Z]:/)) {
const _logFolder = path.normalize(path.join(__dirname + '/../../../', logFolder).replace(/\\/g, '/')).replace(/\\/g, '/');
if (!fs.existsSync(_logFolder)) {
logFolder = path.normalize(path.join(__dirname + '/../../', logFolder).replace(/\\/g, '/')).replace(/\\/g, '/');
} else {
logFolder = _logFolder;
}
}

filename = path.normalize(path.join(logFolder, filename).replace(/\\/g, '/')).replace(/\\/g, '/');

if (filename.startsWith(logFolder) && fs.existsSync(filename)) {
const stat = fs.lstatSync(filename);
// if file is archive
if (filename.toLowerCase().endsWith('.gz')) {
// try to not process to big files
if (stat.size > 1024 * 1024/* || !fs.existsSync('/dev/null')*/) {
res.header('Content-Type', 'application/gzip');
res.sendFile(filename);
} else {
try {
unzipFile(filename, fs.readFileSync(filename), res);
} catch (e) {
res.header('Content-Type', 'application/gzip');
res.sendFile(filename);
adapter.log.error(`Cannot extract file ${filename}: ${e}`);
}
}
} else if (stat.size > 2 * 1024 * 1024) {
res.header('Content-Type', 'text/plain');
res.sendFile(filename);
} else {
res.header('Content-Type', 'text/html');
res.send(decorateLogFile(filename));
}

return;
}
}
}

res.status(404).send(`File ${escapeHtml(filename)} not found`);
}
res.status(404).send('File ' + escapeHtml(filename) + ' not found');
});

const appOptions = {};
Expand Down
Loading

0 comments on commit a1d8e4c

Please sign in to comment.