This commit is contained in:
xdrm-brackets 2017-02-19 12:32:47 +01:00
commit 75d4202eec
6 changed files with 252 additions and 25 deletions

3
lib/api/init Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/init.php;

40
lib/api/source/deploy.php Executable file
View File

@ -0,0 +1,40 @@
<?php
require_once __DIR__.'/../../include/php/const';
function api_deploy(){
/* [1] Fetch api response
=========================================================*/
/* (1) Try to read response */
$file_r = @file_get_contents(TMP_DIR.'/api.response');
/* (2) Check response */
if( $file_r === false )
return 127;
/* (3) Try to parse response */
$arr_r = json_decode($file_r, true);
/* (4) Check parse error */
if( is_null($arr_r) )
return 127;
return 0;
}
/* [1] Check $1 argument
=========================================================*/
if( $argc < 2 || !in_array($argv[1], 'init', 'sync' ){
echo 127;
die();
}
echo api_deploy($argv[1]);
?>

153
lib/api/source/init.php Executable file
View File

@ -0,0 +1,153 @@
<?php
require_once __DIR__.'/../../include/php/const';
function api_init(){
/* [1] Fetch & generate useful data
=========================================================*/
/* (1) Fetch target url */
$url = @file_get_contents(URL_CONF);
if( $url === false ){
slog("Cannot find server's api url", 'api:init','update');
return 127;
}
// remove unwanted spaces or linebreaks
$url = preg_replace('/\s/', '', $url).'/init';
/* (2) Fetch machine id */
$id_machine = @file_get_contents(ID_CONF);
if( $id_machine === false ){
slog("Cannot find machine id", 'api:init','update');
return 127;
}
/* (3) Fetch cyclic hash */
$hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash');
if( strlen($hash) != 128 ){
slog("Wrong hash length (".strlen($hash).")", 'api:init','update');
return 127;
}
/* (4) Try new hash if available */
$new = syscall(SOURCE_DIR.'/lib/cyclic-hash/new');
if( $new === false ){
slog("cyclic-hash:new returned $new EXIT_STATUS", 'api:init','update');
return 127;
}
/* (5) Fetch machine unlock code */
$unlock_code = @file_get_contents(UNLOCK_CODE);
if( $unlock_code === false ){
slog("Cannot find unlock code", 'api:init','update');
return 127;
}
$unlock_code = preg_replace('/\s/', '', $unlock_code);
/* [2] Create httpRequest base
=========================================================*/
/* (1) Set URL */
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
/* (2) Set HTTP method -> POST */
curl_setopt($curl, CURLOPT_POST, true);
/* (3) Additional options */
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
/* [3] Manage post data
=========================================================*/
/* (1) Set post data */
$postarray = [
'id_machine' => $id_machine,
'token' => $hash,
'unlock' => $unlock_code
];
/* (2) Add new hash if renew */
if( strlen($new) == 128 )
$postarray['token'] = $new;
/* (3) Set postdata raw to curl */
curl_setopt($curl, CURLOPT_POSTFIELDS, $postarray);
/* [4] Manage authentication
=========================================================*/
/* (1) Fetch warehouse token */
$wh_tok = @file_get_contents(WAREHOUSE_TOK);
if( $wh_tok === false ){
slog("Cannot find warehouse token", 'api:init','update');
return 127;
}
$wh_tok = preg_replace('/\s/', '', $wh_tok);
/* (2) Set Auth header digest */
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Authorization: Digest $wh_tok"
]);
/* [5] Send and catch request response
=========================================================*/
/* (1) Send and catch response */
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
/* (2) Close request */
curl_close($curl);
/* (3) Return response as result */
if( $response === false ){
slog("Request error", 'api:init','update');
return 127;
}
/* [6] Decrement cyclic-hash so request has ran successfully
=========================================================*/
/* (1) Decrement the hash */
$decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr');
if( $decr === false ){
slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:init','update');
return 127;
}
/* [6] Response management
=========================================================*/
/* (1) Writes request to temporary pipe */
file_put_contents(TMP_DIR.'/api.response', $response);
/* (2) Manage response deployement */
$deploy = syscall(SOURCE_DIR.'/lib/api/deploy init');
/* (3) Return state */
return ($deploy === true) ? 0 : 127;
}
echo api_init();
?>

View File

@ -15,13 +15,14 @@
} }
// remove unwanted spaces or linebreaks // remove unwanted spaces or linebreaks
$url = preg_replace('/\s/', '', $url); $url = preg_replace('/\s/', '', $url).'/sync';
/* (2) Fetch cyclic hash */ /* (2) Fetch cyclic hash */
$hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash'); $hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash');
if( strlen($hash) != 128 ){ if( strlen($hash) != 128 ){
slog("Wrong cyclic-hash:hash hash length (".strlen($hash).")", 'api:sync','update'); slog("Wrong hash length (".strlen($hash).")", 'api:sync','update');
return 127; return 127;
} }
@ -33,10 +34,7 @@
return 127; return 127;
} }
/* (4) Generate the multipart boundary */ /* (4) Fetch data */
$boundary = 'boundary--'.hash('sha512', uniqid()).'--boundary';
/* (5) Fetch data */
$data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetch')); $data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetch'));
if( is_null($data) ){ if( is_null($data) ){
@ -47,7 +45,7 @@
/* [2] Create httpRequest basis /* [2] Create httpRequest base
=========================================================*/ =========================================================*/
/* (1) Set URL */ /* (1) Set URL */
$curl = curl_init(); $curl = curl_init();
@ -56,6 +54,9 @@
/* (2) Set HTTP method -> POST */ /* (2) Set HTTP method -> POST */
curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POST, true);
/* (3) Additional options */
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
/* [3] Manage post data /* [3] Manage post data
@ -74,14 +75,32 @@
curl_setopt($curl, CURLOPT_POSTFIELDS, $postarray); curl_setopt($curl, CURLOPT_POSTFIELDS, $postarray);
/* [4] Send and catch request response /* [4] Manage authentication
=========================================================*/
/* (1) Fetch warehouse token */
$wh_tok = @file_get_contents(WAREHOUSE_TOK);
if( $wh_tok === false ){
slog("Cannot find warehouse token", 'api:sync','update');
return 127;
}
$wh_tok = preg_replace('/\s/', '', $wh_tok);
/* (2) Set Auth header digest */
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Authorization: Digest $wh_tok"
]);
/* [5] Send and catch request response
=========================================================*/ =========================================================*/
/* (1) Send and catch response */ /* (1) Send and catch response */
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl); $response = curl_exec($curl);
/* (2) Close request */ /* (2) Close request */
//curl_close($curl); curl_close($curl);
/* (3) Return response as result */ /* (3) Return response as result */
if( $response === false ){ if( $response === false ){
@ -90,7 +109,8 @@
} }
/* [5] Decrement cyclic-hash so request has ran successfully
/* [6] Decrement cyclic-hash so request has ran successfully
=========================================================*/ =========================================================*/
/* (1) Decrement the hash */ /* (1) Decrement the hash */
$decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr'); $decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr');
@ -101,20 +121,27 @@
} }
/* [6] Response management
/* [5] Response management
=========================================================*/ =========================================================*/
/* (1) Writes request to temporary pipe */ /* (1) Try to json_decode response */
$response_arr = json_decode($response, true);
// if cannot, abort
if( is_null($response_arr) )
return 127;
/* (2) Check response error */
if( !isset($response_arr['error']) || $response_arr['error'] != 0 )
return 127;
/* (3) Writes request to temporary pipe */
file_put_contents(TMP_DIR.'/api.response', $response); file_put_contents(TMP_DIR.'/api.response', $response);
/* (2) Manage response deployement */ /* (4) Manage response deployement */
$deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); $deploy = syscall(SOURCE_DIR.'/lib/api/deploy sync');
/* (3) Return state */ /* (5) Return state */
return ($deploy === true); return ($deploy === true) ? 0 : 127;
} }

View File

@ -19,6 +19,8 @@ export BRANCH_CONF="$CONF_DIR/machine.branch";
export ID_CONF="$CONF_DIR/machine.id"; export ID_CONF="$CONF_DIR/machine.id";
export URL_CONF="$CONF_DIR/api.url"; export URL_CONF="$CONF_DIR/api.url";
export AUTH_CONF="$CONF_DIR/auth.list"; export AUTH_CONF="$CONF_DIR/auth.list";
export WAREHOUSE_TOK="$CONF_DIR/warehouse.token";
export UNLOCK_CODE="$CONF_DIR/machine.unlock";
# LOG FILES # LOG FILES
export BOOT_LOG="$LOG_DIR/boot.log"; export BOOT_LOG="$LOG_DIR/boot.log";

View File

@ -9,7 +9,7 @@
define('DATA_DIR', ROOT_DIR.'/data'); define('DATA_DIR', ROOT_DIR.'/data');
define('CONF_DIR', ROOT_DIR.'/conf'); define('CONF_DIR', ROOT_DIR.'/conf');
define('SOURCE_DIR', ROOT_DIR.'/source'); define('SOURCE_DIR', ROOT_DIR.'/source');
#define('SOURCE_DIR', '/home/xdrm-brackets/Desktop/git.xdr/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats'); #define('SOURCE_DIR', '/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats');
define('TMP_DIR', ROOT_DIR.'/tmp'); define('TMP_DIR', ROOT_DIR.'/tmp');
# CONFIGURATION FILES # CONFIGURATION FILES
@ -19,6 +19,8 @@
define('ID_CONF', CONF_DIR.'/machine.id'); define('ID_CONF', CONF_DIR.'/machine.id');
define('URL_CONF', CONF_DIR.'/api.url'); define('URL_CONF', CONF_DIR.'/api.url');
define('AUTH_CONF', CONF_DIR.'/auth.list'); define('AUTH_CONF', CONF_DIR.'/auth.list');
define('WAREHOUSE_TOK', CONF_DIR.'/warehouse.token');
define('UNLOCK_CODE', CONF_DIR.'/machine.unlock');
# LOG FILES # LOG FILES
define('BOOT_LOG', LOG_DIR.'/boot.log'); define('BOOT_LOG', LOG_DIR.'/boot.log');