This commit is contained in:
2025-11-07 09:47:03 +01:00
parent 646e574059
commit 55620c52d4
22 changed files with 1835 additions and 2 deletions

22
web/api/config_get.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
require __DIR__ . '/../lib/app.php';
require __DIR__ . '/../lib/torctl.php';
header('Content-Type: application/json');
$parsed = read_torpanel_conf();
$ui = torrc_to_ui($parsed);
$rate_mb = round($ui['rate_mbps'] / 8, 2);
$burst_mb = round($ui['burst_mbps'] / 8, 2);
$torrc = [
'Nickname' => $ui['nickname'],
'ContactInfo' => $ui['contact'],
'ORPort' => (string)$ui['orport'],
'BandwidthRate' => rtrim(rtrim(number_format($rate_mb, 2, '.', ''), '0'), '.') . ' MB',
'BandwidthBurst' => rtrim(rtrim(number_format($burst_mb, 2, '.', ''), '0'), '.') . ' MB',
'AccountingMax' => $ui['cap_gb'] . ' GB',
'AccountingStart' => 'month ' . $ui['acc_day'] . ' 00:00',
];
echo json_encode(['ok' => true] + $ui + $torrc);

12
web/api/config_set.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
require __DIR__ . '/../lib/app.php';
require __DIR__ . '/../lib/torctl.php';
auth_require();
header('Content-Type: application/json');
$in = json_decode(file_get_contents('php://input'), true);
if (!is_array($in)) { echo json_encode(['ok'=>false,'error'=>'bad json']); exit; }
$ok = config_apply($in);
echo json_encode(['ok'=>$ok]);

23
web/api/now.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
require __DIR__ . '/../lib/app.php'; auth_require();
require __DIR__ . '/../lib/torctl.php';
header('Content-Type: application/json');
try {
$info = torctl([
"GETINFO traffic/read",
"GETINFO traffic/written",
"GETINFO status/circuit-established",
"GETINFO version",
"GETINFO status/bootstrap-phase"
]);
echo json_encode([
"ok" => true,
"read" => (int)($info["traffic/read"] ?? 0),
"written" => (int)($info["traffic/written"] ?? 0),
"circuits" => (($info["status/circuit-established"] ?? "0") === "1"),
"version" => ($info["version"] ?? "unknown"),
"bootstrap" => ($info["status/bootstrap-phase"] ?? "")
]);
} catch (Throwable $e) {
echo json_encode(["ok"=>false,"error"=>$e->getMessage()]);
}

62
web/api/reach.php Normal file
View File

@@ -0,0 +1,62 @@
<?php
require __DIR__ . '/../lib/app.php'; auth_require();
require __DIR__ . '/../lib/torctl.php';
header('Content-Type: application/json');
try {
$cfg = read_torpanel_conf();
$orport = (int)($cfg['ORPort'] ?? 9001);
$info = torctl(["GETINFO fingerprint","GETINFO address"]);
$fp_raw = trim($info['fingerprint'] ?? '');
$fingerprint = strtoupper(str_replace([' ', "\n", "\r", "\t"], '', $fp_raw));
$tor_addr = trim($info['address'] ?? '');
$lan_ip = trim(shell_exec('hostname -I 2>/dev/null'));
$lan_ip = $lan_ip ? preg_split('/\s+/', $lan_ip)[0] : '';
$onionoo = [
'found' => false, 'running' => false, 'flags' => [],
'last_seen' => null, 'nickname' => ($cfg['Nickname'] ?? 'RaspberryRelay'),
'or_addresses' => [], 'country' => null
];
if ($fingerprint !== '') {
$url = 'https://onionoo.torproject.org/details?lookup=' . urlencode($fingerprint);
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 6,
CURLOPT_USERAGENT => 'TorPanel/1.0 (+relay reachability check)',
]);
$res = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($res !== false && $http === 200) {
$j = json_decode($res, true);
if (!empty($j['relays']) && count($j['relays']) > 0) {
$r = $j['relays'][0];
$onionoo['found'] = true;
$onionoo['running'] = (bool)($r['running'] ?? false);
$onionoo['flags'] = $r['flags'] ?? [];
$onionoo['last_seen'] = $r['last_seen'] ?? null;
$onionoo['nickname'] = $r['nickname'] ?? $onionoo['nickname'];
$onionoo['or_addresses']= $r['or_addresses'] ?? [];
$onionoo['country'] = $r['country'] ?? null;
}
}
}
echo json_encode([
'ok' => true,
'fingerprint' => $fingerprint,
'nickname' => $onionoo['nickname'],
'orport' => $orport,
'tor_address' => $tor_addr,
'lan_ip' => $lan_ip,
'onionoo' => $onionoo,
]);
} catch (Throwable $e) {
echo json_encode(['ok'=>false,'error'=>$e->getMessage()]);
}

7
web/api/stats.php Normal file
View File

@@ -0,0 +1,7 @@
<?php
require __DIR__ . '/../lib/app.php'; auth_require();
header('Content-Type: application/json');
$path = "/var/lib/torpanel/stats.json";
if (!file_exists($path)) { echo json_encode(["data"=>[]]); exit; }
$raw = file_get_contents($path);
echo $raw ?: json_encode(["data"=>[]]);