WOL 전용 토큰 API 추가
This commit is contained in:
+47
-3
@@ -9,7 +9,11 @@ if (!$controlApiLibrary && session_status() !== PHP_SESSION_ACTIVE) {
|
||||
|
||||
require_once __DIR__ . '/../config/config.php';
|
||||
|
||||
if (!$controlApiLibrary && !signed_in()) {
|
||||
$controlApiAction = $_GET['action'] ?? $_POST['action'] ?? 'status';
|
||||
$controlApiTokenScope = in_array($controlApiAction, ['wake_lan', 'wake_lan_status'], true) ? 'wake_lan' : '';
|
||||
$controlApiTokenAuthenticated = $controlApiTokenScope !== '' && control_api_token_valid($controlApiTokenScope);
|
||||
|
||||
if (!$controlApiLibrary && !$controlApiTokenAuthenticated && !signed_in()) {
|
||||
json_out([
|
||||
'ok' => false,
|
||||
'error' => 'login_required',
|
||||
@@ -2862,6 +2866,35 @@ function send_wake_on_lan(string $mac, string $broadcast = '192.168.50.255', int
|
||||
];
|
||||
}
|
||||
|
||||
function wake_lan_status(string $mac): array
|
||||
{
|
||||
$mac = strtolower(trim($mac));
|
||||
if (!wifi_valid_mac($mac)) {
|
||||
throw new InvalidArgumentException('bad_mac');
|
||||
}
|
||||
|
||||
$leases = dnsmasq_leases();
|
||||
$activeLanMacs = array_fill_keys(lan_fdb_macs(), true);
|
||||
$observedLanMacs = array_fill_keys(observed_lan_macs($leases), true);
|
||||
$known = isset($leases[$mac]) || isset($activeLanMacs[$mac]) || isset($observedLanMacs[$mac]);
|
||||
|
||||
if (!$known) {
|
||||
throw new InvalidArgumentException('unknown_dhcp_client');
|
||||
}
|
||||
|
||||
$lease = $leases[$mac] ?? [];
|
||||
$isOnline = isset($activeLanMacs[$mac]);
|
||||
|
||||
return [
|
||||
'mac' => $mac,
|
||||
'ip' => $lease['ip'] ?? null,
|
||||
'hostname' => $lease['hostname'] ?? null,
|
||||
'state' => $isOnline ? 'on' : 'off',
|
||||
'presence' => $isOnline ? 'fdb' : (isset($observedLanMacs[$mac]) ? 'observed' : 'lease'),
|
||||
'wol_available' => !$isOnline,
|
||||
];
|
||||
}
|
||||
|
||||
function wifi_time_ms(string $value): int
|
||||
{
|
||||
if (preg_match('/(\d+)/', $value, $m)) {
|
||||
@@ -3630,9 +3663,11 @@ function collect_snapshot(bool $applyFan = true): array
|
||||
|
||||
function control_api_dispatch(): void
|
||||
{
|
||||
$action = $_GET['action'] ?? $_POST['action'] ?? 'status';
|
||||
global $controlApiAction, $controlApiTokenAuthenticated;
|
||||
|
||||
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') {
|
||||
$action = $controlApiAction;
|
||||
|
||||
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST' && !$controlApiTokenAuthenticated) {
|
||||
require_csrf();
|
||||
}
|
||||
|
||||
@@ -3839,6 +3874,15 @@ function control_api_dispatch(): void
|
||||
]);
|
||||
}
|
||||
|
||||
if ($action === 'wake_lan_status') {
|
||||
json_out([
|
||||
'ok' => true,
|
||||
'data' => [
|
||||
'wake' => wake_lan_status((string)($_GET['mac'] ?? $_POST['mac'] ?? '')),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($action === 'collect') {
|
||||
json_out([
|
||||
'ok' => true,
|
||||
|
||||
Reference in New Issue
Block a user