From 7f23e89a2ac58f61f16a35ff23afbe3a3e923375 Mon Sep 17 00:00:00 2001 From: seo Date: Fri, 12 Jun 2026 15:10:26 +0900 Subject: [PATCH] Require shared auth for wake requests --- README.md | 21 ++++++++++++ api.php | 101 ++++++++++++++++++------------------------------------ 2 files changed, 55 insertions(+), 67 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e8784be --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Wake On LAN Proxy + +Home Assistant의 PC 전원 스위치 엔티티를 켜는 얇은 프록시 API입니다. + +## 기능 + +- 현재 PC 전원 스위치 상태 조회 +- POST 요청 시 Home Assistant `switch.turn_on` 호출 +- 공통 인증 쿠키 확인 + +## 구조 + +- `api.php`: 상태 조회와 켜기 요청 처리 + +## 공통 모듈 연동 + +Home Assistant 주소, 토큰, 엔티티명, 인증 확인은 `/custom/common` 모듈을 사용합니다. + +## 운영 메모 + +직접 접근 가능한 전원 제어 API이므로 인증 없이 공개하면 안 됩니다. 현재는 공통 인증 쿠키가 있어야 호출됩니다. diff --git a/api.php b/api.php index dee57b3..4369092 100644 --- a/api.php +++ b/api.php @@ -1,67 +1,34 @@ - "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"]); + '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);