Renamed and logged api:sync

This commit is contained in:
xdrm-brackets 2017-01-27 20:56:02 +01:00
parent e69f2b58f3
commit aa4d378e80
4 changed files with 38 additions and 121 deletions

View File

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

View File

@ -1,109 +0,0 @@
<?php
require_once __DIR__.'/../../include/const';
function api_request(){
/* [1] Fetch & generate useful data
=========================================================*/
/* (1) Fetch target url */
$url = @file_get_contents(URL_CONF);
if( $url === false )
return 127;
/* (2) Fetch cyclic hash */
$hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash');
if( strlen($hash) != 128 )
return 127;
/* (3) Try new hash if available */
$new = syscall(SOURCE_DIR.'/lib/cyclic-hash/new');
if( $new === false )
return 127;
/* (4) Decrement the hash */
$decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr');
if( $decr === false )
return 127;
/* (5) Generate the multipart boundary */
$boundary = 'boundary--'.hash('sha512', uniqid()).'--boundary';
/* (6) Fetch data */
$data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetchdata'));
if( is_null($data) )
$data = "{}";
/* [2] Create httpRequest basis
=========================================================*/
/* (1) Set URL */
$curl = curl_init($url);
/* (2) Set HTTP method -> POST */
curl_setopt($curl, CURLOPT_POST, true);
/* [3] Manage post data
=========================================================*/
/* (1) Set post data */
$postarray = [
'token' => $hash,
'data' => $data
];
/* (2) Add renew if renew */
if( strlen($new) == 128 )
$postarray['renew'] = $new;
/* (3) Parse postfiels to multipart format */
$postraw = "--$boundary";
foreach($postarray as $postkey=>$postvalue)
$postraw .= "\r\ncontent-disposition: form-data; name=\"$postkey\"\r\n\r\n$postvalue\r\n--$boundary";
$postraw .= "--";
/* (4) Set postdata raw to curl */
curl_setopt($curl, CURLOPT_POSTFIELDS, $postraw);
/* [4] Manage headers
=========================================================*/
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Content-Type: multipart/form-data; boundary=$boundary",
"Content-Length: ".strlen($postraw)
]);
/* [5] Send and catch request response
=========================================================*/
/* (1) Send and catch response */
$response = curl_exec($curl);
/* (2) Close request */
curl_close($curl);
/* (3) Return response as result */
if( $response === false )
return 127;
return $response;
}
echo api_request();
?>

View File

@ -2,42 +2,52 @@
require_once __DIR__.'/../../include/const'; require_once __DIR__.'/../../include/const';
function api_sync(){ function api_request(){
/* [1] Fetch & generate useful data /* [1] Fetch & generate useful data
=========================================================*/ =========================================================*/
/* (1) Fetch target url */ /* (1) Fetch target url */
$url = @file_get_contents(URL_CONF); $url = @file_get_contents(URL_CONF);
if( $url === false ) if( $url === false ){
slog("Cannot find server's api url", 'api:sync');
return 127; return 127;
}
/* (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');
return 127; return 127;
}
/* (3) Try new hash if available */ /* (3) Try new hash if available */
$new = syscall(SOURCE_DIR.'/lib/cyclic-hash/new'); $new = syscall(SOURCE_DIR.'/lib/cyclic-hash/new');
if( $new === false ) if( $new === false ){
slog("cyclic-hash:new returned $new EXIT_STATUS", 'api:sync');
return 127; return 127;
}
/* (4) Decrement the hash */ /* (4) Decrement the hash */
$decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr'); $decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr');
if( $decr === false ) if( $decr === false ){
slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:sync');
return 127; return 127;
}
/* (5) Generate the multipart boundary */ /* (5) Generate the multipart boundary */
$boundary = 'boundary--'.hash('sha512', uniqid()).'--boundary'; $boundary = 'boundary--'.hash('sha512', uniqid()).'--boundary';
/* (6) Fetch data */ /* (6) Fetch data */
$data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetchdata')); $data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetch'));
if( is_null($data) ) if( is_null($data) ){
slog("api:fetch returned unreadable content", 'api:sync');
$data = "{}"; $data = "{}";
}
@ -95,11 +105,28 @@
curl_close($curl); curl_close($curl);
/* (3) Return response as result */ /* (3) Return response as result */
return $response; if( $response === false ){
slog("Request error", 'api:sync');
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');
/* (3) Return state */
return ($deploy === true);
} }
echo api_sync(); echo api_request();

View File

@ -7,10 +7,12 @@
#define('LOG_DIR', ROOT_DIR.'/log'); #define('LOG_DIR', ROOT_DIR.'/log');
#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('TMP_DIR', ROOT_DIR.'/tmp');
define('ROOT_DIR', '/home/xdrm-brackets/SANDBOX/sats-local'); define('ROOT_DIR', '/home/xdrm-brackets/SANDBOX/sats-local');
define('LOG_DIR', ROOT_DIR.'/log'); define('LOG_DIR', ROOT_DIR.'/log');
define('CONF_DIR', ROOT_DIR.'/conf'); define('CONF_DIR', ROOT_DIR.'/conf');
define('TMP_DIR', ROOT_DIR.'/tmp');
define('SOURCE_DIR', '/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats'); define('SOURCE_DIR', '/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats');
# CONFIGURATION FILES # CONFIGURATION FILES