2017-10-20 19:12:08 +00:00
|
|
|
import chrome from 'ui/chrome';
|
2018-01-29 11:39:33 +00:00
|
|
|
const app = require('ui/modules').get('app/wazuh', []);
|
|
|
|
|
2018-03-13 14:18:49 +00:00
|
|
|
app.service('testAPI', function ($q, $http, $location, $rootScope, appState, genericReq) {
|
2017-10-30 10:35:30 +00:00
|
|
|
return {
|
2017-12-20 16:48:44 +00:00
|
|
|
check_stored: data => {
|
2018-03-11 11:17:33 +00:00
|
|
|
|
2017-10-30 10:35:30 +00:00
|
|
|
let defered = $q.defer();
|
2018-03-12 15:36:20 +00:00
|
|
|
|
2018-03-11 11:17:33 +00:00
|
|
|
const headers = {headers:{ "Content-Type": 'application/json' },timeout: $rootScope.userTimeout || 8000};
|
2018-03-12 15:36:20 +00:00
|
|
|
|
|
|
|
/** Checks for outdated cookies */
|
|
|
|
const current = appState.getCreatedAt();
|
|
|
|
const lastRestart = $rootScope.lastRestart;
|
|
|
|
|
|
|
|
if(current && lastRestart && lastRestart > current){
|
2018-03-12 10:50:32 +00:00
|
|
|
appState.removeCurrentPattern();
|
|
|
|
appState.removeCurrentAPI();
|
|
|
|
appState.removeClusterInfo();
|
2018-03-12 15:36:20 +00:00
|
|
|
appState.removeCreatedAt();
|
|
|
|
delete $rootScope.lastRestart;
|
2018-03-13 14:18:49 +00:00
|
|
|
genericReq.request('GET', '/api/wazuh-api/configuration', {})
|
|
|
|
.then(configuration => {
|
|
|
|
appState.setPatternSelector(typeof configuration.data.data['ip.selector'] !== 'undefined' ? configuration.data.data['ip.selector'] : true)
|
|
|
|
defered.resolve('cookies_outdated');
|
|
|
|
})
|
|
|
|
.catch(error => defered.reject(error));
|
2018-03-11 11:17:33 +00:00
|
|
|
|
2018-03-13 14:18:49 +00:00
|
|
|
/** End of checks for outdated cookies */
|
|
|
|
} else {
|
|
|
|
if(appState.getUserCode()) headers.headers.code = appState.getUserCode();
|
2018-03-12 10:50:32 +00:00
|
|
|
|
2018-03-13 14:18:49 +00:00
|
|
|
Promise.all([
|
|
|
|
genericReq.request('GET', '/api/wazuh-api/configuration', {}),
|
|
|
|
$http.post(chrome.addBasePath('/api/wazuh-api/checkStoredAPI'), data,headers)
|
|
|
|
])
|
|
|
|
.then(data => {
|
|
|
|
appState.setPatternSelector(typeof data[0].data.data['ip.selector'] !== 'undefined' ? data[0].data.data['ip.selector'] : true)
|
|
|
|
if (data[1].error) {
|
|
|
|
defered.reject(data[1]);
|
|
|
|
} else {
|
|
|
|
defered.resolve(data[1]);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
if(error.status && error.status === -1){
|
|
|
|
$rootScope.apiIsDown = true;
|
|
|
|
defered.reject({data: 'request_timeout_checkstored'});
|
|
|
|
} else {
|
|
|
|
defered.reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-10-30 10:35:30 +00:00
|
|
|
return defered.promise;
|
|
|
|
},
|
2017-12-20 16:48:44 +00:00
|
|
|
check: data => {
|
2018-01-30 10:45:33 +00:00
|
|
|
const headers = {headers:{ "Content-Type": 'application/json' },timeout: $rootScope.userTimeout || 8000};
|
2018-01-25 12:43:47 +00:00
|
|
|
if(appState.getUserCode()) headers.headers.code = appState.getUserCode();
|
2017-10-30 10:35:30 +00:00
|
|
|
let defered = $q.defer();
|
2017-12-28 10:25:35 +00:00
|
|
|
const url = chrome.addBasePath("/api/wazuh-api/checkAPI");
|
|
|
|
$http
|
2018-01-25 12:43:47 +00:00
|
|
|
.post(url, data, headers)
|
2017-12-28 10:25:35 +00:00
|
|
|
.then(response => {
|
2017-10-30 10:35:30 +00:00
|
|
|
if (response.error) {
|
|
|
|
defered.reject(response);
|
|
|
|
} else {
|
|
|
|
defered.resolve(response);
|
|
|
|
}
|
|
|
|
})
|
2017-12-14 15:08:38 +00:00
|
|
|
.catch(error => {
|
2018-01-29 11:39:33 +00:00
|
|
|
if(error.data && error.data.message && error.data.message.includes('ENOTFOUND')) {
|
2017-12-21 14:23:09 +00:00
|
|
|
defered.reject({data: 'invalid_url'});
|
|
|
|
} else if(error.data && error.data.message && error.data.message.includes('ECONNREFUSED')) {
|
|
|
|
defered.reject({data: 'invalid_port'});
|
|
|
|
} else if(error.status && error.status === -1){
|
2017-12-14 15:08:38 +00:00
|
|
|
defered.reject({data: 'request_timeout_checkapi'});
|
2017-12-20 16:48:44 +00:00
|
|
|
} else if (error.data && error.data.message && error.data.message === 'wrong_credentials') {
|
|
|
|
defered.reject({data: 'wrong_credentials'});
|
2017-12-28 10:25:35 +00:00
|
|
|
} else if(error.data && ((error.data.message && error.data.message === 'socket hang up') || (parseInt(error.data.error) === 5))) {
|
|
|
|
defered.reject({data:'socket_hang_up',https: (data.url && data.url.includes('https'))});
|
|
|
|
} else {
|
2017-10-30 10:35:30 +00:00
|
|
|
defered.reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return defered.promise;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|