2018-05-01 09:56:04 +00:00
|
|
|
/*
|
|
|
|
* 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'
|
2018-05-08 14:07:51 +00:00
|
|
|
import CodeMirror from 'plugins/wazuh/utils/codemirror/lib/codemirror'
|
2018-05-04 16:03:35 +00:00
|
|
|
|
2018-05-01 09:56:04 +00:00
|
|
|
const app = modules.get('app/wazuh', []);
|
|
|
|
|
|
|
|
// Logs controller
|
2018-05-08 14:25:49 +00:00
|
|
|
app.controller('devToolsController', function($scope, $rootScope, errorHandler, apiReq, $window, appState, $location) {
|
|
|
|
if(!appState.getCurrentAPI()){
|
|
|
|
errorHandler.handle('Wazuh App: Please set up the Wazuh API.','Dev tools',true);
|
|
|
|
|
|
|
|
$location.search('_a', null);
|
|
|
|
$location.search('tab', 'api');
|
|
|
|
$location.path('/settings');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-08 14:07:51 +00:00
|
|
|
let groups = [];
|
|
|
|
const analyzeGroups = () => {
|
|
|
|
try{
|
2018-05-08 14:25:49 +00:00
|
|
|
const currentState = apiInputBox.getValue().toString();
|
|
|
|
appState.setCurrentDevTools(currentState)
|
2018-05-08 14:07:51 +00:00
|
|
|
const groups = [];
|
2018-05-08 14:25:49 +00:00
|
|
|
const splitted = currentState.split(/[\r\n]+(?=(?:GET|PUT|POST|DELETE)\b)/gm)
|
2018-05-08 14:07:51 +00:00
|
|
|
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
|
|
|
}
|
2018-05-08 14:07:51 +00:00
|
|
|
return groups;
|
|
|
|
} catch(error){
|
|
|
|
return false;
|
2018-05-04 16:03:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 14:07:51 +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"]
|
|
|
|
});
|
|
|
|
|
2018-05-08 14:25:49 +00:00
|
|
|
apiInputBox.on('change',() => {
|
|
|
|
const currentState = apiInputBox.getValue().toString();
|
|
|
|
appState.setCurrentDevTools(currentState)
|
|
|
|
})
|
|
|
|
|
2018-05-08 14:13:50 +00:00
|
|
|
const init = () => {
|
|
|
|
apiInputBox.setSize('auto','100%')
|
|
|
|
apiOutputBox.setSize('auto','100%')
|
2018-05-08 14:25:49 +00:00
|
|
|
const currentState = appState.getCurrentDevTools();
|
|
|
|
if(!currentState){
|
|
|
|
const demoStr = 'GET /\n\nGET /agents\n' + JSON.stringify({limit:1},null,2);
|
|
|
|
appState.setCurrentDevTools(demoStr);
|
|
|
|
apiInputBox.getDoc().setValue(demoStr);
|
|
|
|
} else {
|
|
|
|
apiInputBox.getDoc().setValue(currentState)
|
|
|
|
}
|
2018-05-08 14:13:50 +00:00
|
|
|
}
|
2018-05-08 14:07:51 +00:00
|
|
|
|
|
|
|
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"]
|
|
|
|
});
|
|
|
|
|
|
|
|
const calculateWhichGroup = () => {
|
|
|
|
const selection = apiInputBox.getCursor()
|
|
|
|
const desiredGroup = groups.filter(item => item.end >= selection.line && item.start <= selection.line);
|
|
|
|
return desiredGroup ? desiredGroup[0] : null;
|
|
|
|
}
|
|
|
|
|
2018-05-01 09:56:04 +00:00
|
|
|
$scope.send = async () => {
|
|
|
|
try {
|
2018-05-08 14:07:51 +00:00
|
|
|
groups = analyzeGroups();
|
|
|
|
const desiredGroup = calculateWhichGroup();
|
|
|
|
|
|
|
|
const method = desiredGroup.requestText.startsWith('GET') ?
|
2018-05-04 16:03:35 +00:00
|
|
|
'GET' :
|
2018-05-08 14:07:51 +00:00
|
|
|
desiredGroup.requestText.startsWith('POST') ?
|
2018-05-04 16:03:35 +00:00
|
|
|
'POST' :
|
2018-05-08 14:07:51 +00:00
|
|
|
desiredGroup.requestText.startsWith('PUT') ?
|
2018-05-04 16:03:35 +00:00
|
|
|
'PUT' :
|
2018-05-08 14:07:51 +00:00
|
|
|
desiredGroup.requestText.startsWith('DELETE') ?
|
2018-05-04 16:03:35 +00:00
|
|
|
'DELETE' :
|
|
|
|
'GET';
|
|
|
|
|
2018-05-08 14:07:51 +00:00
|
|
|
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-01 09:56:04 +00:00
|
|
|
'/';
|
|
|
|
|
2018-05-04 16:03:35 +00:00
|
|
|
let validJSON = true, JSONraw = {};
|
|
|
|
try {
|
2018-05-08 14:07:51 +00:00
|
|
|
JSONraw = JSON.parse(desiredGroup.requestTextJson);
|
2018-05-04 16:03:35 +00:00
|
|
|
} catch(error) {
|
|
|
|
validJSON = false;
|
|
|
|
}
|
|
|
|
|
2018-05-01 09:56:04 +00:00
|
|
|
const path = req.includes('?') ? req.split('?')[0] : req;
|
|
|
|
const params = req.includes('?') ? parseParams(req.split('?')[1]) : {}
|
2018-05-04 16:03:35 +00:00
|
|
|
const output = await apiReq.request(method, path, validJSON && !req.includes('?') ? JSONraw : params)
|
2018-05-08 14:07:51 +00:00
|
|
|
|
|
|
|
apiOutputBox.setValue(JSON.stringify(output.data.data,null,2))
|
2018-05-01 09:56:04 +00:00
|
|
|
|
|
|
|
} catch(error) {
|
2018-05-08 14:07:51 +00:00
|
|
|
console.log(error.message || error)
|
2018-05-01 09:56:04 +00:00
|
|
|
$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-08 14:13:50 +00:00
|
|
|
init();
|
2018-05-04 16:03:35 +00:00
|
|
|
$scope.send();
|
2018-05-08 14:07:51 +00:00
|
|
|
|
2018-05-01 09:56:04 +00:00
|
|
|
});
|