14 require_once
'hce_node_api.inc.php';
19 defined(
'SPHINX_JSON_USE_BASE64') or define('SPHINX_JSON_USE_BASE64', 0);
23 defined('HCE_DRCE_EXEC_DEFAULT_TIMEOUT') or define('HCE_DRCE_EXEC_DEFAULT_TIMEOUT', 1000);
27 defined('HCE_DRCE_EXEC_DEFAULT_TTL') or define('HCE_DRCE_EXEC_DEFAULT_TTL', 1000);
31 defined('HCE_DRCE_ERROR_OK') or define('HCE_DRCE_ERROR_OK', 0);
32 defined('HCE_DRCE_ERROR_CREATE_MESSAGE') or define('HCE_DRCE_ERROR_CREATE_MESSAGE', -100);
33 defined('HCE_DRCE_ERROR_PARSE_RESPONSE') or define('HCE_DRCE_ERROR_PARSE_RESPONSE', -101);
34 defined('HCE_DRCE_ERROR_CREATE_CONNECTION') or define('HCE_DRCE_ERROR_CREATE_CONNECTION', -102);
35 defined('HCE_DRCE_ERROR_WRONG_PARAMETERS') or define('HCE_DRCE_ERROR_WRONG_PARAMETERS', -103);
40 defined('HCE_DRCE_CMD_EXEC') or define('HCE_DRCE_CMD_EXEC', 'DRCE_EXEC');
45 defined('HCE_DRCE_EXEC_FIELD_SESSION') or define('HCE_DRCE_EXEC_FIELD_SESSION', 'session');
46 defined('HCE_DRCE_EXEC_FIELD_COMMAND') or define('HCE_DRCE_EXEC_FIELD_COMMAND', 'command');
47 defined('HCE_DRCE_EXEC_FIELD_INPUT') or define('HCE_DRCE_EXEC_FIELD_INPUT', 'input');
48 defined('HCE_DRCE_EXEC_FIELD_FILES') or define('HCE_DRCE_EXEC_FIELD_FILES', 'files');
52 defined('HCE_DRCE_EXEC_FIELD_TYPE') or define('HCE_DRCE_EXEC_FIELD_TYPE', 'type');
53 defined('HCE_DRCE_EXEC_FIELD_PORT') or define('HCE_DRCE_EXEC_FIELD_PORT', 'port');
54 defined('HCE_DRCE_EXEC_FIELD_USER') or define('HCE_DRCE_EXEC_FIELD_USER', 'user');
55 defined('HCE_DRCE_EXEC_FIELD_PASSWD') or define('HCE_DRCE_EXEC_FIELD_PASSWD', 'password');
56 defined('HCE_DRCE_EXEC_FIELD_SHELL') or define('HCE_DRCE_EXEC_FIELD_SHELL', 'shell');
57 defined('HCE_DRCE_EXEC_FIELD_ENV') or define('HCE_DRCE_EXEC_FIELD_ENV', 'environment');
58 defined('HCE_DRCE_EXEC_FIELD_TIMEOUT') or define('HCE_DRCE_EXEC_FIELD_TIMEOUT', 'timeout');
59 defined('HCE_DRCE_EXEC_FIELD_TMODE') or define('HCE_DRCE_EXEC_FIELD_TMODE', 'tmode');
60 defined('HCE_DRCE_EXEC_FIELD_HOME_DIR') or define('HCE_DRCE_EXEC_FIELD_HOME_DIR', '');
61 defined('HCE_DRCE_EXEC_FIELD_TIME_MAX') or define('HCE_DRCE_EXEC_FIELD_TIME_MAX', 'time_max');
62 defined('HCE_DRCE_EXEC_FIELD_TIME_MAX_DEFAULT') or define('HCE_DRCE_EXEC_FIELD_TIME_MAX_DEFAULT', 30000);
63 defined('HCE_DRCE_EXEC_FIELD_TMODE_DEFAUL') or define('HCE_DRCE_EXEC_FIELD_TMODE_DEFAUL', 1);
64 defined('HCE_DRCE_EXEC_FIELD_CLEANUP') or define('HCE_DRCE_EXEC_FIELD_CLEANUP', 'cleanup');
65 defined('HCE_DRCE_EXEC_FIELD_CLEANUP_YES') or define('HCE_DRCE_EXEC_FIELD_CLEANUP_YES', 1);
66 defined('HCE_DRCE_EXEC_FIELD_CLEANUP_NO') or define('HCE_DRCE_EXEC_FIELD_CLEANUP_NO', 0);
70 defined('HCE_DRCE_EXEC_FIELD_LIMITS') or define('HCE_DRCE_EXEC_FIELD_LIMITS', 'limits');
74 defined('HCE_DRCE_EXEC_FIELD_NAME') or define('HCE_DRCE_EXEC_FIELD_NAME', 'name');
75 defined('HCE_DRCE_EXEC_FIELD_DATA') or define('HCE_DRCE_EXEC_FIELD_DATA', 'data');
76 defined('HCE_DRCE_EXEC_FIELD_ACTION') or define('HCE_DRCE_EXEC_FIELD_ACTION', 'action');
80 defined('HCE_DRCE_EXEC_FIELD_ERR_CODE') or define('HCE_DRCE_EXEC_FIELD_ERR_CODE', 'error_code');
81 defined('HCE_DRCE_EXEC_FIELD_ERR_MSG') or define('HCE_DRCE_EXEC_FIELD_ERR_MSG', 'error_message');
82 defined('HCE_DRCE_EXEC_FIELD_TIME') or define('HCE_DRCE_EXEC_FIELD_TIME', 'time');
83 defined('HCE_DRCE_EXEC_FIELD_RESULTS') or define('HCE_DRCE_EXEC_FIELD_RESULTS', 'results');
87 defined('HCE_DRCE_EXEC_FIELD_STDOUT') or define('HCE_DRCE_EXEC_FIELD_STDOUT', 'stdout');
88 defined('HCE_DRCE_EXEC_FIELD_STDERR') or define('HCE_DRCE_EXEC_FIELD_STDERR', 'stderror');
89 defined('HCE_DRCE_EXEC_FIELD_FILES') or define('HCE_DRCE_EXEC_FIELD_FILES', 'files');
90 defined('HCE_DRCE_EXEC_FIELD_NODE') or define('HCE_DRCE_EXEC_FIELD_NODE', 'node');
91 defined('HCE_DRCE_EXEC_FIELD_TIME') or define('HCE_DRCE_EXEC_FIELD_TIME', 'time');
95 defined('HCE_DRCE_REQUEST_TYPE_FIELD') or define('HCE_DRCE_REQUEST_TYPE_FIELD', 'type');
96 defined('HCE_DRCE_REQUEST_ID_FIELD') or define('HCE_DRCE_REQUEST_ID_FIELD', '
id');
97 defined('HCE_DRCE_REQUEST_DATA_FIELD') or define('HCE_DRCE_REQUEST_DATA_FIELD', 'data');
98 defined('HCE_DRCE_REQUEST_SUBTASKS_FIELD') or define('HCE_DRCE_REQUEST_SUBTASKS_FIELD', 'subtasks');
102 defined('HCE_DRCE_REQUEST_TYPE_SET') or define('HCE_DRCE_REQUEST_TYPE_SET', 0);
103 defined('HCE_DRCE_REQUEST_TYPE_CHECK') or define('HCE_DRCE_REQUEST_TYPE_CHECK', 1);
104 defined('HCE_DRCE_REQUEST_TYPE_TERMINATE') or define('HCE_DRCE_REQUEST_TYPE_TERMINATE', 2);
105 defined('HCE_DRCE_REQUEST_TYPE_GET') or define('HCE_DRCE_REQUEST_TYPE_GET', 3);
106 defined('HCE_DRCE_REQUEST_TYPE_DELETE') or define('HCE_DRCE_REQUEST_TYPE_DELETE', 4);
110 defined('HCE_DRCE_REQUEST_TYPE_CHECK_SIMPLE') or define('HCE_DRCE_REQUEST_TYPE_CHECK_SIMPLE', 1);
111 defined('HCE_DRCE_REQUEST_TYPE_CHECK_EXTENDED') or define('HCE_DRCE_REQUEST_TYPE_CHECK_EXTENDED', 2);
115 defined('HCE_DRCE_EXEC_FIELD_TERMINATE_ALG') or define('HCE_DRCE_EXEC_FIELD_TERMINATE_ALG', 'alg');
116 defined('HCE_DRCE_EXEC_TERMINATE_ALG_SIMPLE') or define('HCE_DRCE_EXEC_TERMINATE_ALG_SIMPLE', 1);
117 defined('HCE_DRCE_EXEC_TERMINATE_ALG_EXTENDED') or define('HCE_DRCE_EXEC_TERMINATE_ALG_EXTENDED', 2);
121 defined('HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY') or define('HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY', 'delay');
122 defined('HCE_DRCE_EXEC_TERMINATE_DELAY_DEFAULT') or define('HCE_DRCE_EXEC_TERMINATE_DELAY_DEFAULT', 1000);
126 defined('HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT') or define('HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT', 'repeat');
127 defined('HCE_DRCE_EXEC_TERMINATE_REPEAT_DEFAULT') or define('HCE_DRCE_EXEC_TERMINATE_REPEAT_DEFAULT', 3);
131 defined('HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL') or define('HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL', 'sinal');
132 defined('HCE_DRCE_EXEC_TERMINATE_SIGNAL_DEFAULT') or define('HCE_DRCE_EXEC_TERMINATE_SIGNAL_DEFAULT', 15);
136 defined('HCE_DRCE_REQUEST_TYPE_FETCH_DELETE') or define('HCE_DRCE_REQUEST_TYPE_FETCH_DELETE', 1);
137 defined('HCE_DRCE_REQUEST_TYPE_FETCH_NOT_DELETE') or define('HCE_DRCE_REQUEST_TYPE_FETCH_NOT_DELETE', 2);
141 defined('HCE_DRCE_REQUEST_READ_FROM_FILE_STATEMENT') or define('HCE_DRCE_REQUEST_READ_FROM_FILE_STATEMENT', 'READ_FROM_FILE:');
155 case HCE_DRCE_REQUEST_TYPE_SET : {
156 $ret=array(HCE_DRCE_EXEC_FIELD_SESSION=>
157 array(HCE_DRCE_EXEC_FIELD_TYPE=>0,
158 HCE_DRCE_EXEC_FIELD_PORT=>0,
159 HCE_DRCE_EXEC_FIELD_USER=>
'',
160 HCE_DRCE_EXEC_FIELD_PASSWD=>
'',
161 HCE_DRCE_EXEC_FIELD_SHELL=>
'',
162 HCE_DRCE_EXEC_FIELD_ENV=>array(),
163 HCE_DRCE_EXEC_FIELD_HOME_DIR=>
'',
164 HCE_DRCE_EXEC_FIELD_TIMEOUT=>0,
165 HCE_DRCE_EXEC_FIELD_TMODE=>HCE_DRCE_EXEC_FIELD_TMODE_DEFAUL,
166 HCE_DRCE_EXEC_FIELD_TIME_MAX=>HCE_DRCE_EXEC_FIELD_TIME_MAX_DEFAULT,
167 HCE_DRCE_EXEC_FIELD_CLEANUP=>HCE_DRCE_EXEC_FIELD_CLEANUP_YES,
169 HCE_DRCE_EXEC_FIELD_COMMAND=>
'',
170 HCE_DRCE_EXEC_FIELD_INPUT=>
'',
171 HCE_DRCE_EXEC_FIELD_FILES=>array()
175 case HCE_DRCE_REQUEST_TYPE_CHECK : {
176 $ret=array(HCE_DRCE_EXEC_FIELD_TYPE=>HCE_DRCE_REQUEST_TYPE_CHECK_SIMPLE);
179 case HCE_DRCE_REQUEST_TYPE_TERMINATE : {
181 HCE_DRCE_EXEC_FIELD_TERMINATE_ALG=>HCE_DRCE_EXEC_TERMINATE_ALG_SIMPLE,
182 HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY=>HCE_DRCE_EXEC_TERMINATE_DELAY_DEFAULT,
183 HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT=>HCE_DRCE_EXEC_TERMINATE_REPEAT_DEFAULT,
184 HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL=>HCE_DRCE_EXEC_TERMINATE_SIGNAL_DEFAULT
188 case HCE_DRCE_REQUEST_TYPE_GET : {
190 HCE_DRCE_EXEC_FIELD_TYPE=>HCE_DRCE_REQUEST_TYPE_FETCH_DELETE
194 case HCE_DRCE_REQUEST_TYPE_DELETE : {
200 if(is_array($parameters)){
205 $json_in=@json_decode($parameters,
true);
206 if(is_array($json_in)){
209 case HCE_DRCE_REQUEST_TYPE_SET : {
211 if(isset($json_in[HCE_DRCE_EXEC_FIELD_SESSION])){
212 foreach($ret[HCE_DRCE_EXEC_FIELD_SESSION] as $key=>$val){
213 if(isset($json_in[HCE_DRCE_EXEC_FIELD_SESSION][$key])){
214 $ret[HCE_DRCE_EXEC_FIELD_SESSION][$key]=$json_in[HCE_DRCE_EXEC_FIELD_SESSION][$key];
219 if(isset($json_in[HCE_DRCE_EXEC_FIELD_COMMAND])){
220 $ret[HCE_DRCE_EXEC_FIELD_COMMAND]=$json_in[HCE_DRCE_EXEC_FIELD_COMMAND];
223 if(isset($json_in[HCE_DRCE_EXEC_FIELD_INPUT])){
224 $ret[HCE_DRCE_EXEC_FIELD_INPUT]=$json_in[HCE_DRCE_EXEC_FIELD_INPUT];
227 if(isset($json_in[HCE_DRCE_EXEC_FIELD_FILES]) && is_array($json_in[HCE_DRCE_EXEC_FIELD_FILES])){
228 foreach($json_in[HCE_DRCE_EXEC_FIELD_FILES] as $key=>$value){
229 if(is_array($value) && isset($value[HCE_DRCE_EXEC_FIELD_NAME]) && isset($value[HCE_DRCE_EXEC_FIELD_DATA]) && isset($value[HCE_DRCE_EXEC_FIELD_ACTION])){
230 if(strlen($value[HCE_DRCE_EXEC_FIELD_DATA])>15 && substr($value[HCE_DRCE_EXEC_FIELD_DATA], 0, 15)==HCE_DRCE_REQUEST_READ_FROM_FILE_STATEMENT){
232 $file_name=substr($value[HCE_DRCE_EXEC_FIELD_DATA], 15);
233 if(file_exists($file_name)){
234 $value[HCE_DRCE_EXEC_FIELD_DATA]=file_get_contents($file_name);
235 if(($value[HCE_DRCE_EXEC_FIELD_ACTION]+0) & 2147483648){
236 $value[HCE_DRCE_EXEC_FIELD_DATA]=base64_encode($value[HCE_DRCE_EXEC_FIELD_DATA]);
239 $value[HCE_DRCE_EXEC_FIELD_DATA]=
'File not found: '.$file_name;
242 $ret[HCE_DRCE_EXEC_FIELD_FILES][]=array(
243 HCE_DRCE_EXEC_FIELD_NAME=>$value[HCE_DRCE_EXEC_FIELD_NAME],
244 HCE_DRCE_EXEC_FIELD_DATA=>$value[HCE_DRCE_EXEC_FIELD_DATA],
245 HCE_DRCE_EXEC_FIELD_ACTION=>($value[HCE_DRCE_EXEC_FIELD_ACTION]+0)
251 if(isset($json_in[HCE_DRCE_EXEC_FIELD_LIMITS])){
252 foreach($ret[HCE_DRCE_EXEC_FIELD_LIMITS] as $key=>$val){
253 if(isset($json_in[HCE_DRCE_EXEC_FIELD_LIMITS][$key])){
254 $ret[HCE_DRCE_EXEC_FIELD_LIMITS][$key]=$json_in[HCE_DRCE_EXEC_FIELD_LIMITS][$key];
260 case HCE_DRCE_REQUEST_TYPE_CHECK : {
262 if(isset($json_in[HCE_DRCE_EXEC_FIELD_TYPE])){
263 $ret[HCE_DRCE_EXEC_FIELD_TYPE]=$json_in[HCE_DRCE_EXEC_FIELD_TYPE];
265 $ret[HCE_DRCE_EXEC_FIELD_TYPE]=HCE_DRCE_REQUEST_TYPE_CHECK_SIMPLE;
269 case HCE_DRCE_REQUEST_TYPE_TERMINATE : {
271 if(isset($json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_ALG])){
272 $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_ALG]=$json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_ALG];
274 $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_ALG]=HCE_DRCE_EXEC_TERMINATE_ALG_SIMPLE;
277 if(isset($json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY])){
278 $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY]=$json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY];
280 $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY]=HCE_DRCE_EXEC_TERMINATE_DELAY_DEFAULT;
283 if(isset($json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT])){
284 $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT]=$json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT];
286 $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT]=HCE_DRCE_EXEC_TERMINATE_REPEAT_DEFAULT;
289 if(isset($json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL])){
290 $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL]=$json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL];
292 $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL]=HCE_DRCE_EXEC_TERMINATE_SIGNAL_DEFAULT;
296 case HCE_DRCE_REQUEST_TYPE_GET : {
298 if(isset($json_in[HCE_DRCE_EXEC_FIELD_TYPE])){
299 $ret[HCE_DRCE_EXEC_FIELD_TYPE]=$json_in[HCE_DRCE_EXEC_FIELD_TYPE];
301 $ret[HCE_DRCE_EXEC_FIELD_TYPE]=HCE_DRCE_REQUEST_TYPE_FETCH_DELETE;
305 case HCE_DRCE_REQUEST_TYPE_DELETE : {
311 echo
'Parsing input json file error: '.json_last_error();
334 if(SPHINX_JSON_USE_BASE64){
335 $drce_admin_protocol=base64_encode($drce_admin_protocol);
338 $ret=json_encode(array(
339 HCE_HANDLER_COVER_FIELD_TYPE=>HCE_HANDLER_TYPE_DRCE,
340 HCE_HANDLER_COVER_FIELD_DATA=>$drce_admin_protocol,
341 HCE_HANDLER_COVER_FIELD_TTL=>$ttl
361 HCE_DRCE_REQUEST_TYPE_FIELD=>
$type,
363 HCE_DRCE_REQUEST_ID_FIELD=>$id,
364 HCE_DRCE_REQUEST_SUBTASKS_FIELD=>$subtasks
378 return json_decode($response_body,
true);
388 $ret=array(
'Error'=>
'',
'Data'=>null);
390 $tmp=json_decode($response_body,
true);
392 if(isset($tmp[HCE_DRCE_EXEC_FIELD_TYPE]) && $tmp[HCE_DRCE_EXEC_FIELD_TYPE]==HCE_HANDLER_TYPE_DRCE){
393 if(isset($tmp[HCE_DRCE_EXEC_FIELD_DATA])){
394 if(SPHINX_JSON_USE_BASE64){
395 $ret[
'Data']=json_decode(base64_decode($tmp[HCE_DRCE_EXEC_FIELD_DATA]),
true);
397 $ret[
'Data']=json_decode($tmp[HCE_DRCE_EXEC_FIELD_DATA],
true);
401 $ret[
'Error']=
'Structure error: `data` field not found!';
405 $ret[
'Error']=
'Structure error: `type` field not found or type is not a proper value!';
421 $json_in=json_decode($subtasks,
true);
426 if(is_array($json_in)){
427 foreach($json_in as $subtask_item){
429 if(is_array($subtask_item) && isset($subtask_item[HCE_DRCE_EXEC_FIELD_DATA]) && is_string($subtask_item[HCE_DRCE_EXEC_FIELD_DATA])){
430 if(strlen($subtask_item[HCE_DRCE_EXEC_FIELD_DATA])>15 && substr($subtask_item[HCE_DRCE_EXEC_FIELD_DATA], 0, 15)==HCE_DRCE_REQUEST_READ_FROM_FILE_STATEMENT){
431 $file_name=substr($subtask_item[HCE_DRCE_EXEC_FIELD_DATA], 15);
432 if(file_exists($file_name)){
433 $subtask_data=file_get_contents($file_name);
435 $subtask_data=
'file not found "'.$file_name.
'"';
438 $subtask_data=$subtask_item[HCE_DRCE_EXEC_FIELD_DATA];
440 $subtask_item[HCE_DRCE_EXEC_FIELD_DATA]=base64_encode($subtask_data);
441 $ret[]=$subtask_item;
442 }elseif(is_array($subtask_item) && isset($subtask_item[HCE_DRCE_EXEC_FIELD_DATA]) && is_array($subtask_item[HCE_DRCE_EXEC_FIELD_DATA])){
443 $subtask_data=json_encode($subtask_item[HCE_DRCE_EXEC_FIELD_DATA]);
444 $subtask_item[HCE_DRCE_EXEC_FIELD_DATA]=base64_encode($subtask_data);
447 if(is_array($subtask_item) && isset($subtask_item[HCE_DRCE_REQUEST_SUBTASKS_FIELD]) && is_array($subtask_item[HCE_DRCE_REQUEST_SUBTASKS_FIELD])){
450 $ret[]=$subtask_item;