Require shared auth for wake requests
This commit is contained in:
@@ -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이므로 인증 없이 공개하면 안 됩니다. 현재는 공통 인증 쿠키가 있어야 호출됩니다.
|
||||||
@@ -1,67 +1,34 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../common/auth.php';
|
||||||
|
require_once __DIR__ . '/../common/ha.php';
|
||||||
|
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
|
header('Cache-Control: no-store');
|
||||||
|
|
||||||
// Home Assistant 설정
|
custom_require_auth();
|
||||||
$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) {
|
$config = custom_config();
|
||||||
global $HA_URL, $HA_TOKEN;
|
$entity = $config['entities']['wake_switch'];
|
||||||
|
|
||||||
$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'])) {
|
if (isset($_GET['status'])) {
|
||||||
$state = haRequest("GET", "/states/$ENTITY");
|
$state = custom_ha_state('seoul_wol', $entity);
|
||||||
|
if (!$state || !isset($state['state'])) {
|
||||||
if (!$state || !isset($state["state"])) {
|
custom_json(['result' => 'error'], 502);
|
||||||
echo json_encode(["result" => "error"]);
|
|
||||||
exit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
echo json_encode([
|
custom_json([
|
||||||
"result" => "ok",
|
'result' => 'ok',
|
||||||
"state" => $state["state"] // "on" or "off"
|
'state' => $state['state'],
|
||||||
]);
|
]);
|
||||||
exit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
--------------------------------------------
|
$res = custom_ha_request('seoul_wol', 'POST', '/services/switch/turn_on', [
|
||||||
2) 스위치 켜기 요청 (POST)
|
'entity_id' => $entity,
|
||||||
--------------------------------------------
|
|
||||||
*/
|
|
||||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|
||||||
$res = haRequest("POST", "/services/switch/turn_on", [
|
|
||||||
"entity_id" => $ENTITY
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
echo json_encode(["result" => "sent"]);
|
custom_json(['result' => $res === null ? 'error' : 'sent'], $res === null ? 502 : 200);
|
||||||
exit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
echo json_encode(["result" => "invalid_request"]);
|
custom_json(['result' => 'invalid_request'], 400);
|
||||||
|
|||||||
Reference in New Issue
Block a user