Require shared auth for wake requests

This commit is contained in:
seo
2026-06-12 15:10:26 +09:00
parent 77f2b33963
commit 7f23e89a2a
2 changed files with 55 additions and 67 deletions
+34 -67
View File
@@ -1,67 +1,34 @@
<?php
header('Content-Type: application/json; charset=utf-8');
// Home Assistant 설정
$HA_URL = "https://ha.seoul.chaegeon.com/api"; // 또는 http://IP:8123/api
$HA_TOKEN = "REDACTED_HA_TOKEN"; // HA 장기 토큰
$ENTITY = "switch.keompyuteo"; // 제어할 스위치 엔티티명
function haRequest($method, $endpoint, $data = null) {
global $HA_URL, $HA_TOKEN;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $HA_URL . $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
$headers = [
"Authorization: Bearer $HA_TOKEN",
"Content-Type: application/json"
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
/*
--------------------------------------------
1) 상태 확인 요청 (/api.php?status=1)
--------------------------------------------
*/
if (isset($_GET['status'])) {
$state = haRequest("GET", "/states/$ENTITY");
if (!$state || !isset($state["state"])) {
echo json_encode(["result" => "error"]);
exit;
}
echo json_encode([
"result" => "ok",
"state" => $state["state"] // "on" or "off"
]);
exit;
}
/*
--------------------------------------------
2) 스위치 켜기 요청 (POST)
--------------------------------------------
*/
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$res = haRequest("POST", "/services/switch/turn_on", [
"entity_id" => $ENTITY
]);
echo json_encode(["result" => "sent"]);
exit;
}
echo json_encode(["result" => "invalid_request"]);
<?php
require_once __DIR__ . '/../common/auth.php';
require_once __DIR__ . '/../common/ha.php';
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store');
custom_require_auth();
$config = custom_config();
$entity = $config['entities']['wake_switch'];
if (isset($_GET['status'])) {
$state = custom_ha_state('seoul_wol', $entity);
if (!$state || !isset($state['state'])) {
custom_json(['result' => 'error'], 502);
}
custom_json([
'result' => 'ok',
'state' => $state['state'],
]);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$res = custom_ha_request('seoul_wol', 'POST', '/services/switch/turn_on', [
'entity_id' => $entity,
]);
custom_json(['result' => $res === null ? 'error' : 'sent'], $res === null ? 502 : 200);
}
custom_json(['result' => 'invalid_request'], 400);