rob-revisited/tools/embed/tests/fake-playground.php

55 lines
2.6 KiB
PHP

<?php
// A fake playground for test.sh: checks the signatures like App\Service\EmbedSignature of jumphp-playground, answers like its /api/embed/.
$secret = 'jes_test-secret';
$uri = $_SERVER['REQUEST_URI'];
[$path, $query] = array_pad(explode('?', $uri, 2), 2, '');
$body = (string)file_get_contents('php://input');
$time = (string)($_SERVER['HTTP_X_JUMPHP_TIME'] ?? '');
$expected = hash_hmac('sha256', $_SERVER['REQUEST_METHOD'] . "\n$path\n$query\n$time\n" . hash('sha256', $body), $secret);
file_put_contents(sys_get_temp_dir() . '/fake-hits.log', $_SERVER['REQUEST_METHOD'] . " $uri\n", FILE_APPEND);
header('X-Robots-Tag: noindex');
header('Set-Cookie: playground=1');
if (($_SERVER['HTTP_X_JUMPHP_SITE'] ?? '') !== 'es_00000000000000000000' || abs(time() - (int)$time) > 300 || !hash_equals($expected, (string)($_SERVER['HTTP_X_JUMPHP_SIGNATURE'] ?? ''))) {
http_response_code(401);
header('Content-Type: application/json');
echo '{"error":"wrong signature"}';
return;
}
$big = str_repeat('0123456789abcdef', 16384); // 256 KB
switch (true) {
case $path === '/api/embed/demo/block/login':
header('ETag: "b1"');
header('Cache-Control: public, max-age=300');
if (($_SERVER['HTTP_IF_NONE_MATCH'] ?? '') === '"b1"') {
http_response_code(304);
return;
}
header('Content-Type: text/html; charset=utf-8');
echo '<form action="https://partner.test/chat/">login</form>';
return;
case $path === '/api/embed/demo/app/':
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: no-cache');
echo '<!doctype html><title>page</title>' . $_SERVER['REQUEST_METHOD'] . ' query=' . $query . ' body=' . $body;
return;
case $path === '/api/embed/demo/app/engine/1.0.0/big.bin':
header('Content-Type: application/octet-stream');
header('Cache-Control: public, max-age=31536000, immutable');
header('ETag: "big"');
header('Accept-Ranges: bytes');
if (preg_match('/bytes=(\d+)-(\d+)/', $_SERVER['HTTP_RANGE'] ?? '', $m)) {
http_response_code(206);
header("Content-Range: bytes $m[1]-$m[2]/" . strlen($big));
echo substr($big, (int)$m[1], $m[2] - $m[1] + 1);
return;
}
header('Content-Length: ' . strlen($big));
echo $big;
return;
case $path === '/api/embed/demo/tcp/ticket':
header('Content-Type: application/json');
echo json_encode(['ip' => $_SERVER['HTTP_X_JUMPHP_CLIENT_IP'] ?? null, 'body' => json_decode($body, true)]);
return;
}
http_response_code(404);
echo 'not found';