-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
201 lines (175 loc) · 8.22 KB
/
Copy pathapi.php
File metadata and controls
201 lines (175 loc) · 8.22 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2025-2026 Tech1k <hello@tech1k.com>
// Free software under the GNU AGPL v3 or later. See LICENSE.
//
// Developer JSON API. Routed by .htaccess:
// GET /api/v1/ -> index (endpoint list)
// GET /api/v1/info -> per-faucet payout / window / balance [?network=<slug>]
// POST /api/v1/claim -> { "network": "<slug>", "address": "<addr>" }
//
// <slug> is the faucet's URL slug: xmr-stagenet, xmr-testnet, ltc-testnet,
// btc-testnet (the catalog 'href' without the leading slash).
//
// Keyless by design: the per-address/IP claim window and a faucet-wide daily cap
// are SHARED with the web faucets (same SQLite tables + reservation pattern), so
// the API can't be used to bypass limits or double-pay. Put Cloudflare WAF
// rate-limiting in front of /api/ as the outer layer. Off unless api_enabled.
$catalog = require __DIR__ . '/faucets.php';
$config = is_file(__DIR__ . '/config.php') ? require __DIR__ . '/config.php' : [];
require __DIR__ . '/faucet_lib.php';
header('Content-Type: application/json; charset=utf-8');
header('X-Content-Type-Options: nosniff');
/** Emit a JSON response and stop. */
function api_out(int $code, array $payload): void
{
global $config;
http_response_code($code);
// AGPL section 13: API callers never see the website footer that offers the
// source to browser users, so every response carries the source link.
if (!isset($payload['source'])) {
$payload['source'] = $config['source_url'] ?? 'https://github.com/Tech1k/cypherfaucet';
}
echo json_encode($payload, JSON_UNESCAPED_SLASHES);
exit;
}
// Opt-in per deployment: when the API is off, serve the normal styled 404 page
// (not a JSON error) so a fork that hasn't enabled it doesn't advertise the
// endpoint at all. (api.php already sent a JSON content-type; override it.)
if (($config['api_enabled'] ?? false) !== true) {
header('Content-Type: text/html; charset=utf-8');
require __DIR__ . '/404.php';
exit;
}
// Map URL slug -> [catalog key, entry]. The slug is the public identifier. Only
// engines this library knows how to serve are exposed; an unrecognised engine is
// skipped rather than mishandled as Core.
$bySlug = [];
foreach ($catalog as $key => $entry) {
$engine = $entry['engine'] ?? '';
if ($engine !== 'core' && $engine !== 'xmr') {
continue;
}
$slug = ltrim($entry['href'] ?? '', '/');
if ($slug !== '') {
$bySlug[$slug] = [$key, $entry];
}
}
$action = $_GET['_api'] ?? '';
$dbFile = getenv('FAUCET_DB') ?: __DIR__ . '/db/faucet.db';
// ---- GET /api/v1/ : index -----------------------------------------------
if ($action === 'index') {
api_out(200, [
'ok' => true,
'name' => 'CypherFaucet API',
'version' => 'v1',
'endpoints' => [
'GET /api/v1/info' => 'Faucet list with payout amount, claim window, and balance.',
'POST /api/v1/claim' => 'Body: {"network":"<slug>","address":"<addr>"}. Sends one payout.',
],
'networks' => array_keys($bySlug),
'docs' => (($config['source_url'] ?? 'https://github.com/Tech1k/cypherfaucet') . '#developer-api'),
]);
}
// ---- GET /api/v1/info ----------------------------------------------------
if ($action === 'info') {
$only = trim($_GET['network'] ?? '');
$cacheDir = dirname($dbFile);
$out = [];
foreach ($bySlug as $slug => [$key, $entry]) {
if ($only !== '' && $slug !== $only) {
continue;
}
$out[] = faucet_info_one($key, $entry, $config, $cacheDir);
}
if ($only !== '' && !$out) {
api_out(404, ['ok' => false, 'error' => 'unknown_network', 'message' => 'Unknown network slug.']);
}
api_out(200, ['ok' => true, 'faucets' => $out]);
}
// ---- POST /api/v1/claim --------------------------------------------------
if ($action === 'claim') {
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
header('Allow: POST');
api_out(405, ['ok' => false, 'error' => 'method_not_allowed', 'message' => 'Use POST.']);
}
// Accept a JSON body (preferred) or form-encoded params.
$body = json_decode((string) file_get_contents('php://input'), true);
if (!is_array($body)) {
$body = $_POST;
}
// Coerce to string: a JSON body with a non-string field (e.g. {"network":[]})
// would otherwise throw a TypeError on trim() and 500 instead of a clean 400.
$nf = $body['network'] ?? $body['coin'] ?? '';
$af = $body['address'] ?? '';
$slug = is_string($nf) ? trim($nf) : '';
$address = is_string($af) ? trim($af) : '';
if ($slug === '' || $address === '') {
api_out(400, ['ok' => false, 'error' => 'invalid_request', 'message' => '"network" and "address" are required.']);
}
if (!isset($bySlug[$slug])) {
api_out(400, ['ok' => false, 'error' => 'unknown_network', 'message' => 'Unknown network slug.']);
}
[$key, $entry] = $bySlug[$slug];
if (($config['enabled'][$key] ?? true) === false) {
api_out(503, ['ok' => false, 'error' => 'faucet_offline', 'message' => 'This faucet is offline.']);
}
try {
$db = new PDO("sqlite:$dbFile");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('PRAGMA busy_timeout = 5000');
} catch (PDOException $e) {
error_log("[faucet-api] DB connection failed for {$dbFile}: " . $e->getMessage());
api_out(503, ['ok' => false, 'error' => 'unavailable', 'message' => 'Faucet temporarily unavailable.']);
}
$ip = faucet_client_ip();
$ipTrusted = !empty($_SERVER['HTTP_CF_CONNECTING_IP']);
$cap = (int) ($config['api_daily_cap'] ?? 0);
$ipCap = (int) ($config['api_ip_daily_cap'] ?? 0);
$r = faucet_claim($db, $key, $entry, $config, $address, $ip, $ipTrusted, $cap, $ipCap);
switch ($r['status']) {
case 'ok':
$resp = [
'ok' => true,
'network' => $slug,
'currency' => $r['currency'],
'amount' => number_format((float) $r['amount'], 8, '.', ''),
'txid' => $r['txid'],
];
if (!empty($r['tx_key'])) {
$resp['tx_key'] = $r['tx_key'];
}
api_out(200, $resp);
case 'invalid_address':
api_out(400, ['ok' => false, 'error' => 'invalid_address', 'message' => 'Not a valid address for this network.']);
case 'rate_limited':
if (!empty($r['retry_after'])) {
header('Retry-After: ' . (int) $r['retry_after']);
}
api_out(429, [
'ok' => false,
'error' => 'rate_limited',
'message' => 'You have already claimed within the current window.',
'retry_after' => $r['retry_after'] ?? null,
'next_claim' => $r['next_claim'] ?? null,
]);
case 'ip_rate_limited':
api_out(429, ['ok' => false, 'error' => 'ip_rate_limited', 'message' => 'This IP has reached its daily claim budget. Try again later or from another host.']);
case 'daily_cap':
api_out(429, ['ok' => false, 'error' => 'daily_cap', 'message' => 'The faucet has reached its daily limit. Try again later.']);
case 'faucet_empty':
api_out(409, ['ok' => false, 'error' => 'faucet_empty', 'message' => 'The faucet is out of spendable coins right now.']);
case 'node_busy':
header('Retry-After: 30');
api_out(503, ['ok' => false, 'error' => 'node_busy', 'message' => 'The node is busy; no coins were sent. Try again shortly.']);
case 'send_failed':
header('Retry-After: 30');
api_out(503, ['ok' => false, 'error' => 'send_failed', 'message' => 'Could not complete the send. Try again shortly.']);
case 'faucet_unavailable':
api_out(503, ['ok' => false, 'error' => 'unavailable', 'message' => 'Faucet temporarily unavailable.']);
default:
api_out(500, ['ok' => false, 'error' => 'internal_error', 'message' => 'Something went wrong. If it persists, contact me.']);
}
}
// Unknown action / bare api.php hit.
api_out(404, ['ok' => false, 'error' => 'not_found']);