Spaces:
Sleeping

File size: 3,463 Bytes
015c315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
 * CorpusDB API Proxy
 *
 * Place this file on YOUR hosting (e.g., api.php)
 * Users connect to YOUR domain β€” backend URL is hidden.
 */

// CORS headers β€” restrict to your domain in production
$allowed_origins = ['https://corpusdb.66ghz.com', 'http://localhost:8000', 'http://localhost:3000'];
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
if (in_array($origin, $allowed_origins)) {
    header('Access-Control-Allow-Origin: ' . $origin);
}
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, X-Api-Key, X-Secret-Key');
header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit;
}

// ── CONFIG ────────────────────────────────────────────────────────────────────
define('BACKEND_URL', 'https://mrsavage1-corpusdb.hf.space');

// Allowlist of permitted endpoint prefixes (prevents SSRF)
$ALLOWED_PREFIXES = [
    'api/cdb/',
    'api/databases',
    'api/tables',
    'api/schema/',
    'api/rows/',
    'api/sql',
    'api/health',
    'api/ai/',
    'api/query-builder/',
    'api/visualize/',
    'api/performance/',
    'api/projects',
    'api/templates',
    'api/settings/',
];
// ─────────────────────────────────────────────────────────────────────────────

$method   = $_SERVER['REQUEST_METHOD'];
$endpoint = isset($_GET['endpoint']) ? trim($_GET['endpoint'], '/') : '';
$headers  = getallheaders();

// Validate API keys
$apiKey    = $headers['X-Api-Key']    ?? ($headers['x-api-key']    ?? '');
$secretKey = $headers['X-Secret-Key'] ?? ($headers['x-secret-key'] ?? '');

if (empty($apiKey) || empty($secretKey)) {
    http_response_code(401);
    echo json_encode(['ok' => false, 'error' => 'API keys required']);
    exit;
}

// Validate endpoint against allowlist (prevents SSRF)
$allowed = false;
foreach ($ALLOWED_PREFIXES as $prefix) {
    if (strpos($endpoint, $prefix) === 0) {
        $allowed = true;
        break;
    }
}

if (!$allowed || strpos($endpoint, '..') !== false) {
    http_response_code(403);
    echo json_encode(['ok' => false, 'error' => 'Endpoint not allowed']);
    exit;
}

// Forward request to backend
$body = file_get_contents('php://input');
$url  = BACKEND_URL . '/' . $endpoint;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'X-Api-Key: ' . $apiKey,
    'X-Secret-Key: ' . $secretKey,
]);

if ($body && in_array($method, ['POST', 'PUT', 'PATCH'])) {
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);

if ($response === false) {
    http_response_code(502);
    echo json_encode(['ok' => false, 'error' => 'Backend unreachable: ' . $curlError]);
    exit;
}

http_response_code($httpCode);
echo $response;