wazuh-kibana-app/public/controllers/dev-tools.js

144 lines
5.0 KiB
JavaScript
Raw Normal View History

/*
* Wazuh app - Blank screen controller
* Copyright (C) 2018 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import * as modules from 'ui/modules'
2018-05-04 16:03:35 +00:00
import beautifier from 'plugins/wazuh/utils/json-beautifier'
import CodeMirror from 'plugins/wazuh/utils/codemirror/lib/codemirror'
2018-05-04 16:03:35 +00:00
const app = modules.get('app/wazuh', []);
// Logs controller
app.controller('devToolsController', function($scope, $rootScope, errorHandler, apiReq, $window, appState) {
let groups = [];
const analyzeGroups = () => {
try{
const groups = [];
const splitted = apiInputBox.getValue().toString().split(/[\r\n]+(?=(?:GET|PUT|POST|DELETE)\b)/gm)
let start = 0, end = 0;
let lastElement = null;
for(let i=0; i<splitted.length; i++){
start = lastElement ? lastElement.length + i : i;
const tmp = splitted[i].split('\n');
end = start + tmp.length;
lastElement = tmp;
const tmpRequestText = tmp[0];
let tmpRequestTextJson = '';
let j=1;
for(j=1; j<tmp.length; j++){
if(!!tmp[j]){
tmpRequestTextJson += tmp[j];
}
}
groups.push({
requestText: tmpRequestText,
requestTextJson: tmpRequestTextJson,
start,
end
})
2018-05-04 16:16:01 +00:00
}
return groups;
} catch(error){
return false;
2018-05-04 16:03:35 +00:00
}
}
const apiInputBox = CodeMirror.fromTextArea(document.getElementById('api_input'),{
lineNumbers : true,
matchBrackets: true,
mode: {name:"javascript",json:true},
styleActiveLine: true,
theme:'ttcn',
foldGutter: true,
gutters: ["CodeMirror-foldgutter"]
});
apiInputBox.getDoc().setValue('GET /agents\n' + JSON.stringify({limit:1},null,2));
let requestText = 'GET /agents'
let requestTextJson = '{ "limit": 1 }'
const apiOutputBox = CodeMirror.fromTextArea(document.getElementById('api_output'),{
lineNumbers : true,
matchBrackes: true,
mode: {name:"javascript",json:true},
readOnly: true,
lineWrapping: true,
styleActiveLine: true,
theme:'ttcn',
foldGutter: true,
gutters: ["CodeMirror-foldgutter"]
});
apiInputBox.setSize('auto','100%')
apiOutputBox.setSize('auto','100%')
const calculateWhichGroup = () => {
const selection = apiInputBox.getCursor()
const desiredGroup = groups.filter(item => item.end >= selection.line && item.start <= selection.line);
return desiredGroup ? desiredGroup[0] : null;
}
$scope.send = async () => {
try {
groups = analyzeGroups();
const desiredGroup = calculateWhichGroup();
const method = desiredGroup.requestText.startsWith('GET') ?
2018-05-04 16:03:35 +00:00
'GET' :
desiredGroup.requestText.startsWith('POST') ?
2018-05-04 16:03:35 +00:00
'POST' :
desiredGroup.requestText.startsWith('PUT') ?
2018-05-04 16:03:35 +00:00
'PUT' :
desiredGroup.requestText.startsWith('DELETE') ?
2018-05-04 16:03:35 +00:00
'DELETE' :
'GET';
const requestCopy = desiredGroup.requestText.includes(method) ?
desiredGroup.requestText.split(method)[1].trim() :
desiredGroup.requestText;
2018-05-04 16:03:35 +00:00
const req = requestCopy ?
requestCopy.startsWith('/') ?
requestCopy :
`/${requestCopy}` :
'/';
2018-05-04 16:03:35 +00:00
let validJSON = true, JSONraw = {};
try {
JSONraw = JSON.parse(desiredGroup.requestTextJson);
2018-05-04 16:03:35 +00:00
} catch(error) {
validJSON = false;
}
const path = req.includes('?') ? req.split('?')[0] : req;
const params = req.includes('?') ? parseParams(req.split('?')[1]) : {}
console.log(method, path, validJSON,!req.includes('?'),JSONraw, params)
2018-05-04 16:03:35 +00:00
const output = await apiReq.request(method, path, validJSON && !req.includes('?') ? JSONraw : params)
apiOutputBox.setValue(JSON.stringify(output.data.data,null,2))
} catch(error) {
console.log(error.message || error)
$scope.output = beautifier.prettyPrint(error.data);
if(!$scope.$$phase) $scope.$digest();
}
}
2018-05-04 16:03:35 +00:00
2018-05-07 10:07:52 +00:00
$scope.help = () => {
$window.open('https://documentation.wazuh.com/current/user-manual/api/reference.html');
}
2018-05-04 16:03:35 +00:00
$scope.send();
});